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

时间:2016-10-30 23:00:43

标签: java swing click jbutton

我使用java swing创建了一个GUI,在特定情况下,JButton没有响应,我必须单击两次。在单击时,它获取textArea中的信息并将其发送到要解析的TextParser类。如果我之后在区域中输入更多东西,然后单击evaluateButton,它就不会响应,我必须再次单击才能工作。有谁知道这是一个已知的bug还是我可以解决这个问题?

该课程的代码如下。

/**
 * 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);
}

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

1 个答案:

答案 0 :(得分:0)

正如Reimeus和Jason C在评论中所说的那样,我应该一直使用ActionListener,它完美无缺。