简体   繁体   English

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

[英]Why is my ItemListener program not allowing me to select an option from two different JComboBoxes in Java?

My program will only do one JcomboBox at a time when I am trying to get both numbers to be added together to display the final number. 当我试图将两个数字加在一起显示最终数字时,我的程序只会执行一个JcomboBox。 It allows me to run the program, but it will not display the final price because it does not want to select two things at once. 它允许我运行程序,但它不会显示最终价格,因为它不想一次选择两件事。

Here is the code for my program: 这是我的程序的代码:

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);
  }
}

Your problem is your if (source == ...) if blocks which prevent the program from getting the selected item from the other JComboBox, the one that isn't being actively selected. 你的问题是你的if (source == ...) if if阻止程序从另一个JComboBox中获取所选项目的块,这个块未被主动选择。

One solution: get rid of the offending if blocks that test for the source of the event in the listener: 一个解决方案:摆脱在监听器中测试事件源的块的问题:

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));
}

Also, it would be safer if you use some else if blocks in your item selection statements. 此外,如果您在项目选择语句中使用else if块,那么它会更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM