这是我的主要:
import javax.swing.*;
import java.awt.event.*;
public class GUI {
public static void main(String[] args){
JFrame cF = new JFrame("HANGMAN");
cF.setContentPane(new Hangman());
// cF.setSize(700, 600);
cF.pack();
cF.setLocationRelativeTo(null);
cF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cF.setVisible(true);
}
}
我想在这个子类中创建 JMenuBar,但是当我编译并运行时,菜单没有被创建。如何在主节点的 JFrame 上创建菜单?相当菜鸟,我不知道这是如何工作的,所以如果有人可以帮助我,那就太好了。
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
public class Hangman extends JPanel {
JTextField guessesField, missField, statusField, guessField;
JPanel guessPanel, missPanel, statusPanel;
int gu = 0 ;
public Hangman(){
setLayout(new GridLayout(4, 1, 10, 15));
/*--Guesses Left--*/
JPanel guessPanel = new JPanel();
JLabel guessLabel = new JLabel("Guesses Left: " + gu);
//JTextField guessField = new JTextField(1);
//guessField.setEditable(false);
//guessField.setText("");
guessPanel.add(guessLabel);
//guessPanel.add(guessField);
add(guessPanel);
/*--Current Status---*/
JPanel statusPanel = new JPanel();
JLabel statusLabel = new JLabel("Current Status:");
statusPanel.add(statusLabel);
statusField = new JTextField(10);
statusField.setText("_ _ _ _ _ _ _ _");
statusField.setEditable(false);
add(statusPanel);
statusPanel.add(statusField);
/*--Guess---*/
JPanel guessesPanel = new JPanel();
JLabel guessesLabel = new JLabel("Guess:" );
guessesPanel.add(guessesLabel);
guessesField = new JTextField(1);
guessesPanel.add(guessesField);
add(guessesPanel);
/*--Misses---*/
JPanel missPanel = new JPanel();
JLabel missLabel = new JLabel("Misses:");
missField = new JTextField(10);
missField.setEditable(false);
missField.setText("_ _ _ _ _ _ _ _");
missPanel.add(missLabel);
missPanel.add(missField);
add(missPanel);
}
public void menubar(JFrame cF){
/*--Menu--*/
JMenuBar menubar = new JMenuBar();
cF.setJMenuBar(menubar);
JMenu start = new JMenu("Start");
menubar.add(start);
JMenuItem startg = new JMenuItem("Start");
JMenuItem exit = new JMenuItem("Exit");
start.add(startg);
start.add(exit);
JMenu options = new JMenu("Options");
menubar.add(options);
JRadioButtonMenuItem hard = new JRadioButtonMenuItem("Hard");
JRadioButtonMenuItem medium = new JRadioButtonMenuItem("Medium");
JRadioButtonMenuItem easy = new JRadioButtonMenuItem("Easy");
ButtonGroup bg = new ButtonGroup();
bg.add(hard);
bg.add(medium);
bg.add(easy);
options.add(hard);
options.add(medium);
options.add(easy);
/*--AllActionListeners--*/
startg.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
hard.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
medium.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
easy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
}
}
您已经创建了menubar(JFrame cF)
方法,但没有在任何地方使用它。
试试这样
JFrame cF = new JFrame("HANGMAN");
Hangman h = new Hangman();
cF.setContentPane(h);
h.menubar(cF);
// cF.setSize(700, 600);
cF.pack();
cF.setLocationRelativeTo(null);
cF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cF.setVisible(true);