我破坏了我的代码,但我无法弄清楚如何。完全没有。
有一次,编译和运行代码每次都会渲染一个窗口。然后我工作了大约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);
}
}
答案 0 :(得分:3)
this
对象?如果它没有添加到GUI,它将不会显示。