简体   繁体   English

如何使用ActionListener将JPanel插入JFrame中?

[英]How do I insert a JPanel inside a JFrame with an ActionListener?

package database;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.SwingUtilities;

public class Application {
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new ApplicationFrame("Application");
                frame.setSize(500, 400);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });

    }

    public class ApplicationFrame extends JFrame {

        private CompaniesPanel companiesPanel;

        public ApplicationFrame(String title) {
            super(title);

            setLayout(new BorderLayout());

            JMenuBar menuBar = new MenuBar("Menu");

            Container container = getContentPane();
            container.add(menuBar, BorderLayout.NORTH);

        }
    }

    public class MenuBar extends JMenuBar {

        public MenuBar(String title) {
            super();

            JMenu menuFile = new JMenu("File");
            add(menuFile);

            JMenu menuOpen = new JMenu("Open");
            menuFile.add(menuOpen);

            JMenuItem menuItemCompanies = new JMenuItem("Companies");
            menuOpen.add(menuItemCompanies);

            menuItemCompanies.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // In here I would like to take a CompaniesPanel and insert
                    // it into
                    // an ApplicationFrame if the Companies button is pressed on
                    // menu
                }

            });
        }
    }

    public class CompaniesPanel extends JPanel {
        public CompaniesPanel() {
            Dimension size = getPreferredSize();
            size.width = 250;
            setPreferredSize(size);

            setBorder(BorderFactory.createTitledBorder("Company Names"));

            setLayout(new GridBagLayout());

            GridBagConstraints gridBagConstraints = new GridBagConstraints();

        }
    }
}

I just want my application to open up that menu with the rest being blank, and open up the CompaniesPanel when Companies is pressed from the drop down menu. 我只希望我的应用程序打开其余菜单为空白的菜单,并在从下拉菜单中按下“公司”时打开“公司面板”。 Is this possible without opening up another jFrame? 不打开另一个jFrame就可以吗?

container.add(menuBar, BorderLayout.NORTH);

Frist of all that is not how you add a menu to the frame. 第一,这不是您如何向框架添加菜单的方法。 A frame has a special area reserved for the menu bar. 框架具有为菜单栏保留的特殊区域。

Instead you just use: 相反,您只使用:

setJMenuBar( menuBar );

Read the section from the Swing tutorial on How to Use Menus for more information and working examples. 阅读Swing教程中有关如何使用菜单的部分, 获取更多信息和工作示例。

As already mentioned a CardLayout would be a good choice. 如前所述, CardLayout将是一个不错的选择。 By adding two panels (one empty, and one for the companines) the preferred size of the layout will be determined by your companies panel so the frame size does not change when you display the companies panel. 通过添加两个面板(一个为空,另一个为陪伴),布局的首选大小将由公司面板确定,因此在显示公司面板时框架的大小不会改变。

The Swing tutorial also has a section on How so Use CardLayout with working examples to get your started. Swing教程中还有一个章节,介绍如何使用CardLayout以及一些示例,以帮助您入门。

Keep a reference to the Swing tutorial handy for all the Swing basics. 有关所有Swing基础知识,请随时参考Swing教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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