这是我在框架上绘制矩形的简单代码。如何向该框架添加按钮?我试图设置 FlowLayout 但矩形不可见。请帮助。
import java.awt.*;
import javax.swing.*;
public class test extends Canvas{
public static JFrame frame;
public static JButton button;
public void paint(Graphics graphics) {
graphics.setColor(Color.yellow);
graphics.fillRect(10, 10, 100, 100);
graphics.setColor(Color.red);
graphics.drawRect(10, 10, 100, 100);
}
public static void main(String args[]){
test x=new test();
frame=new JFrame();
button=new JButton();
button.setSize(20,20);
button.setText("Click");
frame.setSize(500,500);
frame.add(button);
frame.add(x);
frame.setVisible(true);
}
}