用数组填充JComboBox的问题

时间:2014-02-05 21:25:59

标签: arrays swing jframe jbutton jcombobox

我已经在这方面工作了一段时间,但我似乎无法修复给我所有问题的那一行。这行“list = new JComboBox(measure);”在数组下面,似乎给了我错误。我可以抑制错误,它运行除了JComboBox不能正常工作并给出错误  “线程中的异常”AWT-EventQueue-0“java.lang.ClassCastException:javax.swing.JComboBox无法强制转换为javax.swing.JButton     在cube.actionPerformed(cube.java:132)“

任何帮助将不胜感激!

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
//@SuppressWarnings("unchecked")
 public class cube extends JFrame implements ActionListener{
 public static JComboBox list;
 public static JTextField a;
 public static JTextField b;
 public static JTextField c;
 public static JTextField d;
 public static String measureFinal;
 public static String measurement;
 //public static String [] measurement = {"Inches" , "Meters", "Feet" , "Centimeters" , "Millimeters" , "Yards" };
 public static JFrame main = new JFrame("Volume of Cube");
 public static JPanel myPanel = new JPanel(new GridLayout (0,1));
 public static void main(String args[]){
     cube object = new cube();
    }
    cube(){
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myPanel.setPreferredSize(new Dimension(400,350));
    main.add(myPanel);
    a = new JTextField(3);
    b = new JTextField(3);
    c = new JTextField(3);
    d = new JTextField(3);
    JButton button1 = new JButton("Solve!");
    JButton button2 = new JButton("Back to shape selector");
    myPanel.add(new JLabel ("Select the unit of measurement"));

    String measure[] = {"Inches" , "Meters", "Feet" , "Centimeters" , "Millimeters" , "Yards" };
    list = new JComboBox(measure);
    list.setSelectedIndex(0);
    list.addActionListener(this);
    myPanel.add(list);
    myPanel.add(new JLabel ("Enter the height of the box"));
    myPanel.add(a);
    myPanel.add(new JLabel ("Enter the width of the box"));
    myPanel.add(b);
    myPanel.add(new JLabel ("Enter the length of the box"));
    myPanel.add(c);

    myPanel.add(button1);
    button1.addActionListener(this);
    myPanel.add(button2);
    button2.addActionListener(this);
    main.pack();
    main.setLocation(600,200);
    main.setVisible(true);




    //tring [] measurement = {"Inches" , "Meters", "Feet"};

    //double volume = height * width * length;
    //String answer = "The volume of the box is " +volume+ measurement;

}
public void CBSelect(ActionEvent e){
  int temp = 0;
  temp = list.getSelectedIndex();
  //Object temp = e.getSource();

  /*
  if (e.getSource() == list){
      temp = list.getSelectedIndex();
      switch(temp){
          case 0:
          measurement = "Inches";
          break;
          case 1:
          measurement = "Meters";
          break;
          case 2:
          measurement = "Feet";
          break;
          case 3:
          measurement = "Centimeters";
          break;
          case 4:
          measurement = "Millimeters";
          break;
          case 5:
          measurement = "Yards";
          break;

        }
    } 
    */
  if (temp == 0){
      measurement = "Inches";
    } else if (temp == 1){
      measurement = "Meters";
    }else if (temp == 2){
      measurement = "Feet";
    }else if (temp == 3){
      measurement = "Centimeters";
    }else if (temp == 4){
      measurement = "Millimeters";
    }else if (temp == 5){
      measurement = "Yards";
    }
}
  public void actionPerformed(ActionEvent e) {
  String actionCommand = ((JButton) e.getSource()).getActionCommand();
  //JComboBox cb = ((JComboBox) e.getSource());
  //measureFinal = (String)cb.getSelectedItem();
  if (actionCommand == "Solve!"){
    double height = Double.parseDouble(a.getText());
    double width = Double.parseDouble(b.getText());
    double length = Double.parseDouble(c.getText());
    double volume = height * width * length;
     try{
    final ImageIcon icon = new ImageIcon(new URL("http://wiki.fantasticcontraption.com/w/images/0/06/Solved.png"));
    JOptionPane.showMessageDialog(this, ("The volume of the box is " +volume+" "+ measurement),"Volume", JOptionPane.PLAIN_MESSAGE, icon);
   } catch (MalformedURLException ex){
    System.out.println("Image Not Found!");
   }
    }
  if (actionCommand == "Back to shape selector"){
      main.dispose();
      main.setVisible(false);
    }
}

}

更新:回顾它我现在认为问题出在CBSelect Action Listener中。

2 个答案:

答案 0 :(得分:1)

事情是,因为你这样做:

list.addActionListener(this);
// some other stuff...
button1.addActionListener(this);
button2.addActionListener(this);

传递给您的方法cube#actionPerformed的事件的来源可以是listbutton1button2

因此,您需要检查来源的类型,然后再将其转换为任何内容,并在actionPerformed方法的正文中访问其属性。你可以这样做:

final Object source = e.getSource();
String actionCommand = null;
if(source instanceof JButton) {
    actionCommand = ((JButton) e.getSource()).getActionCommand();
} else if(source instanceof JComboBox<?>) {
    actionCommand = ((JComboBox) e.getSource()).getSelectedItem().toString();
}

干杯!

答案 1 :(得分:0)

看看这是否像您想要的那样:

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
//@SuppressWarnings("unchecked")
public class cube extends JFrame implements ActionListener{
public static JComboBox list;
public static JTextField a;
public static JTextField b;
public static JTextField c;
public static JTextField d;
public static String measureFinal;
public static String [] measurement = {"Inches" , "Meters", "Feet" , "Centimeters" , "Millimeters" , "Yards" };
public static JFrame main = new JFrame("Volume of Cube");
public static JPanel myPanel = new JPanel(new GridLayout (0,1));
public static void main(String args[]){
    cube object = new cube();
}
cube(){
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myPanel.setPreferredSize(new Dimension(400,350));
    main.add(myPanel);
    a = new JTextField(3);
    b = new JTextField(3);
    c = new JTextField(3);
    d = new JTextField(3);
    JButton button1 = new JButton("Solve!");
    JButton button2 = new JButton("Back to shape selector");
    myPanel.add(new JLabel ("Select the unit of measurement"));

    String measure[] = {"Inches" , "Meters", "Feet" , "Centimeters" , "Millimeters" , "Yards" };
    list = new JComboBox(measure);
    list.setSelectedIndex(0);
    list.addActionListener(this);
    myPanel.add(list);
    myPanel.add(new JLabel ("Enter the height of the box"));
    myPanel.add(a);
    myPanel.add(new JLabel ("Enter the width of the box"));
    myPanel.add(b);
    myPanel.add(new JLabel ("Enter the length of the box"));
    myPanel.add(c);

    myPanel.add(button1);
    button1.addActionListener(this);
    myPanel.add(button2);
    button2.addActionListener(this);
    main.pack();
    main.setLocation(600,200);
    main.setVisible(true);

}
public void CBSelect(ActionEvent e){
    int temp = 0;
    temp = list.getSelectedIndex();
}

public void actionPerformed(ActionEvent e) {
    String actionCommand = ((JButton) e.getSource()).getActionCommand();
    measureFinal = (String)list.getSelectedItem();
    if (actionCommand == "Solve!"){
        double height = Double.parseDouble(a.getText());
        double width = Double.parseDouble(b.getText());
        double length = Double.parseDouble(c.getText());
        double volume = height * width * length;
        try{
            final ImageIcon icon = new ImageIcon(new URL("http://wiki.fantasticcontraption.com/w/images/0/06/Solved.png"));
            JOptionPane.showMessageDialog(this, ("The volume of the box is " +volume+" "+ measureFinal),"Volume", JOptionPane.PLAIN_MESSAGE, icon);
        } catch (MalformedURLException ex){
            System.out.println("Image Not Found!");
        }
    }
    if (actionCommand == "Back to shape selector"){
        main.dispose();
        main.setVisible(false);
    }
}
}