這是我在框架上繪製矩形的簡單代碼。如何將按鈕添加到此框架?我試圖設置FlowLayout,但矩形不可見,請幫助。將JButton和矩形添加到JFrame
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);
}
}
你看到了什麼?你指的是哪一個矩形? – mattbdean
您是否嘗試過在「測試?」上設置preferredSize? – resueman
您是否嘗試過在'paint'方法中調用'super.paint(graphics)'? – martijno