Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过单击JMenuBar打开新容器?_Java_Swing_Jmenu_Jmenubar - Fatal编程技术网

Java 通过单击JMenuBar打开新容器?

Java 通过单击JMenuBar打开新容器?,java,swing,jmenu,jmenubar,Java,Swing,Jmenu,Jmenubar,我创建了一个包含JMenuBar的窗口,当我单击JMenuBar菜单时,我想打开另一个容器(通过调用一个新类,按类分隔每个容器)。但我不知道怎么做 实际上,我重置了当前的容器,并像这样在其上添加项目 public void mousePressed(MouseEvent arg0) { if(arg0.getSource()==login) { cont.removeAll(); MenuLogin menu= new MenuLogin(); cont.ad

我创建了一个包含
JMenuBar
的窗口,当我单击
JMenuBar
菜单时,我想打开另一个容器(通过调用一个新类,按类分隔每个容器)。但我不知道怎么做

实际上,我重置了当前的容器,并像这样在其上添加项目

public void mousePressed(MouseEvent arg0) 
{
  if(arg0.getSource()==login)
  { 
    cont.removeAll();
    MenuLogin menu= new MenuLogin();
    cont.add(menu);
    cont.repaint();
    Window.this.setVisible(true);
  }
}
但这不是我想要的

“当我从JMenuBar中单击菜单时,我想打开另一个容器”

  • 不要对
    JMenu
    使用
    MouseListener
    。使用或仅将
    JMenuItem
    添加到
    JMenu
    中,并使用
    ActionListener
    对其执行操作

  • 当您要删除组件并将其添加到容器中时,需要
    revalidate()
    以及
    repaint()
    。首先重新验证

  • 与其删除所有面板并添加新面板,不如考虑使用
    CardLayout
    ,这将允许您交换视图。更多信息请访问


  • 更多信息请访问

    “当我从JMenuBar中单击菜单时,我想打开另一个容器”

  • 不要对
    JMenu
    使用
    MouseListener
    。使用或仅将
    JMenuItem
    添加到
    JMenu
    中,并使用
    ActionListener
    对其执行操作

  • 当您要删除组件并将其添加到容器中时,需要
    revalidate()
    以及
    repaint()
    。首先重新验证

  • 与其删除所有面板并添加新面板,不如考虑使用
    CardLayout
    ,这将允许您交换视图。更多信息请访问


  • 更多信息请访问

    “当我从JMenuBar中单击菜单时,我想打开另一个容器”

  • 不要对
    JMenu
    使用
    MouseListener
    。使用或仅将
    JMenuItem
    添加到
    JMenu
    中,并使用
    ActionListener
    对其执行操作

  • 当您要删除组件并将其添加到容器中时,需要
    revalidate()
    以及
    repaint()
    。首先重新验证

  • 与其删除所有面板并添加新面板,不如考虑使用
    CardLayout
    ,这将允许您交换视图。更多信息请访问


  • 更多信息请访问

    “当我从JMenuBar中单击菜单时,我想打开另一个容器”

  • 不要对
    JMenu
    使用
    MouseListener
    。使用或仅将
    JMenuItem
    添加到
    JMenu
    中,并使用
    ActionListener
    对其执行操作

  • 当您要删除组件并将其添加到容器中时,需要
    revalidate()
    以及
    repaint()
    。首先重新验证

  • 与其删除所有面板并添加新面板,不如考虑使用
    CardLayout
    ,这将允许您交换视图。更多信息请访问



  • 更多信息请参见

    以下是我为您编写的一些代码,您可以查看如何执行

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

    正如您所看到的,您只需要将已创建的帧的可见设置为true,将上一帧的可见设置为false。但是有一种更好的方法可以通过使用Cardlayout来处理它。你可以在这里找到更多关于它的信息

    下面是使用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();
    }
    

    下面是我为您编写的一些代码,您可以查看如何执行

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

    正如您所看到的,您只需要将已创建的帧的可见设置为true,将上一帧的可见设置为false。但是有一种更好的方法可以通过使用Cardlayout来处理它。你可以在这里找到更多关于它的信息

    下面是使用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();
    }
    

    下面是我为您编写的一些代码,您可以查看如何执行

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

    正如您所看到的,您只需要将已创建的帧的可见设置为true,将上一帧的可见设置为false。但是有一种更好的方法可以通过使用Cardlayout来处理它。你可以在这里找到更多关于它的信息

    下面是使用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();
    }
    

    下面是我为您编写的一些代码,您可以查看如何执行

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

    正如您所看到的,您只需要将已创建的帧的可见设置为true,将上一帧的可见设置为false。但是有一种更好的方法可以通过使用Cardlayout来处理它。你可以在这里找到更多关于它的信息

    下面是使用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();
    }