Java中的侦听器有问题。我有一个下拉框,没有像我需要的那样传递用户选择
我有一个本周三要上的课的项目,我有一些问题:我有两个if-else语句控制两个不同下拉菜单中显示的值。对我来说,似乎我没有得到上一个下拉列表的信息(有两个下拉列表会影响下一个下拉列表中显示的值)
以下是我迄今为止的代码:
///Occupancy///
JLabel label2 = new JLabel("Please Select the Number of Occupants");
JComboBox occupancy_list = new JComboBox(occupancy_string);
occupancy_list.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)//Listener for Occupancy Drop Down
{
JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
String selection = (String) cb.getSelectedItem();
int number = Integer.parseInt((String) selection);
}
});
String selection = (String) occupancy_list.getSelectedItem();
int number = Integer.parseInt((String) selection);
if(number>2)
{
style=(style2);
}
else
{
style=(style1);
}
///Room Type///
JLabel label3 = new JLabel("Please Select a Room Style");
//Creates RoomStyle Drop Down
JComboBox room_type = new JComboBox(style);
roomtype_string=(String) room_type.getSelectedItem();
room_type.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)//Listener for Room Style Drop Down
{
JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
String selection = (String) cb.getSelectedItem();
}
});
String selection2 = (String) room_type.getSelectedItem();
if(selection2.equals("Cabin"))
{
room_number=(cabin_string);
}
else
{
room_number=(suite_string);
}
///Room Selection///
JLabel label4 = new JLabel("Please Select a Room");
//Creates RoomNumber Drop Down
JComboBox room_list = new JComboBox(room_number);
roomselected = (String) room_list.getSelectedItem();
room_list.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)//Listener for Occupancy Drop Down
{
JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
String selection = (String) cb.getSelectedItem();
}
});
String selection3 = (String) room_list.getSelectedItem();
//Dining
JLabel label5 = new JLabel("Please Select a Dining Time");
JComboBox dining_list = new JComboBox(dining_string);
dining_list.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)//Listener for Occupancy Drop Down
{
JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
String selection = (String) cb.getSelectedItem();
}
});
String selection4 = (String) dining_list.getSelectedItem();
注意:我已经重写了我的代码,仍然没有骰子: `//创建包含占用率、房间类型、房间和就餐时间请求的子面板2 JPanel subpanel=新的JPanel()
///Occupancy///
JLabel label2 = new JLabel("Please Select the Number of Occupants");
JComboBox occupancy_list = new JComboBox(occupancy_string);
Occupancy_Listener occupancy = new Occupancy_Listener();
occupancy_list.addActionListener(occupancy);
//updateStyle(occupancy_string[occupancy_list.getSelectedIndex()]);
///Room Type///
JLabel label3 = new JLabel("Please Select a Room Style");
//Creates RoomStyle Drop Down
JComboBox room_type = new JComboBox(style);
Style_Listener styleListen = new Style_Listener();
room_type.addActionListener(styleListen);
//updateNumber(style[room_type.getSelectedIndex()]);
///Room Selection///
JLabel label4 = new JLabel("Please Select a Room");
//Creates RoomNumber Drop Down
JComboBox room_list = new JComboBox(room_number);
Room_Listener room = new Room_Listener();
room_list.addActionListener(room);
//updateRoom(room_number[room_list.getSelectedIndex()]);
//Dining
JLabel label5 = new JLabel("Please Select a Dining Time");
JComboBox dining_list = new JComboBox(dining_string);
Din_Listener dining = new Din_Listener();
dining_list.addActionListener(dining);
//updateDin(dining_string[dining_list.getSelectedIndex()]);
...
...
...
...
...
}
private class Occupancy_Listener implements ActionListener
{
public void actionPerformed(ActionEvent event)//Listener for Occupancy Drop Down
{
JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
String selection = (String) cb.getSelectedItem();
System.out.println(selection);
System.out.println(style[0]);
System.out.println(room_number[0]);
updateStyle(selection);
}
}
private class Style_Listener implements ActionListener
{
public void actionPerformed(ActionEvent event)//Listener for Occupancy Drop Down
{
JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
String selection = (String) cb.getSelectedItem();
updateNumber(selection);
}
}
private class Room_Listener implements ActionListener
{
public void actionPerformed(ActionEvent event)//Listener for Occupancy Drop Down
{
JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
String selection = (String) cb.getSelectedItem();
updateRoom(selection);
}
}
private class Din_Listener implements ActionListener
{
public void actionPerformed(ActionEvent event)//Listener for Occupancy Drop Down
{
JComboBox cb = (JComboBox)event.getSource(); //grab the user selection
String selection = (String) cb.getSelectedItem();
updateDin(selection);
}
}
protected void updateStyle(String pick)
{
String[] style1 ={"Cabin", "Suite"};
String[] style2 ={"Suite"};
//ocu_string=pick;
number = Integer.parseInt(pick);
if(number>2)
{
style=style2;
}
else
{
style=style1;
}
}
protected void updateNumber(String pick)
{
String[] cabin_string = {"11-1","11-2","11-3","11-4","11-5","11-6","11-7","11-8","11-9","11-10"};
String[] suite_string = {"11-S1","11-S2"};
type=pick;
if(type.equals("cabin"))
{
room_number=cabin_string;
}
else
{
room_number=suite_string;
}
}
protected void updateRoom(String pick)
{
room_num=pick;
}
protected void updateDin(String pick)
{
din_time=pick;
}
//public String getPopulation()
{
//return ocu_string;
}
# 1 楼答案
匿名类(或本地类)中的代码不会与周围的代码同时执行,这可能会让您感到困惑
所以在一段代码中,它看起来像:
“运行东西”部分现在不运行。创建了一个对象,其中包含一个稍后可以在其中使用的方法,然后该对象被传递到。该方法将不会运行,直到有人专门调用它。当您到达
getSomething()
时,“Run stuff”部分还没有运行因此,在您的代码中:
创建占用列表,为其创建操作侦听器,并将该操作侦听器存储在占用列表中。但是
actionPerformed()
方法中编写的内容在GUI显示、用户选择某个内容并触发事件之前都不会运行GUI编程不同于控制台编程,在控制台编程中,您调用“scan()”,然后程序停止并等待输入内容。首先准备GUI,当显示GUI时,会根据用户的操作调用各种侦听器
因此,在将列表添加到GUI之前,您的代码直接跳转到
occupancy_list.getSelectedItem()
,当然,没有选择任何内容,也没有显示任何内容,因此其他下拉列表是在用户看到GUI之前创建的对于代码中的所有操作侦听器都是如此
编写动作监听器的正确方法是考虑GUI已经运行时的条件。您的操作侦听器仅在将被丢弃的局部变量中设置值。相反,当用户在操作侦听器中进行选择时,它们应该做您想做的事情
这意味着此组合框的操作侦听器必须创建其他列表并将其添加到GUI中,重新验证并重新绘制。或者,它可以简单地修改已经创建的其他列表,这取决于您认为最好的列表。但是他们必须访问这些列表,因此必须将这些列表定义为周围类的字段,并在操作侦听器中访问。请注意,您还可以定义一个执行此操作的方法,并从操作侦听器内部调用它
我的建议是,首先创建一个简单的GUI,只包含第一个组合框,并根据选择更改一些简单的内容。例如,您可以遵循Oracle Tutorial。了解如何编写更改简单内容的操作侦听器后,展开程序以添加其他列表,并从操作侦听器中操作它们
# 2 楼答案
这可能就是你的问题所在。从第一个组合框中选择一个项目,并将其填充到第二个组合框中: