我有这个 GUI 类:
import java.awt.*;
import javax.swing.*;
public class Exp2 extends JFrame {
public Exp2 () {
setLayout(new FlowLayout());
setSize(360, 360);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
add(panel2);
add(panel1);
panel1.paint(null);
JButton button1 = new JButton("Run");
panel2.add(button1, BorderLayout.PAGE_END);
}
public void paint(Graphics g) {
g.setColor(Color.green);
g.fillRect(50, 50, 20, 20);
}
}
连同这个主要课程:
import javax.swing.JFrame;
class Exp1 extends JFrame {
public static void main(String[] args) {
Exp2 box = new Exp2();
}
}
但是 JButtonbutton1
仅在我将鼠标移到它应该在的位置后才会出现。我究竟做错了什么?