gpt4 book ai didi

java - SWING 组件添加到不同类的框架中

转载 作者:行者123 更新时间:2023-12-01 18:35:09 25 4
gpt4 key购买 nike

我试图在调用类时使 GUI 组件出现。例如,如果我单击一个菜单项,它将打开一个面板,其中填充了该类的组件。

这是我的代码:(只是 if 语句的第一部分...其他带有 printfs 的概念是相同的概念,只是不同的类。

    private class FileMenuAction implements ActionListener{
//overrides the defualt action.
//then deals with the event from the action listener
@Override
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("Add Airport")){

AddAirport airport1 = new AddAirport(frame); <----creating an object of my class
}
else if (e.getActionCommand() == "Add Airline"){
System.out.println("Add Airline Clicked");

}
else if (e.getActionCommand() == "Add Flight"){
System.out.println("Add Flight Clicked");
}
else if (e.getActionCommand().equals("Exit")){
System.exit(0);
}
}

}

private class BookMenuAction implements ActionListener {

//overrides the defualt action.
//then deals with the event from the action listener
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Flight Reservation")){
System.out.println("Flight Reservation Clicked");
}

}
}

这是我的类(class):

    public class AddAirport {

public AddAirport(JFrame parent){
initcomp(parent);

}
public void initcomp(JFrame parent){
JPanel panel = new JPanel();
JLabel label1 = new JLabel("hekllo");
label1.add(panel);
panel.add(parent);


}
}

这是错误:

 Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:483)
at java.awt.Container.addImpl(Container.java:1084)
at java.awt.Container.add(Container.java:410)
at UserInterface.AddAirport.initcomp(AddAirport.java:23)
at UserInterface.AddAirport.<init>(AddAirport.java:16)
at UserInterface.AirlineReservation$FileMenuAction.actionPerformed(AirlineReservation.java:109)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 4 seconds)

我走的路正确吗?我的意思是我有一个运行时错误,但我关闭了

最佳答案

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container

错误消息准确地告诉您出了什么问题。您尝试将某种Window添加到某种Container

如果您仔细查看您的 AddAirport 类,您会发现问题...

public class AddAirport {
// Parent is a frame...
public AddAirport(JFrame parent){
initcomp(parent);
}

public void initcomp(JFrame parent){
JPanel panel = new JPanel();
JLabel label1 = new JLabel("hekllo");
label1.add(panel);
// Attempt to add the parent frame to the JPanel
// Can't be done...
panel.add(parent);
}
}

我不完全确定你想要实现什么,但我的第一个想法是将它翻转一些,将面板添加到父级

parent.add(panel);

但根本没有足够的背景来确定。使用某种对话框可能会更好。

关于java - SWING 组件添加到不同类的框架中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22340699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com