使用 `setEnabled` 来在特定文本字段填写内容时启用按钮。

huangapple go评论70阅读模式
英文:

Use setEnabled to enable button if specific textfields are filled

问题

我有一个项目,需要使用 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);
    }
};

这是我目前的进展,但我觉得还有很多缺失,我对此不太熟悉,无法找到问题所在。我试图按照这里建议的方法来处理它:链接

英文:

I have a project in which i have to enable and disable the use of buttons (addRn, addSw, and addCy) with setEnabled(). I have tried many things including adding a documentListener but I am getting too confused. Does anyone have an idea what to do?

  ArrayList&lt;JTextField&gt; run = new ArrayList&lt;&gt;();
        run.add(intervals);
        run.add(minRest);
        
        ArrayList&lt;JTextField&gt; swim = new ArrayList&lt;&gt;();
        swim.add(intervals);
        swim.add(minRest);
        swim.add(loc);
        
        ArrayList&lt;JTextField&gt; cycle = new ArrayList&lt;&gt;();
        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);
			}
        };

this is what I have so far, but i feel like there is quite a bit missing and I am not familiar enough to find the problem. I tried to approach it as was suggested here

答案1

得分: 1

// 更新:我成功解决了这个问题

public class TrainingRecordGUI extends JFrame implements ActionListener, DocumentListener {

将 DocumentListener 添加到公共类中

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

public TrainingRecordGUI() {
 add(intervals);
        intervals.setEditable(true);
        intervals.getDocument().addDocumentListener(this); // 添加用于启用按钮的 DocumentListener
 add(labminr);
        add(minRest);
        minRest.setEditable(true);
        minRest.getDocument().addDocumentListener(this);// 添加用于启用按钮的 DocumentListener
        add(labloc);
        add(loc);
        loc.setEditable(true);
        loc.getDocument().addDocumentListener(this);// 添加用于启用按钮的 DocumentListener
        add(labter);
        add(terrain);
        terrain.setEditable(true);
        terrain.getDocument().addDocumentListener(this);// 添加用于启用按钮的 DocumentListener
        add(labtempo);
        add(tempo);
        tempo.setEditable(true);
        tempo.getDocument().addDocumentListener(this);// 添加用于启用按钮的 DocumentListener
}

然后添加按钮应启用和不应启用的最终代码

// 按钮启用/禁用代码

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

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

@Override
public void changedUpdate(DocumentEvent e) {
	// 设置运行、游泳和骑行按钮是否启用的布尔变量
	boolean canEnableRn = false;
	boolean canEnableSw = false;
	boolean canEnableCy = false;
	
	// 如果填充了运行所需以外的任何字段
	   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; // 将 canEnableRn 设置为 false,以防止启用按钮
	   }else { canEnableRn = true;} // 否则设置为 true,允许用户添加到运行
	   addRn.setEnabled(canEnableRn); // 将按钮设置为布尔值
}
英文:

UPDATE I managed to figure it out

public class TrainingRecordGUI extends JFrame implements ActionListener, DocumentListener {

add DocumentListener to public class

then register the DocumentListener with the textfields that you want to use

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
}

then add final code for the cases where the button should be enabled and when it shouldn't

// 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(&quot;&quot;) &amp;&amp; minRest.getText().equals(&quot;&quot;) 
			   || !intervals.getText().equals(&quot;&quot;) &amp;&amp; minRest.getText().equals(&quot;&quot;) 
			   || intervals.getText().equals(&quot;&quot;) &amp;&amp; !minRest.getText().equals(&quot;&quot;)
			   || !loc.getText().equals(&quot;&quot;) &amp;&amp; !terrain.getText().contentEquals(&quot;&quot;) &amp;&amp; !tempo.getText().equals(&quot;&quot;)) {
		   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
}

huangapple
  • 本文由 发表于 2020年4月8日 02:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61087043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定