如何在初始化此程序时禁用Java Swing JButton?

时间:2016-02-12 10:34:41

标签: java swing button jbutton jcombobox

如何在程序初始化时禁用btnExecute JButton? 之后,当三个JComboBox个选定项目不等于"选择..."项目,启用此按钮 此问题的第二部分已解决,但在启动程序时启用该按钮,如果三个JButton不等于"则应解析为仅允许JComboBox。选择......"项目

这是我的代码:

public class WebStarter_v2 {

private JFrame frame;

private JComboBox<Object> cbProgram;
private JComboBox<Object> cbVersion;
private JComboBox<Object> cbAction;

private JButton btnExecute;

private boolean programSelected = false;
private boolean versionSelected = false;
private boolean actionSelected = false;

private String prototypeValue="XXXXXXXXXX";

private String[] programs = {"Choose...", "Apache", "PHP-FPM", "MySQL"};
private String[] actions = {"Choose...", "start", "restart", "stop"};
//private String[] status = {"standby", "running", "stopped", "error"};

private String[] apacheVersions = {"Choose...", "2.2.31", "2.4.17"};
private String[] phpVersions = {"Choose...", "5.6.16", "7.0.0"};
private String[] mysqlVersions = {"Choose...", "5.7.9"};

public static void main(String[] args) {...}

public WebStarter_v2() {
    initialize();
}
private void initialize() {
...
JPanel panel_Actions = new JPanel();
splitPane.setTopComponent(panel_Actions);

...
...

cbProgram = new JComboBox<Object>(new DefaultComboBoxModel<Object>(programs));
cbProgram.setPrototypeDisplayValue(prototypeValue);
cbProgram.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent event) {
        if(event.getStateChange() == ItemEvent.SELECTED){
            String item = event.getItem().toString();
            if ( item.contains("Apache") ){
                makeComboBoxModel(cbVersion, apacheVersions);
                programSelected = true;
                updateButtonState();
            }                       
            else if ( item.contains("PHP-FPM") ){
                makeComboBoxModel(cbVersion, phpVersions);
                programSelected = true;
                updateButtonState();
            }
            else if ( item.contains("MySQL") ){
                makeComboBoxModel(cbVersion, mysqlVersions);
                programSelected = true;
                updateButtonState();
            }
            else
            {
                programSelected = false;
                //set initial state of cbVersion and cbAction
                cbVersion.setModel(new DefaultComboBoxModel<Object>());
                cbAction.setModel(new DefaultComboBoxModel<Object>());
                updateButtonState();
            }
        }
    }
});
...
...
cbVersion = new JComboBox<Object>();
cbVersion.setPrototypeDisplayValue(prototypeValue);
cbVersion.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent event) {
        if(event.getStateChange() == ItemEvent.SELECTED){
            String item = event.getItem().toString();
            if ( !item.contains("Choose...") )
            {
                versionSelected = true;
                makeComboBoxModel(cbAction, actions);
                updateButtonState();
            }
            else{
                versionSelected = false;
                cbAction.setModel(new DefaultComboBoxModel<Object>());
                updateButtonState();
            }
        }
    }
});
...
...
cbVersion = new JComboBox<Object>();
cbVersion.setPrototypeDisplayValue(prototypeValue);
cbVersion.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent event) {
        if(event.getStateChange() == ItemEvent.SELECTED){
            String item = event.getItem().toString();
            if ( !item.contains("Choose...") )
            {
                versionSelected = true;
                makeComboBoxModel(cbAction, actions);
                updateButtonState();
            }
            else{
                versionSelected = false;
                cbAction.setModel(new DefaultComboBoxModel<Object>());
                updateButtonState();
            }
        }
    }
});
...
...
cbAction = new JComboBox<Object>();
cbAction.setPrototypeDisplayValue(prototypeValue);
cbAction.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent event) {
        if(event.getStateChange() == ItemEvent.SELECTED){
            String item = event.getItem().toString();
            if ( !item.contains("Choose...") ){
                actionSelected = true;
                updateButtonState();
            }
            else{
                actionSelected = false;
                updateButtonState();
            }
        }
    }
});
...
...
btnExecute = new JButton("Execute");
...
...
JPanel panel_Messages = new JPanel();
splitPane.setBottomComponent(panel_Messages);
}

2 个答案:

答案 0 :(得分:3)

您需要JButton的{​​{3}}方法:

btnExecute.setEnabled(false);

之后,当您的用户选择三个JComboBox时,您可以将其设置回true

答案 1 :(得分:1)

或者你可以setVisible()为此:

btnExecute.setVisible(false);

然后在每个JComboBox的addItemListener中,您应该检查programSelectedversionSelectedActionSelected。如果所有这些都是true,请制作

btnExecute.setVisible(true);