1.为什么将两个JComponents放到同一个Bounds
panel1.setLocation(10, 10);
button1.setLocation(10, 10);2.看看Initials Thread
3.public class MyUI extends JFrame {
应该
public class MyUI extends JFrame implements ActionListener{4.不要扩展JFrame,创建一个局部变量
5.setVisible(true);应该(在这种形式下)只有最后一行代码行进入MyUI()构造函数
6.setVisible(true);是重要问题,您可以看到JFrame然后添加JComponent(s)
7.不要使用NullLayout,使用正确的LayoutManager,如果您删除this.setLayout(null);并且panel1.setLayout(null);已添加,则JComponents可见
8.在setVisible(true)之前使用pack()作为构造函数中的最后两行代码行
编辑(使用built_in LayoutManagers,BorderLayout用于JFrame,FlowLayout用于JPanel)
import java.awt.event.*;
import javax.swing.*;
public class MyUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton button1 = new JButton("Click");
private JTextField tb1 = new JTextField(5);
private JPanel panel1 = new JPanel();
public MyUI() {
super("Test");
panel1.add(tb1);
panel1.add(button1);
add(panel1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
button1.addActionListener(this);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MyUI testing = new MyUI();
}
});
}
}