我想创建在用户按下一个按钮,在文本框中输入一个单词一个程序中的字符串相匹配,一旦他们进入他们的文字按下输入按钮,他们输入的单词将被另一个字符串检查。我可以得到它来检查他们输入的字符串,但我不知道我会怎么做,所以用户必须先选择一个按钮,然后在文本中输入,然后按下输入按钮。检查是否在一个文本框的文本一旦按钮被按下
将会有多个按钮供用户选择,它们将会有图像,用户需要写入这些图像在文本框中的内容以检查该单词是否正确,他们将按下另一个按钮去检查。
例如在bag
cat
house
lamp 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();
}
}
究竟是什么你想,当用户按下第一个按钮的发生呢? –
我不确定你在问什么,你尝试过JOptionPane.showInputDialog()吗? –
对不起,但我什么都不明白,请你简短明了地问你的问题。 –