java - JButton 没有响应 - 必须单击两次

标签 java swing click jbutton

我使用 java swing 创建了一个 GUI,在特定情况下,JButton 无响应,我必须单击它两次。单击时,它会获取 textArea 中的信息并将其发送到 TextParser 类进行解析。如果我在之后的区域中输入更多内容,然后单击评估按钮,它不会响应,我必须再次单击它才能工作。有谁知道这是否是一个已知的错误或者我该如何解决这个问题?

该类的代码如下。

/**
 * Add the components to the GUI.
 * @param pane - the pane for the GUI
 */
public static void addComponentsToPane(Container pane) {
    pane.setLayout(new BorderLayout());
    JPanel instructionsPanel = new JPanel();
    JLabel instructions = new JLabel("Enter the email text below");
    instructionsPanel.setBackground(Color.LIGHT_GRAY);
    instructionsPanel.add(instructions);
    pane.add(instructionsPanel, BorderLayout.NORTH);

    JPanel textAreaPanel = new JPanel();
    textAreaPanel.setBackground(Color.LIGHT_GRAY);
    final JTextArea textArea = new JTextArea();
    textArea.setBackground(Color.WHITE);
    textArea.setMinimumSize(new Dimension(400,350));
    textArea.setMaximumSize(new Dimension(400,350));
    textArea.setPreferredSize(new Dimension(400,350));
    textArea.setLineWrap(true);
    Border border = BorderFactory.createLineBorder(Color.BLACK);
    textArea.setBorder(border);

    textArea.setMinimumSize(new Dimension(500, 200));
    textArea.setFont(new Font("Serif", Font.PLAIN, 16));
    textAreaPanel.add(textArea);
    pane.add(textAreaPanel, BorderLayout.CENTER);

    JPanel scoringPanel = new JPanel();
    JButton evaluateButton = new JButton("Evaluate Email");
    final JLabel scoreLabel = new JLabel("");
    JButton uploadFileBtn = new JButton("Upload File");
    JButton importTermsBtn = new JButton("Import Terms");
    scoringPanel.add(evaluateButton);
    scoringPanel.add(uploadFileBtn);
    scoringPanel.add(importTermsBtn);
    scoringPanel.add(scoreLabel);

    pane.add(scoringPanel, BorderLayout.SOUTH);

    evaluateButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                String email = textArea.getText();
                TextParser textParser = new TextParser(email);
                double score = textParser.parse();
                scoreLabel.setText(score+"");
            } catch (Exception ex) {
                System.out.println(ex);
            }

        }
    });

    uploadFileBtn.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            scoreLabel.setText("Feature not yet available.");
        }
    });

    importTermsBtn.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            DatabaseInput d = new DatabaseInput();
            d.main(null);
        }
    });
}

/**
 * Create the GUI and show it.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("EmailGUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setLocationRelativeTo(null);
    frame.setPreferredSize(new Dimension(500,500));
    frame.setTitle("Email Text Input");
    frame.setResizable(true);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setSize(screenSize.width, screenSize.height);
    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

我的主要方法只是调用createAndShowGUI()。我是 StackOverflow 的新手,因此如果我需要在帖子中提供更多或更少的信息,请告诉我!

最佳答案

正如 Reimeus 和 Jason C 在评论中所说,我应该使用完美运行的 ActionListener。

关于java - JButton 没有响应 - 必须单击两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334575/

相关文章:

java - 如何使用SwingWorker实时更新GUI?

java - 带有 GridLayout 和 JScrollPane 的额外列未显示 - Java Swing

javascript - 如何使用 click() 通过 id 激活此功能? (Javascript)

javascript - 事件处理/捕获

Java - 超时(无限循环)错误

java.lang.NoSuchMethodError : com. google.common.io.Closeables.closeQuietly(Ljava/io/Closeable;)V

java.lang.IllegalArgumentException : java. text.ParseException: header 结束

java - 如何在不破坏 CoordinatorLayout.LayoutParams 的情况下重写 CoordinatorLayout.Behavior

java - JColorChooser和JFileChooser开放问题

javascript - 错误选择器上的 jQuery 单击函数