我遇到了一个小问题,我的代码中有一个异常。
我有一个基本框架类:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class BaseFrame extends JFrame {
private BaseListener listener = new BaseListener();
public static final String BTN_END = "Ende";
public static final String BTN_OK = "Ok";
private JButton bu_end = null;
private JButton bu_ok = null;
public BaseFrame() {
super();
initialize();
}
private void initialize() {
JPanel pa_01 = new JPanel();
JPanel pa_02 = new JPanel();
JPanel pa_03 = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout(0, 0));
this.setPreferredSize(new Dimension(800, 600));
this.setMinimumSize(new Dimension(800, 600));
this.add(pa_01, BorderLayout.NORTH);
this.add(pa_02, BorderLayout.CENTER);
this.add(pa_03, BorderLayout.SOUTH);
pa_01.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
pa_01.add(new JLabel("Rennwagenverwaltung"));
pa_01.setPreferredSize(new Dimension(800, 60));
pa_02.setLayout(new GridLayout(8, 2));
pa_02.add(new JLabel("Rennwagenid"));
pa_02.add(new JTextField());
pa_02.add(new JLabel("Modell"));
pa_02.add(new JTextField());
pa_02.add(new JLabel("Hersteller"));
pa_02.add(new JTextField());
pa_02.add(new JLabel("Leistung"));
pa_02.add(new JTextField());
pa_02.add(new JLabel("Hubraum"));
pa_02.add(new JTextField());
pa_02.add(new JLabel("maximaler Tankinhalt"));
pa_02.add(new JTextField());
pa_02.add(new JLabel("aktueller Tankinhalt"));
pa_02.add(new JTextField());
bu_end = new JButton(BaseFrame.BTN_END);
bu_end.addActionListener(listener);
bu_ok = new JButton(BaseFrame.BTN_OK);
bu_ok.addActionListener(listener);
pa_02.add(bu_end);
pa_02.add(bu_ok);
pa_02.setPreferredSize(new Dimension(800, 350));
pa_03.setBackground(Color.WHITE);
pa_03.setPreferredSize(new Dimension(800, 200));
this.setTitle("Rennwagen");
this.setVisible(true);
this.pack();
this.setLocationRelativeTo(null);
}
}
我的监听器类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
public class BaseListener
implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object object = e.getSource();
JButton bu_work = null;
JFrame frame = null;
String text = null;
if (object instanceof JButton)
{
bu_work = (JButton) object;
text = bu_work.getText();
frame = this.getMyJFrame(bu_work);
if(text == BaseFrame.BTN_OK) {
Rennwagen rw = null;
} else if(text == BaseFrame.BTN_END) {
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
}
/**
* @param button
* @return
*/
private BaseFrame getMyJFrame(JButton button)
{
JPanel contenpane = null;
JLayeredPane layeredpane = null;
JRootPane rootpane = null;
BaseFrame myframe = null;
contenpane = (JPanel) button.getParent();
layeredpane = (JLayeredPane) contenpane.getParent();
rootpane = (JRootPane) layeredpane.getParent();
myframe = (BaseFrame) rootpane.getParent();
return myframe;
}
}
我现在在这条线上有这样的专长:
layeredpane = (JLayeredPane) contenpane.getParent();
那就是整个错误日志:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JPanel cannot be cast to javax.swing.JLayeredPane
at Rennwagen.BaseListener.getMyJFrame(BaseListener.java:50)
at Rennwagen.BaseListener.actionPerformed(BaseListener.java:27)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
为什么那不可能?我在这里错了什么?
最佳答案
如果要获取JFrame,请调用getTopLevelAncestor。
关于java - javax.swing.JPanel无法转换为javax.swing.JLayeredPane,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27274418/