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

时间:2014-03-12 02:17:57

标签: java swing

我正在尝试在调用类时显示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");
       }

  }
  }

这是我的班级:

    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)
我正走在正确的道路上吗?我的意思是我有一个运行时错误,但我关闭了

2 个答案:

答案 0 :(得分:2)

您不明白该例外情况怎么办?

java.lang.IllegalArgumentException: adding a window to a container

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

您正在尝试将JFrame添加到JPanel。那不会奏效。您还试图将面板添加到标签中。这就是你真正想要的。好像你有事情倒退了。您可能希望将标签添加到面板,将面板添加到框架中。

答案 1 :(得分:2)

  

线程“AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:向容器添加窗口

错误消息告诉您究竟出了什么问题。您尝试将某种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);
    }
}

我不完全确定你想要实现的目标,但我的第一个问题是将panel添加到parent

parent.add(panel);

但是根本没有足够的背景来确定。最好使用某种对话框。