所以我尝试寻找解决方案,但一无所获,也不知道如何让我的搜索更准确。
我有这个用于Java的家庭作业,我遇到了JComboBox
,但并不总是改变我JPanel
中的组件。
这是我第一次发帖提问,对不起,如果我搞砸了这个问题。
无论如何,这是代码(目的是为飞行公司创建GUI)。
问题在于,当我从选项1(“办公室”)转到选项2(“试用”)时,JComboBox
执行它应该做的事情并将JPanel
(jplType)更改为应该,但当我从选项3(“主机”)回到选项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);
}
}
答案 0 :(得分:1)
从
更改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();
}
答案 1 :(得分:0)
private JPanel jplType = new JPanel(new FlowLayout(FlowLayout.LEFT, GAP, GAP));
////////////////////////////
///////////////////////////
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);
//
//
revalidate();
repaint();
//
}
错误的想法是期望Layout会自己安排他的内容,特别是当你使用如FlowLayout这样简单的布局时