1

我想创建一个程序,其中用户按下按钮并且必须在文本框中输入一个单词,一旦他们输入了他们必须按下回车按钮的文本,他们输入的单词将与另一个字符串进行检查。我可以让它检查他们输入的字符串,但我不确定我会怎么做,所以用户必须先选择一个按钮,然后输入文本,然后按 Enter 按钮。

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

例如,四个带有图像bag cat house lamp post的按钮,用户选择一个按钮,然后他们需要使用文本框来拼写单词,然后按回车键来检查文本框中的文本是否与某个字符串匹配。

谢谢

这是我尝试过的:

 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();
}
}
4

3 回答 3

2

这是一个针对字符串测试输入并将输出附加到JTextArea. 它甚至使用LayoutManager, 你会发现它比布局有用1000倍(至少) 。null

在此处输入图像描述

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();
        }
    }
}
于 2013-04-14T18:32:42.440 回答
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。并非如此,您的内部 if 语句将永远不会运行。

这是我认为您正在尝试做的一个示例。

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
           }
       });
    }
}
于 2013-04-14T18:34:07.493 回答
1

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

在此处输入图像描述

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();
}
}
于 2013-04-14T18:43:55.113 回答