Eventqueue 0 java lang classcastexception

Исключение в потоке «AWT-EventQueue-0» java.lang.ClassCastException в моем классе JFrame

Я делаю симуляцию жизни, у меня есть волк, который едят овец, овцы едят траву. Это просто, но я не эксперт в материалах JFrame и JPanel . Это мой код JFrame :

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class InterfaceGraphique extends JFrame < private Case[][] caseMemoire = new Case[16][16]; public InterfaceGraphique()< setSize(800, 825); setTitle("Evolution"); setLocationRelativeTo(null); Container c = getContentPane(); c.setLayout(null); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); for(int i = 0; i> setVisible(true); > public Case getCase(int i, int j) < return caseMemoire[i][j]; >public class Case extends JPanel < private int coordX; private int coordY; private JLabel image; public Case(int x, int y)< coordX = x; coordY = y; setBounds(coordX, coordY, 55, 55); image = new JLabel(new ImageIcon("../images/Grass.png")); this.setLayout(new BorderLayout()); this.add(image); >public void setImg(int i) < switch(i)< case 0: this.remove(image); image = new JLabel( new ImageIcon("../images/Grass.png")); this.setLayout(new BorderLayout()); this.add(image); break; case 1: this.remove(image); break; case 2: this.remove(image); image = new JLabel( new ImageIcon("../images/Wolf.png")); this.setLayout(new BorderLayout()); this.add(image); break; case 3: this.remove(image); image = new JLabel( new ImageIcon("../images/Sheep.png")); this.setLayout(new BorderLayout()); this.add(image); break; >> public JLabel getImage() < return image; >> public void afficheUniv(Univers u) < for (int i=0; i < u.getLignes(); i++)< for (int j=0; j < u.getColones(); j++) < if(u.getCellule(i, j).isLoup()) caseMemoire[i][j].setImg(2); else if(u.getCellule(i, j).isMouton()) caseMemoire[i][j].setImg(3); else if(u.getCellule(i, j).isHerbe()) caseMemoire[i][j].setImg(0); else if(u.getCellule(i, j).isSels()) caseMemoire[i][j].setImg(1); else caseMemoire[i][j].setImg(1); >> > public static void main(String[] args) < Univers u = new Univers (16, 16, 5, 5); InterfaceGraphique evolution = new InterfaceGraphique(); while(u.getNbMoutons()+u.getNbLoups() >0) < u.simulation(); evolution.afficheUniv(u); evolution.setVisible(true); >JOptionPane.showMessageDialog(null, "Il n'y a plus d'animaux en vie !", "Fin", JOptionPane.PLAIN_MESSAGE); > 

> Компиляция javac Interfacegraphique.java работает нормально. Но когда я запускаю свою программу ( java InterfaceGraphique ), у меня есть ошибка:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException at javax.swing.LayoutComparator.compare(LayoutComparator.java:75) at javax.swing.LayoutComparator.compare(LayoutComparator.java:42) at java.util.TimSort.countRunAndMakeAscending(TimSort.java:356) at java.util.TimSort.sort(TimSort.java:230) at java.util.Arrays.sort(Arrays.java:1512) at java.util.ArrayList.sort(ArrayList.java:1454) at java.util.Collections.sort(Collections.java:175) at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136) at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110) at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:445) at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166) at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:535) at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:420) at java.awt.Component.dispatchEventImpl(Component.java:4752) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:719) at java.awt.EventQueue$4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 

Источник

Читайте также:  Input в массив java

Exception in thread «AWT-EventQueue-0» java.lang.ClassCastException: javax.swing.JCom

I’m pretty lost on this. I have no idea how to get my program to work. Basically, when an option is selected a price should show up in the text box automatically. Thank you all for your help. Exception in thread «AWT-EventQueue-0» java.lang.ClassCastException: javax.swing.JComboBox cannot be cast to javax.swing.JTextField
at BCard$ComboListener.actionPerformed(BCard.java:164)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:567)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:603)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:816)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:480)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

import javax.swing.*; import java.awt.event.*; public class BCard extends JFrame < private JPanel panel; //private JPanel panel2; //private JPanel panel3; //private JPanel panel4; // private JPanel panel5; //private JPanel panel6; //private JPanel panel7; private JLabel msgLbl; private JLabel lbl; private JLabel lbl2; private JRadioButton oneB; private JRadioButton bothB; private JRadioButton glossB; private JRadioButton matteB; private JRadioButton twoDB; private JRadioButton sevenB; private JRadioButton wksB; private JRadioButton reqB; private JRadioButton dontB; private JTextField bCTotal; private String[] bC = ; private JComboBox bCQty; private ButtonGroup radioButtonGroupSd; private ButtonGroup radioButtonGroupQual; private ButtonGroup radioButtonGroupDel; private ButtonGroup radioButtonGroupDes; private final int WINDOW_WIDTH = 300; private final int WINDOW_HEIGHT = 200; boolean value = true; public BCard() < setTitle("Business Cards"); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buildBCPanel(); add(panel); //add(panel2); //add(panel3); //add(panel4); //add(panel5); //add(panel6); //add(panel7); setVisible(value); >private void buildBCPanel() < msgLbl = new JLabel("Select the criteria that fits your needs:"); lbl = new JLabel("Quantity:"); bCQty = new JComboBox(bC); oneB = new JRadioButton("One side"); bothB = new JRadioButton("Both sides"); glossB = new JRadioButton("Gloss"); matteB = new JRadioButton("Matte"); twoDB = new JRadioButton("Next Day"); sevenB = new JRadioButton("Seven Days"); wksB = new JRadioButton("Up to Two Weeks"); reqB = new JRadioButton("Request Design"); dontB = new JRadioButton("Don't Require"); lbl2 = new JLabel("Your total may be $"); bCTotal = new JTextField(10); bCTotal.setEditable(false); radioButtonGroupSd = new ButtonGroup(); radioButtonGroupSd.add(oneB); radioButtonGroupSd.add(bothB); radioButtonGroupQual = new ButtonGroup(); radioButtonGroupQual.add(glossB); radioButtonGroupQual.add(matteB); radioButtonGroupDel = new ButtonGroup(); radioButtonGroupDel.add(twoDB); radioButtonGroupDel.add(sevenB); radioButtonGroupDel.add(wksB); radioButtonGroupDes = new ButtonGroup(); radioButtonGroupDes.add(reqB); radioButtonGroupDes.add(dontB); bCQty.addActionListener(new ComboListener()); //oneB.addActionListener(new RadioButtonListener()); // bothB.addActionListener(new RadioButtonListener()); //glossB.addActionListener(new RadioButtonListener()); //matteB.addActionListener(new RadioButtonListener()); //twoDB.addActionListener(new RadioButtonListener2()); //sevenB.addActionListener(new RadioButtonListener2()); //wksB.addActionListener(new RadioButtonListener2()); //reqB.addActionListener(new RadioButtonListener()); //dontB.addActionListener(new RadioButtonListener()); panel = new JPanel(); panel.add(msgLbl); //panel2 = new JPanel(); panel.add(lbl); panel.add(bCQty); //panel3 = new JPanel(); panel.add(oneB); panel.add(bothB); //panel4 = new JPanel(); panel.add(glossB); panel.add(matteB); // panel5 = new JPanel(); panel.add(twoDB); panel.add(sevenB); panel.add(wksB); // panel6 = new JPanel(); panel.add(reqB); panel.add(dontB); //panel7 = new JPanel(); panel.add(lbl2); panel.add(bCTotal); >private class ComboListener implements ActionListener < public void actionPerformed (ActionEvent e) < double oneHun = 8.95; double twoFif = 11.95; double fiveHun = 12.95; double oneThou = 13.95; double twoThou = 27.95; String oneHunS = Double.toString(oneHun); String twoFifS = Double.toString(twoFif); String fiveHunS = Double.toString(fiveHun); String oneThouS = Double.toString(oneThou); String twoThouS = Double.toString(twoThou); if("100".equals(e.getActionCommand())) < JTextField bCTotal = (JTextField)e.getSource(); bCTotal.setText(oneHunS); >else if("250".equals(e.getActionCommand())) < JTextField bCTotal = (JTextField)e.getSource(); bCTotal.setText(twoFifS); >else if("500".equals(e.getActionCommand())) < JTextField bCTotal = (JTextField)e.getSource(); bCTotal.setText(fiveHunS); >else if("1000".equals(e.getActionCommand())) < JTextField bCTotal = (JTextField)e.getSource(); bCTotal.setText(oneThouS); >else < JTextField bCTotal = (JTextField)e.getSource(); bCTotal.setText(twoThouS); >> > public static void main(String[] args) < BCard bCard = new BCard(); >>
  • 2 Contributors
  • 4 Replies
  • 3K Views
  • 7 Hours Discussion Span
  • Latest Post 13 Years Ago Latest Post by lwschjang
Читайте также:  Python path walk files

e.getSource() returns the component that generated the event, which in your case is the combo box. You cannot cast that JComboBox reference to a JTextField.

All 4 Replies

e.getSource() returns the component that generated the event, which in your case is the combo box. You cannot cast that JComboBox reference to a JTextField.

e.getSource() returns the component that generated the event, which in your case is the combo box. You cannot cast that JComboBox reference to a JTextField.

JTextField bCTotal = (JTextField)e.getSource();

in each if statement. When I did that and selected a quantity, 27.95 showed up in the textfield. I realize that was do to the fact that the final if statement did not have the statement

if("#".equals(e.getActionCommand()))

in it. So I am figuring that I need to find a different way to go about finding the quantity. Any help on how I should go about this would be great.

Источник

Exception in thread «AWT-EventQueue-0» java.lang.ClassCastException: javax.swing.KeyS

send pies

posted 17 years ago

  • Report post to moderator
  • Please excuse if this is in the wrong section but as the beginning of the error said Thread i figured probably the best place to put it.

    I am currently working on a Java Chat program, and have been encountering this error that is driving me insane!!

    It only seems to happen now and again, most of the time the program works without a problem but then all of a sudden it will begin to produce this error:

    Exception in thread «AWT-EventQueue-0» java.lang.ClassCastException: javax.swing.KeyStroke

    Has anyone else come accross this problem?

    If not has anyone got any idea y this problem is occuring?

    author and iconoclast

    Chrome

    send pies

    posted 17 years ago

  • Report post to moderator
  • This is a Swing error; it’s telling you on what thread the exception occurred, but that doesn’t make it a threading problem.

    Do you see a whole stack trace? That would allow you to see what code is running when the error occurs. Hopefully, it’s your own code and you can find out who is making the bad cast.

    I’m going to move this to the Swing/AWT forum for further discussion.

    Marshal

    send pies

    posted 17 years ago

  • Report post to moderator
  • You need to check the line where the exception occurred, which you can find in a few minutes by following the printed stack trace. You have probably got all the details you require in the heading of the thread (thank you for giving such a helpful title).
    Go to the line in questions where you might find something like this:-or maybe:-What class casting does is to take an object and apply the name of a class to it. BUT that object has to be an instance of that class. Otherwise the exception.

    If you look in the API specification for KeyStroke, you find it extends AWTKeyStroke which extends Object. I couldn’t find a known subclass for KeyStroke on the API, but you might have one yourself, let’s call it JohnsKeyStroke. So you can cast a KeyStroke or a JohnsKeysStroke to KeyStroke. You can also cast a KeyStroke to AWTKeyStroke or Object, but both those casts are rather pointless.
    If you have another subclass of AWTKeyStroke, let’s say John’sAWTKeyStroke, you can’t cast that to KeyStroke.

    I hope I have made things clearer rather than confused you.

    send pies

    posted 17 years ago

  • Report post to moderator
  • lol, i am still a little confused but it is just due to the randomness of the error. But i will continue my research into it.

    Here is what was printed after the error, im not entirely sure where to look tbh.

    Exception in thread «AWT-EventQueue-0» java.lang.ClassCastException: javax.swing.KeyStroke
    at java.util.TreeMap.compare(TreeMap.java:1093)
    at java.util.TreeMap.put(TreeMap.java:465)
    at java.util.TreeSet.add(TreeSet.java:210)
    at javax.swing.JComponent.getManagingFocusBackwardTraversalKeys(JComponent.java:409)
    at javax.swing.JTextArea.(JTextArea.java:203)
    at javax.swing.JTextArea.(JTextArea.java:118)
    at client.clientChat.initComponents(clientChat.java:58)
    at client.clientChat.(clientChat.java:37)
    at client.clientGUI.bChatActionPerformed(clientGUI.java:209)
    at client.clientGUI.access$100(clientGUI.java:18)
    at client.clientGUI$2.actionPerformed(clientGUI.java:134)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
    at java.awt.Component.processMouseEvent(Component.java:5488)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    at java.awt.Component.processEvent(Component.java:5253)
    at java.awt.Container.processEvent(Container.java:1966)
    at java.awt.Component.dispatchEventImpl(Component.java:3955)
    at java.awt.Container.dispatchEventImpl(Container.java:2024)
    at java.awt.Component.dispatchEvent(Component.java:3803)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
    at java.awt.Container.dispatchEventImpl(Container.java:2010)
    at java.awt.Window.dispatchEventImpl(Window.java:1774)
    at java.awt.Component.dispatchEvent(Component.java:3803)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

    Hopefully this will aid in people understading what is wrong, all the things being traced to my program i will put the code for below:

    tConvo = new javax.swing.JTextArea();

    //call from within the constructor
    initComponents();

    public class clientGUI extends javax.swing.JFrame
    implements Runnable

    Sorry for the lenght of this message, its just i have no idea where to move forward with this error.

    Any ideas would be gr8ly appreciated,

    John
    [ June 04, 2006: Message edited by: John Bartlett ]

    Источник

    Оцените статью