我打破了我的代码,但我不知道如何。完全没有。
在某一时刻,编译和运行代码每次都会呈现一个窗口。然后我工作了大约 15 分钟,运行时框架不再出现。我尝试过撤消工作、添加运行方法、添加主要方法,甚至直接从我的教科书中复制和粘贴代码。没有任何效果。
我试图粘贴尽可能少的代码。如果我的代码草率,请原谅我;这是我第一次使用swing。谢谢!
public class LibraryFrame extends JFrame implements ActionListener
{
private JScrollPane studentScroller = new JScrollPane();
private JPanel addStudent = new JPanel();
private JTextField UID = new JTextField();
private JTextField name = new JTextField();
private JTextField email = new JTextField();
private JButton okButton = new JButton("OK");
private JButton closeButton = new JButton("Close");
private JPanel buttonPane = new JPanel();
public LibraryFrame()
{
setTitle("Student Enrollment");
setSize(500,200);
setLocationByPlatform(true);
addStudent.setLayout(new GridLayout(0, 2, 0, 0));
addStudent.add(new JLabel("Enter Student UID:"));
addStudent.add(UID);
addStudent.add(new JLabel("Enter Student Name:"));
addStudent.add(name);
addStudent.add(new JLabel("Enter Student Email:"));
addStudent.add(email);
addStudent.add(Box.createRigidArea(new Dimension(0, 5)));
addStudent.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
okButton.setActionCommand(null);
closeButton.addActionListener(new ActionListener() //implement window close only
{
@Override
public void actionPerformed(ActionEvent e)
{ dispose(); }
});
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
buttonPane.add(Box.createRigidArea(new Dimension(0, 0)));
buttonPane.add(okButton);
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPane.add(closeButton);
Container studentPane = getContentPane();
studentPane.add(addStudent, BorderLayout.CENTER);
studentPane.add(buttonPane, BorderLayout.PAGE_END);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}