gpt4 book ai didi

java - JComboBox 仅在从第 1 到第 2 时更改组件(不是从第 3 到第 2)

转载 作者:行者123 更新时间:2023-12-01 12:58:04 24 4
gpt4 key购买 nike

所以我尝试寻找解决方案,但一无所获,也不知道如何使我的搜索更准确。我用 Java 做了这个作业,但我被一个 JComboBox 困住了,它并不总是改变我的 JPanel 中的组件。这是我第一次发布问题,如果我搞砸了,很抱歉。无论如何,这就是代码(目的是为一家航空公司创建 GUI)。

问题是,当我从选项 1(“office”)转到选项 2(“Pilot”)时,JComboBox 执行其应该执行的操作并更改 JPanel code> (jplType) 应该如此,但是当我从选项 3(“Host”)返回到选项 2 时,它根本不会更改 JPanel (jplType)。

问题是,我在

中放置了一个 System.out.println()
if(index == 1)
{
// ..
}

它确实打印了它,但由于某种原因它无法添加JCheckBox

如果有人能弄清楚我在这里搞砸了什么,我将不胜感激,因为我已经花了一个小时了。

提前致谢。

public class abc {

public static void main(String[] args) {
CompanyDialog abc = new CompanyDialog();

}

}

class CompanyDialog extends JFrame {

private String[] employee = {"office", "Pilot", "Host"};
private String[] employeeTitles = {"Office options", "Pilot options", "Host options"};
public final static int GAP = 3;
// Frame Constants.
public final static int FWIDTH = 605;
public final static int FHEIGHT = 235;
// Constant for the Combo Box.
public final static int CBWIDTH = FWIDTH/3-4;
public final static int CBHEIGHT = 20;
// Width constant for TextField.
public final static int TWIDTH = 20;

private JPanel jplType = new JPanel(new FlowLayout(FlowLayout.LEFT, GAP, GAP));

private JComboBox jcbType = new JComboBox(employee);
private JLabel jlblOptions = new JLabel();
private JTextField jtfOptions = new JTextField(TWIDTH);

public CompanyDialog(){
super("Worker Dialog");

//Combo box panel configurations
JPanel jplWorker = new JPanel(new FlowLayout(FlowLayout.LEFT, GAP, GAP));

JTextField nametxt = new JTextField(TWIDTH);
jcbType.setPreferredSize(new Dimension(CBWIDTH,CBHEIGHT));
jplWorker.add(jcbType);

setDisplay(0);

add(jplWorker, BorderLayout.NORTH);
setAlwaysOnTop(true);
setVisible(true);
setSize(FWIDTH, FHEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);

jcbType.addItemListener(new ItemListener() { /**Handle item selection*/
public void itemStateChanged(ItemEvent e) {
jplType.removeAll();
setDisplay(jcbType.getSelectedIndex());
}
}
);
}
public void setDisplay(int index){
jplType.add(jlblOptions);
jplType.add(jtfOptions);
jplType.setBorder(new TitledBorder(employeeTitles[index]));
if(index == 0){
jlblOptions.setText("Office:");
jtfOptions.setText("");
}
else if(index == 1) {
jlblOptions.setText("Air Time:");
jtfOptions.setText("0");
JCheckBox jcbCaptain = new JCheckBox("Is a Captain");
jplType.add(jcbCaptain);
}
else if(index == 2){
jlblOptions.setText("Air Time:");
jtfOptions.setText("0");
}

add(jplType, BorderLayout.CENTER);
}
}

最佳答案

更改 setDisplay() 方法

public void setDisplay(int index){
jplType.add(jlblOptions);
jplType.add(jtfOptions);
jplType.setBorder(new TitledBorder(employeeTitles[index]));
if(index == 0){
jlblOptions.setText("Office:");
jtfOptions.setText("");
}
else if(index == 1) {
jlblOptions.setText("Air Time:");
jtfOptions.setText("0");
JCheckBox jcbCaptain = new JCheckBox("Is a Captain");
jplType.add(jcbCaptain);
}
else if(index == 2){
jlblOptions.setText("Air Time:");
jtfOptions.setText("0");
}
else
{
jlblOptions.setText("");
jtfOptions.setText("");
}
add(jplType, BorderLayout.CENTER);

}

public void setDisplay(int index){
jplType.add(jlblOptions);
jplType.add(jtfOptions);
jplType.setBorder(new TitledBorder(employeeTitles[index]));
if(index == 0){
jlblOptions.setText("Office:");
jtfOptions.setText("");
}
else if(index == 1) {
jlblOptions.setText("Air Time:");
jtfOptions.setText("0");
JCheckBox jcbCaptain = new JCheckBox("Is a Captain");
jplType.add(jcbCaptain);
}
else if(index == 2){
jlblOptions.setText("Air Time:");
jtfOptions.setText("0");
}
else
{
jlblOptions.setText("");
jtfOptions.setText("");
}
add(jplType, BorderLayout.CENTER);

this.revalidate();
}

关于java - JComboBox 仅在从第 1 到第 2 时更改组件(不是从第 3 到第 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23734947/

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