Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用数组填充JComboBox时出现问题_Arrays_Swing_Jframe_Jbutton_Jcombobox - Fatal编程技术网

Arrays 使用数组填充JComboBox时出现问题

Arrays 使用数组填充JComboBox时出现问题,arrays,swing,jframe,jbutton,jcombobox,Arrays,Swing,Jframe,Jbutton,Jcombobox,我已经在这方面工作了一段时间,但我似乎无法修复给我带来所有问题的那一行。数组下面的这行“list=newjcombobox(measure);”似乎给出了错误。我可以抑制错误并运行它,但JComboBox不能正常工作并给出错误 线程“AWT-EventQueue-0”java.lang.ClassCastException中的“异常”:javax.swing.JComboBox不能转换为javax.swing.JButton 在cube.actionPerformed上(cube.java:13

我已经在这方面工作了一段时间,但我似乎无法修复给我带来所有问题的那一行。数组下面的这行“list=newjcombobox(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操作侦听器

看看这是否如您所愿:

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

问题是,既然你这样做了:

list.addActionListener(this);
// some other stuff...
button1.addActionListener(this);
button2.addActionListener(this);
然后,传递给方法
多维数据集的事件源可以是
列表
按钮1
按钮2

因此,您必须先检查源的类型,然后再将其强制转换为任何内容,并在
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();
}
final Object source=e.getSource();
字符串actionCommand=null;
if(JButton的源实例){
actionCommand=((JButton)e.getSource()).getActionCommand();
}else if(JComboBox的源实例){
actionCommand=((JComboBox)e.getSource()).getSelectedItem().toString();
}

干杯

如果我取消对第二个度量定义的注释,那么我将从第94行开始标记错误。度量值定义为字符串[],您正在尝试将其设置为字符串。我会从那里开始。我会看一下代码,看看我是否能弄明白你想做什么。谢谢!我完全摆脱了CBSelect操作事件,只是将您的代码添加到actionPerformed方法中,它工作得非常好。我所做的只是在其中添加if语句来更改度量字符串。再次感谢!