我有一个带有JTextFields,JComboBoxes和JLabels的JFrame。我想制作按钮"更新"只有在窗口中的某些内容已被更改(字段)时才可单击。
答案 0 :(得分:5)
我想只在窗口中的某些内容被更改(字段)时才能使“UPDATE”按钮可单击。
你需要倾听你的“领域”(无论它们应该是什么)的变化,并在那个听众中调用你感兴趣的JButton上的setEnabled(true)
或setEnabled(false)
- 或者更好关于它的动作,取决于你的领域的状态。为什么行动更好?你可以有几个按钮和JMenuItems共享相同的Action,通过调用Actions上的方法,你可以同时禁用它们。
例如,如果字段是JTextFields,则将DocumentListener添加到字段中,并根据侦听器中字段的状态调用上面提到的方法。
如果您需要更多具体的帮助,那么您需要向我们提供更具体的信息和代码。
对于一个包含5个JTextField和一个JButton的示例,其中只有在每个 JTextField中都有文本时,JButton才处于活动状态:
package pkg;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@SuppressWarnings("serial")
public class ButtonActiveTest extends JPanel {
private static final int FIELD_COUNT = 5;
private MyAction myAction = new MyAction("My Button"); // The Action
private JButton myButton = new JButton(myAction); // the button
private JTextField[] textFields = new JTextField[FIELD_COUNT];
public ButtonActiveTest() {
myAction.setEnabled(false); // initially disable button's Action
// create single Doc listener
MyDocumentListener myDocumentListener = new MyDocumentListener();
// create jtext fields in a loop
for (int i = 0; i < textFields.length; i++) {
textFields[i] = new JTextField(5); // create each text field
// add document listener to doc
textFields[i].getDocument().addDocumentListener(myDocumentListener);
// add to gui
add(textFields[i]);
}
add(myButton);
}
private static void createAndShowGui() {
ButtonActiveTest mainPanel = new ButtonActiveTest();
JFrame frame = new JFrame("ButtonActiveTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private class MyAction extends AbstractAction {
public MyAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic); // to give button an alt-key hotkey
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ButtonActiveTest.this, "Button Pressed");
}
}
private class MyDocumentListener implements DocumentListener {
@Override
public void changedUpdate(DocumentEvent e) {
checkFields();
}
@Override
public void insertUpdate(DocumentEvent e) {
checkFields();
}
@Override
public void removeUpdate(DocumentEvent e) {
checkFields();
}
// if any changes to any document (any of the above methods called)
// check if all JTextfields have text. If so, activate the action
private void checkFields() {
boolean allFieldsHaveText = true;
for (JTextField jTextField : textFields) {
if (jTextField.getText().trim().isEmpty()) {
allFieldsHaveText = false;
}
}
myAction.setEnabled(allFieldsHaveText);
}
}
}
显示类似的代码,但现在JMenuItem与JButton共享相同的Action。请注意,菜单项和按钮并行处于活动/非活动状态:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@SuppressWarnings("serial")
public class ButtonActiveTest extends JPanel {
private static final int FIELD_COUNT = 5;
private MyAction myAction = new MyAction("Process Data in Fields"); // The Action
private JButton myButton = new JButton(myAction); // the button
private JTextField[] textFields = new JTextField[FIELD_COUNT];
private JMenuBar menuBar = new JMenuBar();
public ButtonActiveTest() {
myAction.setEnabled(false); // initially disable button's Action
// create single Doc listener
MyDocumentListener myDocumentListener = new MyDocumentListener();
// create jtext fields in a loop
for (int i = 0; i < textFields.length; i++) {
textFields[i] = new JTextField(5); // create each text field
// add document listener to doc
textFields[i].getDocument().addDocumentListener(myDocumentListener);
// add to gui
add(textFields[i]);
}
add(myButton);
// create menu item with our action
JMenuItem menuItem = new JMenuItem(myAction);
JMenu menu = new JMenu("Menu");
menu.setMnemonic(KeyEvent.VK_M);
menu.add(menuItem);
menuBar.add(menu);
}
public JMenuBar getMenuBar() {
return menuBar;
}
private static void createAndShowGui() {
ButtonActiveTest mainPanel = new ButtonActiveTest();
JFrame frame = new JFrame("ButtonActiveTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setJMenuBar(mainPanel.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private class MyAction extends AbstractAction {
public MyAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic); // to give button an alt-key hotkey
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ButtonActiveTest.this, "Action Performed!");
}
}
private class MyDocumentListener implements DocumentListener {
@Override
public void changedUpdate(DocumentEvent e) {
checkFields();
}
@Override
public void insertUpdate(DocumentEvent e) {
checkFields();
}
@Override
public void removeUpdate(DocumentEvent e) {
checkFields();
}
// if any changes to any document (any of the above methods called)
// check if all JTextfields have text. If so, activate the action
private void checkFields() {
boolean allFieldsHaveText = true;
for (JTextField jTextField : textFields) {
if (jTextField.getText().trim().isEmpty()) {
allFieldsHaveText = false;
}
}
myAction.setEnabled(allFieldsHaveText);
}
}
}