所以我正在创建一个swing 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.
}
}
});
}
}
这就是我所看到的,这是预期的:
答案 0 :(得分:0)
从命令行运行代码并在那里看不到任何错误消息后,我重新启动了Eclipse,这就是诀窍。因此Eclipse需要重新启动。