gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:00:25 27 4
gpt4 key购买 nike

如何在程序初始化时禁用我的btnExecute JButton
之后,当三个JComboBox选择项不等于“Choose...”项时,启用此按钮。
这个问题的第二部分解决了,但是当启动程序时启用了按钮,这应该解决为只允许在 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);
}

最佳答案

你想要 setEnabled JButton 的方法:

btnExecute.setEnabled(false);

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

关于java - 初始化此程序时如何禁用 Java Swing JButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35360363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com