我正在研究一个简单的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);
}
}
答案 0 :(得分: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);
...
}
答案 1 :(得分:0)
因为你没有设置你的JMenuBar可见。
将此添加到您的代码中
frame.setJMenuBar(myMenuBar); // This line to be added
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);