java - JLabel 覆盖 JMenuBar

标签 java swing

aboutWindow 框架打开时,只有标签(-contents)name 可见,菜单栏不见了。当标签被注释掉时,栏出现,但它填满了整个框架。我错过了什么?

import javax.swing.*;
import java.net.*;
import java.awt.event.*;

public class Main
{

    public static JDialog aboutWindow;

    public static void about () {
    //JDialog aboutWindow = new JDialog();
    Main.aboutWindow = new JDialog();
    Main.aboutWindow.setTitle("About kingfisher a0.0.3");
    Main.aboutWindow.setSize(300,600);
    Main.aboutWindow.setModal(true);
        JMenuBar menubar = new JMenuBar();
            JMenu control = new JMenu("Control");
                JMenuItem quit = new JMenuItem("quit");
                    quit.addActionListener(new ActionListene121r() {
                        public void actionPerformed(ActionEvent event) {
                            Main.aboutWindow.setVisible(false);
                            }
                        }
                    );
                control.add(quit);
            menubar.add(control);
        Main.aboutWindow.add(menubar);
    JLabel name = new JLabel("kingfisher");
    Main.aboutWindow.add(name);
    Main.aboutWindow.setVisible(false);
    }


    public static void main (String args[]){
    //Defined window dimensions
    JFrame controlpanel = new JFrame();
    controlpanel.setTitle("kingfisher Control Panel");
    controlpanel.setSize(500,400);
    controlpanel.add(new JLabel("The angels have the blue box"));
        JMenuBar menubar = new JMenuBar();
        //defining menu groups
            JMenu windows = new JMenu("Windows");
                JCheckBoxMenuItem chat = new JCheckBoxMenuItem("Chat");
                JCheckBoxMenuItem filetransfer = new JCheckBoxMenuItem("Filetransfer");
                JCheckBoxMenuItem settings = new JCheckBoxMenuItem("Settings");
                windows.add(chat);
                windows.add(filetransfer);
                windows.add(settings);
            JMenu control = new JMenu("Control");
                JMenuItem quit = new JMenuItem("Quit");
                quit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        System.exit(0);
                        }
                    }
                );
                control.add(quit);
            JMenu help = new JMenu("Help");
                JMenuItem support = new JMenuItem("Support");
                JMenuItem about = new JMenuItem("About");
                    about.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                            Main.aboutWindow.setVisible(true);
                            }
                        }
                    );
                help.add(support);
                help.add(about);
            menubar.add(control);
            menubar.add(windows);
            menubar.add(help);
        controlpanel.setJMenuBar(menubar);
    controlpanel.setVisible(true);
    };
};

最佳答案

不添加 JMenuBar,而是使用 JDialog.setJMenuBar

Main.aboutWindow = new JDialog();
    Main.aboutWindow.setTitle("About kingfisher a0.0.3");
    Main.aboutWindow.setSize(300,600);
    Main.aboutWindow.setModal(true);
        JMenuBar menubar = new JMenuBar();
            JMenu control = new JMenu("Control");
                JMenuItem quit = new JMenuItem("quit");
                    quit.addActionListener(new ActionListene121r() {
                        public void actionPerformed(ActionEvent event) {
                            Main.aboutWindow.setVisible(false);
                            }
                        }
                    );
                control.add(quit);
            menubar.add(control);

        Main.aboutWindow.setJMenuBar(menubar); //changed

    JLabel name = new JLabel("kingfisher");
    Main.aboutWindow.add(name);
    Main.aboutWindow.setVisible(false);

关于java - JLabel 覆盖 JMenuBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39125936/

相关文章:

java - 返回字符串数组

Java networkinterface.isVirtual() 方法总是返回值是 false?

java - Oracle Java 代码约定

eclipse - 在 Eclipse 中定位 JRE

java - 动态添加组件并调用repaint/validate/revalidate

java - 在java中使用keyAdapter和keyEvent时出错

java - 为什么构造函数不被继承?

java - 在 JTextArea 中键入时,JLabel 中的文本会移动

java - 如何在 JPanel 中显示图像

java - 在JFrame中显示移动 block