当选择那“控计学院”时,组合框中显示信安和智能班级,当选择“电气学院”时,组合框显示电气和通信班级。
我点按钮显示的是乱码
package zuoye;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
class Demo implements ItemListener{
private JFrame frame;
private JPanel panel;
private JComboBox<String[]> jb;
private JRadioButton jr1,jr2;
private Label l1,l2,l3,l4;
private String f1[],f2[];
Demo(){
f1 = new String[] {"信安2201","智能2201"};
f2 = new String[] {"电气2201","通信2201"};
frame = new JFrame("选项类组件");
frame.setSize(400,400);
panel = new JPanel();
panel.setSize(400,400);
frame.add(panel);
jr1 = new JRadioButton("控计学院");
jr2 = new JRadioButton("电气学院");
ButtonGroup bg = new ButtonGroup();
bg.add(jr1);
bg.add(jr2);
jb = new JComboBox();
jb.setEditable(false);
jr1.addItemListener(this);
jr2.addItemListener(this);
panel.add(jr1);
panel.add(jr2);
panel.add(jb);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e) {
jb.removeAllItems();
if(e.getItem().equals(jr1))
{jb.addItem(f1);System.out.println(f1);}
else if(e.getItem().equals("电气学院"))
{jb.addItem(f2);System.out.println(f2);}
}
}
public class zuoye1 {
public static void main(String[] args) {
Demo l1 = new Demo();
}
}