JButton 在 JAVA 中启用后需要点击两次

问题描述

我正在尝试构建一个带有四个手动输入的 JFrame JTextFields:描述、小时、分钟、ID 和两个 JButton:提交和重置

我需要禁用“提交”按钮,直到所有文本字段都有一些数据。为了实现这一点,我使用了 DocumentListener 并且它工作正常,也就是说,最初“提交”按钮被禁用,只有当所有文本字段都有一些输入时才会启用。

问题:“提交”按钮启用后,我需要点击提交按钮两次才能获得实际操作触发器(可能第一次是在之后恢复焦点)被启用,第二次做实际工作)。 “重置”按钮不会发生此问题。

尝试过但无效:我尝试使用 submit.requestFocusInWindow() 恢复焦点并将焦点移至“提交”按钮,但最后修改的文本字段失去焦点,我不得不单击再次专注于该领域。

请帮忙。我是 StackOverflow 的新手,所以请不要关闭线程。

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));

JPanel titlePanel = new JPanel();
JPanel descPanel = new JPanel();
JPanel timeWorkedPanel = new JPanel();
JPanel iDPanel = new JPanel();

titlePanel.add(title);
mainPanel.add(titlePanel);

descPanel.add(desc);
mainPanel.add(descPanel);

timeWorkedPanel.add(hour);
timeWorkedPanel.add(minute);
mainPanel.add(timeWorkedPanel);

iDPanel.add(iD);
mainPanel.add(iDPanel);

JTextArea reqList = new JTextArea();
reqList.setLineWrap(false);
reqList.setEditable(false);
JScrollPane reqListScroll = new JScrollPane (reqList);
reqListScroll.setPreferredSize(new Dimension(400,400));
reqListScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
reqListScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
reqListScroll.setColumnHeaderView(new JLabel("               REQUEST                           CREATED                       TIME WORKED"));
mainPanel.add(reqListScroll);

List<JTextField> textFieldList = new ArrayList<>();
textFieldList.add(desc);
textFieldList.add(hour);
textFieldList.add(minute);
textFieldList.add(csiID);

JButton submit = new JButton("SUBMIT");
JButton reset = new JButton("RESET");

buttonPanel.add(submit);
buttonPanel.add(reset);
mainPanel.add(buttonPanel);
mainPanel.add(Box.createVerticalStrut(20)); // a spacer


JFrame mainFrame = new JFrame("Test Frame");
mainFrame.getContentPane().add(mainPanel);
mainFrame.setSize(new Dimension(500,600));
mainFrame.setLocationRelativeto(null);
mainFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);  
mainFrame.setVisible(true);

submit.setEnabled(false);

DocumentListener docListener = new DocumentListener() {

    @Override
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);                       
    }

    @Override
    public void changedUpdate(DocumentEvent e) {

        boolean isEnabled = true;
        for(JTextField tf : textFieldList) {
            if(tf.getText().isEmpty())
                isEnabled = false;
        }
        submit.setEnabled(isEnabled);
    }
    
};

for(JTextField tf : textFieldList) {
    tf.getDocument().addDocumentListener(docListener);
}


final WebDriver newDriver = driver;
final ChromeOptions newOptions = options;

//Click on "SUBMIT" button
submit.addActionListener(new ActionListener() {
    
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            System.out.println("submitting");
            mainFrame.setVisible(false);
            createTicket(newDriver,newOptions,subportfolio,title,desc,hour,minute,csiID);
            mainFrame.setVisible(true);
        } catch (InterruptedException e1) {

        }

    }
    
});


//Click on "RESET" button
reset.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        desc.setText(null);
        hour.setText(null);
        minute.setText(null);
        csiID.setText(null);
    }
    
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)