我使用JAVA Swing创建了一个下拉列表。当我选择“跟踪RCM的状态:”时,我想在所选的选项旁边创建另一个下拉列表。我应该使用mouseactionlistener吗?我试图完成这样的事情,当我点击菜单选项时,我可以在菜单选项类别下选择另一个列表。示例:http://smoothjazztampabay.com/wp-content/rockettheme/rt_metropolis_wp/menu-options/dropdownmenu.jpg
我尝试使用此代码但不能。
if (state == ItemEvent.SELECTED)
{
ItemSelectable itemS = itemEvent.getItemSelectable();
String cmd = selectedString(itemS);
if ( cmd.equals("Keep track of status of RCM:"))
{
RCMCombo2.addItem(RCMCombo);
selectionPanel.add(RCMCombo2);
}
代码的完整版本如下所示:
String [] RCM2 = {"Keep track of status of RCM:", "Add and activate RCM", "Remove RCM",
"Display the usage statistics for RCM",
"Update capabilities of RCMs", "Show RCM used most frequently in the last n days",
"Display number of times the RCM was emptied in n hours"};
RCMCombo2 = new JComboBox(RCM2);
RCMCombo2.addItemListener(itemListener);
RCMCombo2.setEditable(false);
RCMCombo2.setBounds(10,10,10,30);
//"Updates capabilities of RCM"); Get the location of RCM");
selectionPanel.add(RCMCombo2);
ItemListener itemListener = new ItemListener()
{
public void itemStateChanged(ItemEvent itemEvent)
{
int state = itemEvent.getStateChange();
//System.out.println((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
//System.out.println("Item: " + itemEvent.getItem());
if (state == ItemEvent.SELECTED)
{
ItemSelectable itemS = itemEvent.getItemSelectable();
String cmd = selectedString(itemS);
if ( cmd.equals("Keep track of status of RCM:"))
{
RCMCombo2.addItem(RCMCombo2);
selectionPanel.add(RCMCombo2);
}
任何帮助将不胜感激。谢谢。
答案 0 :(得分:2)
看起来你的赛道完全错了。我认为您将ItemListener
的使用与列表模型的设置混合在一起。
查看此guide to ComboBox - 它应包含您需要的所有信息。