2014-02-05 134 views
0

我一直在這一段時間,但我似乎無法修復給我所有問題的一條線。這一行「list = new JComboBox(measure);」在陣列下面,似乎給了我錯誤。我可以抑制錯誤,它運行除了JComboBox無法正常工作,並給出錯誤 「線程中的異常」AWT-EventQueue-0「java.lang.ClassCastException:javax.swing.JComboBox不能轉換爲javax.swing。 JButton at cube.actionPerformed(cube.java:132)「問題與數組填充JComboBox

任何幫助將不勝感激!

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

}

UPDATE:回過頭來看,現在我想問題出在CBSelect動作偵聽器內。

+0

如果我取消註釋測量的第二個定義 - 那麼我會從第94行開始標記錯誤。度量定義爲String [],您試圖將其設置爲String。我會從那裏開始。我會查看代碼,看看我能否弄清楚你正在做什麼。 – mikemil

回答

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

乾杯!

+0

謝謝!我完全擺脫了CBSelect動作事件,只是將你的代碼添加到actionPerformed方法中,它完美地工作。我所做的只是添加我的if語句來更改測量字符串。再次感謝! – user3259415

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