gpt4 book ai didi

java - 为什么我的 ItemListener 程序不允许我从 Java 中的两个不同的 JComboBoxes 中选择一个选项?

转载 作者:行者123 更新时间:2023-11-29 05:25:33 27 4
gpt4 key购买 nike

当我试图将两个数字相加以显示最终数字时,我的程序一次只会执行一个 JcomboBox。它允许我运行程序,但它不会显示最终价格,因为它不想一次选择两个东西。

这是我的程序的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CottageRental2 extends JFrame implements ItemListener{
// Declare all instance data (primitives and objects used)
private int WIDTH = 676;
private int HEIGHT = 321;
Container con;
private JLabel label1;
private JLabel label2;
private JLabel label3;
JComboBox cbox1;
JComboBox cbox2;
String [ ] roomChoice = {"1 Bedroom: $600","2 Bedroom: $800","3 Bedroom: $1000"};
String [ ] activityChoice = {"Horse Back Riding: $60","Rafting: $40","Row Boat Rental: $50"};
ImageIcon icon1 = new ImageIcon("C:\\Users\\Coding\\Desktop\\cottage.jpeg");
Font f1 = new Font("Ariel", Font.BOLD, 30);

//constructor
public CottageRental2(){
super("Cottage Rental");
con = getContentPane();
con.setLayout(new BorderLayout());
con.setBackground(Color.GRAY);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void createGUI(){
label1 = new JLabel("Woodberry Cottage Rental", JLabel.CENTER);
label1.setFont(f1);
label1.setForeground(Color.WHITE);
label2 = new JLabel("Rental Amount Due: $660", JLabel.CENTER);
label2.setFont(f1);
label2.setForeground(Color.WHITE);
label3 = new JLabel(icon1);
cbox1 = new JComboBox();
cbox1.addItem(roomChoice[0]);
cbox1.addItem(roomChoice[1]);
cbox1.addItem(roomChoice[2]);
cbox1.addItemListener(this);
cbox2 = new JComboBox();
cbox2.addItem(activityChoice[0]);
cbox2.addItem(activityChoice[1]);
cbox2.addItem(activityChoice[2]);
cbox2.addItemListener(this);
con.add(label1, BorderLayout.NORTH);
con.add(label2, BorderLayout.SOUTH);
con.add(label3, BorderLayout.CENTER);
con.add(cbox1, BorderLayout.WEST);
con.add(cbox2, BorderLayout.EAST);
}

public void itemStateChanged(ItemEvent event){
Object source = event.getSource();
int price1 = 0;
int price2 = 0;
if(source == cbox1){
int roomIndex = cbox1.getSelectedIndex();
if(roomIndex == 0){
price1 = 600;
}
if(roomIndex == 1){
price1 = 800;
}
if(roomIndex == 2){
price1 = 1000;
}
}
if(source == cbox2){
int activityIndex = cbox2.getSelectedIndex();
if(activityIndex == 0){
price2 = 60;
}
if(activityIndex == 1){
price2 = 40;
}
if(activityIndex == 2){
price2 = 50;
}
}
label2.setText("Rental Amount Due: $" + (price1 + price2));
}

public static void main(String[] args){
CottageRental2 object = new CottageRental2();
object.createGUI();
object.setSize(675, 320);
}
}

最佳答案

您的问题是您的 if (source == ...) if block 阻止程序从另一个 JComboBox 获取所选项目,该 JComboBox 未被主动选择。

一个解决方案:摆脱在监听器中测试事件源的违规 if block :

public void itemStateChanged(ItemEvent event) {
// Object source = event.getSource();
int price1 = 0;
int price2 = 0;
// if(source == cbox1){
int roomIndex = cbox1.getSelectedIndex();
if (roomIndex == 0) {
price1 = 600;
} else if (roomIndex == 1) {
price1 = 800;
} else if (roomIndex == 2) {
price1 = 1000;
}
// }
// if(source == cbox2){
int activityIndex = cbox2.getSelectedIndex();
if (activityIndex == 0) {
price2 = 60;
} else if (activityIndex == 1) {
price2 = 40;
} else if (activityIndex == 2) {
price2 = 50;
}
// }
label2.setText("Rental Amount Due: $" + (price1 + price2));
}

此外,如果您在项目选择语句中使用一些 else if block 会更安全。

关于java - 为什么我的 ItemListener 程序不允许我从 Java 中的两个不同的 JComboBoxes 中选择一个选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795604/

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