代码之家  ›  专栏  ›  技术社区  ›  user3416335

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

  •  1
  • user3416335  · 技术社区  · 11 年前

    当我试图将两个数字相加以显示最终数字时,我的程序一次只能执行一个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);
      }
    }
    
    1 回复  |  直到 11 年前
        1
  •  3
  •   Hovercraft Full Of Eels    11 年前

    你的问题是你的 if (source == ...) 如果阻止程序从另一个JComboBox(未被主动选择的JComboBBox)获取所选项目的块。

    一种解决方案:消除在侦听器中测试事件源的有问题的if块:

    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 块。