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

时间:2014-04-01 19:52:51

标签: java swing jcombobox itemlistener

当我试图将两个数字相加以显示最终数字时,我的程序将只执行一个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 个答案:

答案 0 :(得分:3)

你的问题是你的if (source == ...) if阻止程序从其他JComboBox获取所选项目的块,而不是主动选择的那个。

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

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块,则会更安全。