Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果填充了特定的文本字段,请使用setEnabled启用按钮_Java_Swing_User Interface_Jbutton_Jtextfield - Fatal编程技术网

Java 如果填充了特定的文本字段,请使用setEnabled启用按钮

Java 如果填充了特定的文本字段,请使用setEnabled启用按钮,java,swing,user-interface,jbutton,jtextfield,Java,Swing,User Interface,Jbutton,Jtextfield,我有一个项目,其中我必须使用setEnabled()启用和禁用按钮(addRn、addSw和addCy)。我尝试过很多事情,包括添加documentListener,但我太困惑了。有人知道该怎么做吗 ArrayList<JTextField> run = new ArrayList<>(); run.add(intervals); run.add(minRest); ArrayList<JTextField>

我有一个项目,其中我必须使用setEnabled()启用和禁用按钮(addRn、addSw和addCy)。我尝试过很多事情,包括添加documentListener,但我太困惑了。有人知道该怎么做吗

  ArrayList<JTextField> run = new ArrayList<>();
        run.add(intervals);
        run.add(minRest);

        ArrayList<JTextField> swim = new ArrayList<>();
        swim.add(intervals);
        swim.add(minRest);
        swim.add(loc);

        ArrayList<JTextField> cycle = new ArrayList<>();
        cycle.add(tempo);
        cycle.add(terrain);

        DocumentListener listener = new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                changedUpdate(e);
            }

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

            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                boolean canEnable = true;
                for (JTextField intervals : run) {
                    intervals.getDocument().addDocumentListener(listener);
                    if (intervals.getText().isEmpty()) {
                        canEnable = false;
                    }
                }
                for (JTextField minRest : run) {
                    if (minRest.getText().isEmpty()) {
                        canEnable = false;
                    }
                }
                addRn.setEnabled(canEnable);
            }
        };
ArrayList run=new ArrayList();
运行。添加(间隔);
run.add(minRest);
ArrayList swim=新建ArrayList();
游泳。增加(间隔);
游泳。添加(minRest);
游泳。添加(loc);
ArrayList循环=新的ArrayList();
循环。添加(节奏);
循环。添加(地形);
DocumentListener=新建DocumentListener(){
@凌驾
公共作废移除更新(文档事件e){
更改日期(e);
}
@凌驾
公共作废插入更新(文档事件e){
更改日期(e);
}
@凌驾
公共作废更改日期(记录事件e){
布尔值=真;
for(JTextField间隔:运行){
interval.getDocument().addDocumentListener(listener);
if(interval.getText().isEmpty()){
canEnable=假;
}
}
for(JTextField minRest:run){
if(minRest.getText().isEmpty()){
canEnable=假;
}
}
addRn.setEnabled(canEnable);
}
};

到目前为止,这就是我所拥有的,但我觉得有相当多的缺失,我不太熟悉,无法找到问题所在。我试着按照建议接近它

更新我设法找到了答案

public class TrainingRecordGUI extends JFrame implements ActionListener, DocumentListener {
将DocumentListener添加到公共类

然后用您想要使用的文本字段注册DocumentListener

public TrainingRecordGUI() {
 add(intervals);
        intervals.setEditable(true);
        intervals.getDocument().addDocumentListener(this); //addDocumentListener for button enabling
 add(labminr);
        add(minRest);
        minRest.setEditable(true);
        minRest.getDocument().addDocumentListener(this);//addDocumentListener for button enabling
        add(labloc);
        add(loc);
        loc.setEditable(true);
        loc.getDocument().addDocumentListener(this);//addDocumentListener for button enabling
        add(labter);
        add(terrain);
        terrain.setEditable(true);
        terrain.getDocument().addDocumentListener(this);//addDocumentListener for button enabling
        add(labtempo);
        add(tempo);
        tempo.setEditable(true);
        tempo.getDocument().addDocumentListener(this);//addDocumentListener for button enabling
}
然后为应该启用按钮和不应该启用按钮的情况添加最终代码

// Button enabling/disabling code

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

}

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

}

@Override
public void changedUpdate(DocumentEvent e) {
    //boolean variables to set run, swim and cycle button enabled
    boolean canEnableRn = false;
    boolean canEnableSw = false;
    boolean canEnableCy = false;

    //if anything but necessary fields for run filled
       if (intervals.getText().equals("") && minRest.getText().equals("") 
               || !intervals.getText().equals("") && minRest.getText().equals("") 
               || intervals.getText().equals("") && !minRest.getText().equals("")
               || !loc.getText().equals("") && !terrain.getText().contentEquals("") && !tempo.getText().equals("")) {
           canEnableRn = false; // set canEnableRn false to prevent the button to be enabled
       }else { canEnableRn = true;} //else set true and allow user to add to run
       addRn.setEnabled(canEnableRn); // set button to boolean value
}

很好的书面问题(格式良好,表述清晰)。您是否在某个地方注册了
DocumentListener
,以便它可以生效?您好,我刚刚试过代码中的实现可以工作,但是当打开GUI时,按钮仍然可以工作,即使文本字段为空,寄存器的代码看起来是这样的“添加(地形)”;地形。设置可编辑(真);terrain.getDocument().addDocumentListener(此)`