gpt4 book ai didi

java - JCombobox 和 Switch 语句

转载 作者:行者123 更新时间:2023-12-01 12:54:42 25 4
gpt4 key购买 nike

我使用的是 Netbeans 6.9.1我正在尝试以酒店注册表的形式制作一个java桌面应用程序。在这个程序中,我有一系列的JComboBox,让用户输入入住的人数、预订的日期等。我想让它在按下按钮时切换发生时将检查是否在“无”或实际项目中选择了这些框。

我的问题是。我怎样才能做到这一点,如果尚未输入 1 个或多个组合框,它会显示一条消息,说明未填写的内容

这是我尝试过的,但没有成功。

case 1:
Hotel.getSelectedItem().toString().equals("None");
Out.setText("Missing Hotel Selection");

我的问题是,我怎样才能做到这一点?

我是 Java 新手,因此请用我能理解的术语进行解释。

最佳答案

假设您有三个名为:hotelSelectionnoOfPeopledayOfBooking 的 JComboBox 对象。因此,首先使用以下代码为这些组合框设置名称。之后我们将在 switch-case 中使用它。

hotelSelection.setName("Hotel Selection");
noOfPeople.setName("No of people");
dayOfBooking.setName("Day of booking");

现在,单击提交按钮时将调用以下方法。

private void submitButtonClicked(java.awt.event.ActionEvent evt) {                                     


// Array of all combobox in that you want to check
ArrayList<JComboBox> allCombobox = new ArrayList<>();
allCombobox.add(hotelSelection);
allCombobox.add(noOfPeople);
allCombobox.add(dayOfBooking);

for (JComboBox temp : allCombobox) {

switch (temp.getSelectedItem().toString()) {

case "None":
// If it is "hotel selection" combobox
if (temp.getName().equalsIgnoreCase("Hotel Selection"))
{
System.out.println("Missing Hotel Selection");
}
// If it is "no of people" combobox
else if (temp.getName().equalsIgnoreCase("No of people"))
{
System.out.println("Missing No of people");
}
// If it is "day of booking" combobox
else if (temp.getName().equalsIgnoreCase("Day of booking"))
{
System.out.println("Missing the Day of booking");
}

}
}

}

希望您能找到答案。

关于java - JCombobox 和 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23986441/

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