【问题标题】:Issue populating JComboBox with array用数组填充 JComboBox 的问题
【发布时间】:2014-02-05 21:25:59
【问题描述】:

我已经为此工作了一段时间,但我似乎无法解决给我带来所有问题的一行。这一行“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 动作侦听器中。

【问题讨论】:

  • 如果我取消注释测量的第二个定义 - 然后我从第 94 行开始标记错误。测量被定义为 String[] 并且您试图将其设置为字符串。我会从那里开始。我将通过代码查看是否可以弄清楚您要做什么。

标签: arrays swing jframe jbutton jcombobox


【解决方案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();
}

干杯!

【讨论】:

  • 谢谢!我完全摆脱了 CBSelect 动作事件,只是将您的代码添加到 actionPerformed 方法中,它运行良好。我所做的只是在里面添加我的 if 语句来更改测量字符串。再次感谢!
【解决方案2】:

看看这是否如你所愿:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2015-09-08
    • 2021-04-12
    • 2014-10-03
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    相关资源
    最近更新 更多