2013-04-14 75 views
1

我想创建在用户按下一个按钮,在文本框中输入一个单词一个程序中的字符串相匹配,一旦他们进入他们的文字按下输入按钮,他们输入的单词将被另一个字符串检查。我可以得到它来检查他们输入的字符串,但我不知道我会怎么做,所以用户必须先选择一个按钮,然后在文本中输入,然后按下输入按钮。检查是否在一个文本框的文本一旦按钮被按下

将会有多个按钮供用户选择,它们将会有图像,用户需要写入这些图像在文本框中的内容以检查该单词是否正确,他们将按下另一个按钮去检查。

例如在bagcathouselamp post四个按钮与图像用户选择一个按钮,然后他们需要使用文本框拼写单词,他们请按Enter检查在文本框中的文本是否有一定的字符串匹配。

感谢

这里是我曾尝试:

public class Textb extends JPanel{ 

JFrame frame =new JFrame(); 
JPanel panel =new JPanel(); 
JButton enter =new JButton("Enter"); 
JButton wordBtn =new JButton("Cat"); 
JTextField tb =new JTextField(); 

public Textb() { 

    // Panel and button layout 

    panel.setLayout(null); 
    panel.setBackground(Color.WHITE); 
    panel.setCursor(new Cursor(Cursor.HAND_CURSOR)); // set the cursor to a hand 

    Insets insets = panel.getInsets(); 

    tb.setVisible(true); 
    tb.setBounds(200 + insets.left, 5 + insets.top, 110,60); 
    tb.setBackground(Color.YELLOW); 


    enter.setLayout(null); 
    enter.setBounds(10 + insets.left, 5 + insets.top, 110,60); 
    enter.setBackground(Color.WHITE); 
    enter.setBorder(BorderFactory.createEmptyBorder()); 
    enter.setFocusPainted(false); 

    wordBtn.setLayout(null); 
    wordBtn.setBounds(10 + insets.left, 70 + insets.top, 110,60); 
    wordBtn.setBackground(Color.WHITE); 
    wordBtn.setBorder(BorderFactory.createEmptyBorder()); 
    wordBtn.setFocusPainted(false); 


    panel.add(tb); 
    panel.add(enter); 
    panel.add(wordBtn); 
    frame.add(panel); 
    frame.setTitle("Matching"); 
    frame.setSize(800, 600); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 



    // This is where i did the action listener 
    enter.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ae) 
     { 
      if(ae.getSource().equals(wordBtn)) 
      { 
       if(tb.getText().equals("cat")){ 
        tb.setText("Correct"); 
       } 
      } 
     } 
    }); 
} 


public static void main(String[] args) { 
    new Textb(); 
} 
} 
+1

究竟是什么你想,当用户按下第一个按钮的发生呢? –

+0

我不确定你在问什么,你尝试过JOptionPane.showInputDialog()吗? –

+0

对不起,但我什么都不明白,请你简短明了地问你的问题。 –

回答

1

一个简单flag变量应该工作:

enter image description here

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class TestingCat extends JPanel{ 


JFrame frame =new JFrame(); 
JPanel panel =new JPanel(); 
private int flag=0; 



JButton enter =new JButton("Enter"); 
JButton wordBtn =new JButton("Cat"); 

JTextField tb =new JTextField(); 




public TestingCat() { 


// Panel and button layout 

panel.setLayout(null); 
panel.setBackground(Color.WHITE); 
panel.setCursor(new Cursor(Cursor.HAND_CURSOR)); // set the cursor to a hand 

Insets insets = panel.getInsets(); 

tb.setVisible(true); 
tb.setBounds(200 + insets.left, 5 + insets.top, 110,60); 
tb.setBackground(Color.YELLOW); 


enter.setLayout(null); 
enter.setBounds(10 + insets.left, 5 + insets.top, 110,60); 
enter.setBackground(Color.WHITE); 
enter.setBorder(BorderFactory.createEmptyBorder()); 
enter.setFocusPainted(false); 

wordBtn.setLayout(null); 
wordBtn.setBounds(10 + insets.left, 70 + insets.top, 110,60); 
wordBtn.setBackground(Color.WHITE); 
wordBtn.setBorder(BorderFactory.createEmptyBorder()); 
wordBtn.setFocusPainted(false); 


panel.add(tb); 
panel.add(enter); 
panel.add(wordBtn); 
frame.add(panel); 
frame.setTitle("Matching"); 
frame.setSize(800, 600); 
frame.setLocationRelativeTo(null); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setVisible(true); 



// This is where i did the action listener 
wordBtn.addActionListener(new ActionListener(){ 

public void actionPerformed(ActionEvent ae) 

{ 
flag=1; 
} 

}); 

enter.addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent ae) 
{ 

JFrame f=new JFrame(); 
if(ae.getSource().equals(enter)) 
    { 
     if(flag==1) 
     { 
      flag=0; 
    if(tb.getText().equals("cat")){ 
      tb.setText("Correct"); 
     } 
    } 
     else 
      JOptionPane.showMessageDialog(f,"enter cat 1st"); 
} 

} 
}); 


} 


public static void main(String[] args) { 
new TestingCat(); 
} 
} 
2

下面是对字符串测试输入和输出追加到一个JTextArea的一个简单的例子。它甚至采用了LayoutManager的东西,你会发现次(至少)比null布局更加有用。

enter image description here

public class Test { 
    private static String ENTER = "Enter"; 
    static JButton enterButton; 
    public static JTextArea output; 
    public static JTextField input; 
    static JFrame frame; 
    static JPanel panel; 
    public static String testString = "test"; 

    public static void main(String... args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     createFrame(); 
    } 

    public static void createFrame() 
    { 
     frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     panel = new JPanel(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
     panel.setOpaque(true); 
     ButtonListener buttonListener = new ButtonListener(); 
     output = new JTextArea(15, 50); 
     output.setWrapStyleWord(true); 
     output.setEditable(false); 
     JScrollPane scroller = new JScrollPane(output); 
     scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     JPanel inputpanel = new JPanel(); 
     inputpanel.setLayout(new FlowLayout()); 
     input = new JTextField(20); 
     enterButton = new JButton("Enter"); 
     enterButton.setActionCommand(ENTER); 
     enterButton.addActionListener(buttonListener); 
     // enterButton.setEnabled(false); 
     input.setActionCommand(ENTER); 
     input.addActionListener(buttonListener); 
     DefaultCaret caret = (DefaultCaret) output.getCaret(); 
     caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); 
     panel.add(scroller); 
     inputpanel.add(input); 
     inputpanel.add(enterButton); 
     panel.add(inputpanel); 
     frame.getContentPane().add(BorderLayout.CENTER, panel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     // Center of screen 
     // frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setResizable(false); 
     input.requestFocus(); 
    } 

    public static class ButtonListener implements ActionListener 
    { 

     public void actionPerformed(final ActionEvent ev) 
     { 
      Thread thread = new Thread() 
      { 

       public void run() 
       { 
        if (!input.getText().trim().equals("")) 
        { 
         String cmd = ev.getActionCommand(); 
         if (ENTER.equals(cmd)) 
         { 
          output.append(input.getText()); 
          if (input.getText().trim().equals(testString)) output.append(" = " + testString); 
          else output.append(" != " + testString); 
          output.append("\n"); 
         } 
        } 
        input.setText(""); 
        input.requestFocus(); 
       } 
      }; 
      thread.start(); 
     } 
    } 
} 
1

我不知道你想什么做的正是,但我不认为你的代码将工作的这部分...

enter.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent ae) 
{ 

if(ae.getSource().equals(wordBtn)) 
    { 
    if(tb.getText().equals("cat")){ 
     tb.setText("Correct"); 
} 

} 

} 
}); 

你”重新添加一个ActionListener进入,然后检查wordBtn是否被点击。这不是你的内心如果陈述永远不会运行。

这里是什么,我认为你试图做一个例子。

public class textb extends JPanel { 

    int counter = 0; 

    public textb() { 
     JButton enter = new JButton("Enter"); 
     JButton wordBtn = new JButton("Cat"); 

     enter.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       counter++; 
      } 
     }); 

     wordBtn.addActionListener(new ActionListener() { 
      if (counter > 0) { 
       //insert your code to check the String here, because the 
       //only way to increment counter is by pressing the enter button 
       //this code will not run unless enter has been pressed at least once 
       //to make counter greater than zero, you could also use a boolean 
       //set it to true when enter is pressed and check to see if its true 
       //instead of checking if counter is greater than zero 
      } 
     }); 
    } 
} 
+0

我想要的是,如果输入按钮已被按下,wordBtn也被按下,那么它会检查字符串是否匹配。 – Rachel

+0

我的例子回答了你的问题吗? –

+0

我现在已经做到了我想做的事情,谢谢你给出了答案。 – Rachel

相关问题