在一个面板上,有一个下拉菜单,其值为(1,2,3,4,5,6,7,9,10)
当下拉菜单的值为(1,2,3,4,5,6,7,9),面板上没有任何文字显示
当下拉菜单的值为(10),面板上显示(电脑连接最大负载)文字提示
根据楼上哥们意思 送个demo。。。
[code="java"]
public static void main(String[] args) {
String labels[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
JFrame frame = new JFrame("demojava-swing-测试");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
final JComboBox comboBox = new JComboBox(labels);
comboBox.setMaximumRowCount(5);
comboBox.setEditable(true);
contentPane.add(comboBox, BorderLayout.NORTH);
final JTextArea textArea = new JTextArea();
textArea.setVisible(false);
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(scrollPane, BorderLayout.CENTER);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// textArea.append("Selected: " + comboBox.getSelectedItem());
// textArea.append(", Position: " + comboBox.getSelectedIndex());
if(comboBox.getSelectedItem().equals("10"))
{
textArea.setVisible(true);
textArea.append("电脑连接最大负载");
}
}
};
comboBox.addActionListener(actionListener);
frame.setSize(300, 200);
frame.setVisible(true);
}
[/code]
1、JComboBox通过 addActionListener 添加监听器;
2、每当下拉列表变的时候,监听器会触发;
3、通过调用JComboBox的 getSelectedItem() 得到值;
4、判断值是否是10 是就显示面板,,否则就隐藏