gpt4 book ai didi

Java Swing 下拉列表

转载 作者:行者123 更新时间:2023-12-02 06:09:13 30 4
gpt4 key购买 nike

我使用 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);
}

任何帮助将不胜感激。谢谢。

最佳答案

看起来您走在完全错误的轨道上。我认为您将 ItemListener 的使用与列表模型的设置混合在一起。看看这个guide to ComboBox - 它应该包含您需要的所有信息。

关于Java Swing 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039109/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com