2

我正在研究一个简单的 gui。

我似乎无法找出我的JMenuBar未出现的原因。我错过了什么?

这是下面的代码。

    myMenuBar = new JMenuBar(); 
    myFileMenu = new JMenu("File"); 
    myRegisterItem = new JMenuItem("Register");
    myMenuBar.add(myFileMenu);
    myFileMenu.add(myRegisterItem);
    setJMenuBar(myMenuBar);

全班:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.rmi.Naming;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class GUI extends JFrame{


private JTextArea recievedField;
private JButton sendButton;
private JTextField messageField; 
private JComboBox itemComboBox;
private JButton connectButton;
private JTextField userNameField;
private JPasswordField passwordField;
private JButton loginButton;
private JMenuBar myMenuBar;
private JMenu myFileMenu;
private JMenuItem myRegisterItem;


public static void main(String[] args) {
    new GUI();
}

public GUI() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                } 

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new MainPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    });
}

public class MainPane extends JPanel {

    public MainPane() {


        myMenuBar = new JMenuBar(); 
        setJMenuBar(myMenuBar); 
        myFileMenu = new JMenu("File"); 
        myRegisterItem = new JMenuItem("Register");
        myMenuBar.add(myFileMenu);
        myFileMenu.add(myRegisterItem);


        setBorder(new EmptyBorder(4, 4, 4, 4));
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(new LoginPane(), gbc);
        gbc.gridy++;
        add(new ConnectPane(), gbc);
        gbc.gridy++;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(new JScrollPane(recievedField = new JTextArea(5, 20)), gbc);

        gbc.gridwidth = 1;

        messageField = new JTextField(10);
        sendButton = new JButton("Send");

        gbc.gridy++;
        gbc.weighty = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(messageField, gbc);
        gbc.gridx++;
        gbc.weightx = 0;
        gbc.insets = new Insets(5,5,5,5);
        add(sendButton, gbc);          



    }

}

public class ConnectPane extends JPanel {

    public ConnectPane() {

        itemComboBox = new JComboBox();
        itemComboBox.addItem("Select an Item");
        connectButton = new JButton("Connect");

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5,5,5,5);
        add(itemComboBox, gbc);

        gbc.gridx++;
        gbc.weightx = 1;
        gbc.insets = new Insets(5,5,5,5);
        add(connectButton, gbc);
    }

}

public class LoginPane extends JPanel {

    public LoginPane() {

        userNameField = new JTextField(10);
        passwordField = new JPasswordField(10);
        loginButton = new JButton("Login");

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5,5,5,5);
        add(new JLabel("User name:"), gbc);

        gbc.gridx++;
        gbc.insets = new Insets(5,5,5,5);
        add(userNameField, gbc);

        gbc.gridx++;
        gbc.insets = new Insets(5,5,5,5);
        add(new JLabel("Password:"), gbc);

        gbc.gridx++;
        gbc.insets = new Insets(5,5,5,5);
        add(passwordField, gbc);

        gbc.gridx++;
        gbc.weightx = 1;
        add(loginButton, gbc);

    }

}
4

2 回答 2

3

您的问题是您调用setJMenuBar实例JPanel这是错误的,您应该调用:

frame.setJMenuBar(myMenuBar);

也不需要:

frame.setLayout(new BorderLayout());

因为JFrame contentPane布局默认是BorderLayout

即将您的代码更改为:

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new MainPane());
            frame.setJMenuBar(myMenuBar);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

也不要使用EventQueue相当SwingUtilities

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new MainPane());
            frame.setJMenuBar(myMenuBar);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });

setJmenuBar(..)不要忘记在你的MainPane课堂上注释掉:

    public MainPane() {
        myMenuBar = new JMenuBar();

        //notice no call to setJMenuBar

        myFileMenu = new JMenu("File");
        myRegisterItem = new JMenuItem("Register");
        myMenuBar.add(myFileMenu);

        ...
     }
于 2012-11-22T15:18:59.477 回答
0

这是因为你没有设置你的 JMenuBar 可见。

将此添加到您的代码中

    frame.setJMenuBar(myMenuBar); // This line to be added
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
于 2012-11-22T15:11:10.803 回答