【问题标题】:JMenuBar to JFrameJMenuBar 到 JFrame
【发布时间】:2014-03-06 17:42:58
【问题描述】:

当我单击菜单栏项时,我在 JFrame JF 中有一个菜单栏,会创建并显示一个新的 JFrame JF1,但是当我单击 JF1 的关闭按钮时,两个 JFrame 都已关闭。当我点击 JF1 的关闭按钮时,我只想关闭 JF1,而不是 JF

JMenuBar menubar;
JMenu help;
JMenuItem about;

public GUI() {
    setLayout(new FlowLayout());

    menubar = new JMenuBar();
    add(menubar);

    help = new JMenu("Help");
    menubar.add(help);  
}`

【问题讨论】:

    标签: java swing jframe multiple-instances jmenubar


    【解决方案1】:

    创建并显示一个新的 JFrame JF1

    不要创建新的 JFrame,应用程序应该只有一个 JFrame。

    改为创建JDialog。请参阅:The Use of Multiple JFrames: Good or Bad Practice? 了解更多信息。

    您也不要使用 add(...) 方法将 JMenuBar 添加到 JFrame。请参阅How to Use Menu Bars 了解更好的方法。

    【讨论】:

    • 你能给我举个例子吗
    【解决方案2】:

    我建议您使用 DesktopPane 和 JInternalFrame。

    您对主框架进行更改:contentPane (JPanel) 将是 JDesktopPane。

    单击时显示的 JFrame 将是 JInternalFrame。

    在 JMenuItem 的 actionListener 中,你会这样做:

    MyInternalFrame internalFrame = new MyInternalFrame();
    internalFrame.show();
    contentPane.add(internalFrame);
    

    MyInternalFrame 是显示的 Frame 的类(扩展 JInternalFrame 的类)。

    要关闭“internalFrame”,只需在布局中添加一个带有“Quit”文本的按钮,然后在其 actionListener 中放置“dispose()”。

    试试看它是否有效;)

    --编辑-- 主类(JFRAME)

    public class Main extends JFrame {
    
    private JDesktopPane contentPane;
    private JMenuBar menuBar;
    private JMenu mnExample;
    private JMenuItem mntmAbout;
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public Main() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(20, 20, 800, 800);
    
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);
    
        mnExample = new JMenu("Help");
        menuBar.add(mnExample);
    
        mntmAbout = new JMenuItem("About");
        mntmAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                About frame = new About();
                frame.show();
                contentPane.add(frame);
            }
        });
        mnExample.add(mntmAbout);
        contentPane = new JDesktopPane();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
    }
    }
    

    关于课程(JINTERNALFRAME)

    public class About extends JInternalFrame {
    
    public About() {
        setBounds(100, 100, 544, 372);
    
        JLabel lblSomeText = new JLabel("Some Text");
        getContentPane().add(lblSomeText, BorderLayout.CENTER);
    
        JButton btnClose = new JButton("Close");
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });
        getContentPane().add(btnClose, BorderLayout.SOUTH);
    
    }
    
    }
    

    【讨论】:

    • 你的意思是把 JInternalFrame 添加到 JFrame 中??
    • 到desktopPane,和JFrame里面的desktopPane,就像一个cintentPane
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2020-08-25
    • 2011-05-17
    相关资源
    最近更新 更多