我要做的是创建一个依赖于组合框中选择的JLabel,即如果选择“History Book”,则创建标签“Enter period”,如果选择“Fictional Book”,则创建标签“Enter类型”。事情是无论我选择什么标签是不可见的,它应该保持空的地方。我错的任何想法?
这是我的代码:
JComboBox comboBox = new JComboBox(bookTypes);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"History Book", "Fictional Book", "Textbook"}));
comboBox.setToolTipText("Choose the type of book here!");
comboBox.setBounds(196, 48, 156, 20);
ActionListener cbActionListener = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s = (String) comboBox.getSelectedItem();
if(s.equals("History Book")){
JLabel lblWriteThePeriod = new JLabel("Write the Period:");
lblWriteThePeriod.setBounds(10, 317, 142, 14);
dialog.getContentPane().add(lblWriteThePeriod);
}else if(s.equals("Fictional Book")){
JLabel lblWriteTheGenre = new JLabel("Write the genre:");
lblWriteTheGenre.setBounds(10, 317, 142, 14);
dialog.getContentPane().add(lblWriteTheGenre);
}
}
};
comboBox.addActionListener(cbActionListener);
dialog.getContentPane().add(comboBox);
dialog.setVisible(true);