我正在研究一个简单的 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);
}
}
你的问题是你在实例上调用setJMenuBar
JPanel
这是错误的,你应该调用:
frame.setJMenuBar(myMenuBar);
也不需要:
frame.setLayout(new BorderLayout());
作为JFrame
内容窗格布局默认为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);
}
});
不要忘记注释掉MainPane
类中的setJmenuBar(..)
:
public MainPane() {
myMenuBar = new JMenuBar();
//notice no call to setJMenuBar
myFileMenu = new JMenu("File");
myRegisterItem = new JMenuItem("Register");
myMenuBar.add(myFileMenu);
...
}
这是因为您尚未设置您的JMenuBar可见。
将其添加到您的代码中
frame.setJMenuBar(myMenuBar); // This line to be added
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);