简体   繁体   中英

SWING components added to frame from different class

I am trying to make GUI components appear when I call a class. For example, If I click on a menu item, it opens a panel filled with the components to that class.

here is my code:(just the first portion of the if statement...the others with printfs are the same concept just different classes.

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

  }
  }

and here is my 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);


}
}

and here is the error:

 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)

am I on the right path? I mean I have a runtime error, but I am I close

What about the exception don't you understand?

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

You're trying to add a JFrame to a JPanel . That won't work. Also you're your trying to add the panel to the label. Is that what you really want. Seem like you have things backwards. You probably want to add the label to the panel and the panel to the frame.

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

The error message is telling you exactly what's wrong. You attempting to add some kind of Window to some kind of Container .

If you take a closer look at your AddAirport class you can see the issue...

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

I'm not entirely sure what you want to achieve, but my first though is to flip it some you add the panel to the parent

parent.add(panel);

But there's simply not enough context to be sure. It might be better to use a dialog of some kind instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM