Frage

I created a window which contain a JMenuBar and I want to open another container when I click on a menu from my JMenuBar(by calling a new class, to separate each container by class). But I don't know how to do this.

Actually I reset the currently container and add items on it like that...

public void mousePressed(MouseEvent arg0) 
{
  if(arg0.getSource()==login)
  { 
    cont.removeAll();
    MenuLogin menu= new MenuLogin();
    cont.add(menu);
    cont.repaint();
    Window.this.setVisible(true);
  }
}

but it's not what I want

War es hilfreich?

Lösung 2

Here is some code I made for you so you can see how you do it.

public class MenuBarShow extends JFrame implements ActionListener{
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A menu");
JMenuItem item;
public MenuBarShow(){
    this.setVisible(true);
    this.setLayout(new BorderLayout());
    this.setSize(250,250);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    menu = new JMenu("A menu");
    item = new JMenuItem("An item");
    item.addActionListener(this);
    menuBar.add(menu);
    menu.add(item);
    this.add(menuBar, BorderLayout.NORTH);
    this.add(new JButton("Hello"), BorderLayout.CENTER);

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == item){
      //Create new JFrame when pressing the JMenuItem
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setLayout(new BorderLayout());
        frame.setSize(250, 250);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.setVisible(true);
        this.setVisible(false);
    }
}

public static void main(String []args){

    MenuBarShow mS = new MenuBarShow();

}

}

As you can see you just need to set the visible of the frame you have created to true and the previous frame visible to false. But there is a better way to handle it by using Cardlayout. You can find more about it here http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

Here is the same code using cardLayout.

public class MenuBarShow extends JFrame implements ActionListener{
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A menu");
JMenuItem item;
JPanel p = new JPanel(new CardLayout());
JPanel mainPanel = new JPanel(new BorderLayout());
CardLayout cl = new CardLayout();
JPanel showThisPanel = new JPanel();

public MenuBarShow(){
    this.setVisible(true);
    this.setLayout(new BorderLayout());
    this.setSize(250,250);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    mainPanel.add(new JButton(BorderLayout.CENTER));

    cl = (CardLayout)(p.getLayout());
    p.add(showThisPanel, "STP");
    p.add(mainPanel, "MP");
    cl.show(p, "MP");
    this.add(p);


    menu = new JMenu("A menu");
    item = new JMenuItem("An item");
    item.addActionListener(this);
    menuBar.add(menu);
    menu.add(item);
    this.add(menuBar, BorderLayout.NORTH);

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == item){
        cl.show(p, "STP");
    }
}

public static void main(String []args){

    MenuBarShow mS = new MenuBarShow();
}

Andere Tipps

"I want to open another container when I click on a menu from my JMenuBar"

  1. Don't use a MouseListener for a JMenu. Either use a MenuListener or just add a JMenuItem to the JMenu and use an ActionListener for it.

  2. When you want to remove and add components to a container, you need to revalidate() along with repaint(). revalidate first.

  3. Instead of removing all and adding a new panel, look into using a CardLayout, which will allow you to swap views. See more at How to Use CardLayout


See more at How to Use Menus

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top