问题描述
所以我正在创建一个摆动 GUI。 运行代码后我希望看到的窗口出现了,一切都运行得很完美,除了在我的窗口弹出之前控制台上突然出现了一堆错误,然后这些错误就像它们出现的一样突然消失了。 我无法确定这些“错误”是什么,因为它们出现的时间不到一秒钟左右。 我也不知道我是否应该担心,因为我的代码可以正常工作。
我将在此处添加代码,以防有人需要它进行诊断。
我的 JPanel 类:(注意:ActionListener 和 ItemListener 方法会在后面补上)
public class BoardPanel extends JPanel implements ActionListener, ItemListener{
private JMenu m2difficultyMenu, m1boardSizeMenu;
private JMenuBar menuBar;
private JRadioButtonMenuItem m1r1, m1r2, m2r1, m2r2, m2r3;
private JCheckBox cb1;
private JButton startButton;
private ButtonGroup m1Group, m2Group;
private JLabel label;
private JPanel p1, p2, p3;
public BoardPanel(){
setLayout(new BorderLayout());
p1=new JPanel();
//p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
p2=new JPanel();
//p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
p3=new JPanel();
p3.setBorder(BorderFactory.createEmptyBorder(100, 0, 0, 0));
p1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 100));
p2.setBorder(BorderFactory.createEmptyBorder(0, 100, 0, 0));
//creating menus
m2difficultyMenu=new JMenu("Stufe");
m1boardSizeMenu=new JMenu("Groesse");
//creating menu bar
menuBar=new JMenuBar();
//creating buttons, radio buttons, and check boxes
label=new JLabel("Choose your settings then hit play. Good luck!");
m1r1=new JRadioButtonMenuItem("3x3");
m1r2=new JRadioButtonMenuItem("4x4");
m2r1=new JRadioButtonMenuItem("leicht");
m2r2=new JRadioButtonMenuItem("mittel");
m2r3=new JRadioButtonMenuItem("Unschlagbar");
cb1=new JCheckBox("Hinweise");
startButton=new JButton("Spielen!");
startButton.setBackground(Color.RED);
startButton.setForeground(Color.white);
//setting borders
label.setBorder(BorderFactory.createEmptyBorder(200,125,100,0));
//p1.add(Box.createHorizontalStrut(100));
//p2.add(Box.createHorizontalStrut(100));
//creating button groups
m1Group=new ButtonGroup();
m2Group=new ButtonGroup();
//Assigning buttons to groups
m1Group.add(m1r1);
m1Group.add(m1r2);
m2Group.add(m2r1);
m2Group.add(m2r2);
m2Group.add(m2r3);
//Adding menus to the menu bar
menuBar.add(m2difficultyMenu);
menuBar.add(m1boardSizeMenu);
menuBar.add(cb1);
//menuBar.add(Box.createHorizontalGlue());
//Adding buttons to difficulty menu
m2difficultyMenu.add(m2r1);
m2difficultyMenu.add(m2r2);
m2difficultyMenu.add(m2r3);
//Adding buttons to size menu
m1boardSizeMenu.add(m1r1);
m1boardSizeMenu.add(m1r2);
//Adding components to panels
add(p1, BorderLayout.LINE_START);
add(p2, BorderLayout.LINE_END);
add(p3, BorderLayout.PAGE_END);
add(label, BorderLayout.PAGE_START);
add(startButton, BorderLayout.CENTER);
startButton.setFont(new Font("Arial", 1, 40));
setPreferredSize(new Dimension(500,500));
}
/**
*
* @return Returns the menu bar for use in the JFrame class
*/
public JMenuBar getBar(){
return menuBar;
}
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
JFrame 类:
public class BoardFrame extends JFrame {
private BoardPanel panel;
public BoardFrame() {
panel=new BoardPanel();
setJMenuBar(panel.getBar());
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(panel);
setTitle("TicTacToe");
setResizable(false);
pack();
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
new BoardFrame();
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
}
});
}
}
这是我所看到的,正如预期的那样:
1楼
从命令行运行代码并在那里看到没有错误消息后,我重新启动了 Eclipse 并且成功了。 所以Eclipse只需要重新启动。