1
void setUpGUI() {
JFrame frame = new JFrame();
frame.setContentPane(new Board());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
class Board extends JPanel {
@Override
public void paintComponent(Graphics g) {
setLayout(new GridLayout(10, 9));
JButton b = new JButton("hello");
add(b);
g.setColor(Color.orange);
g.fillRect(20, 50, 100, 100);
}
}
由於某些原因,代碼只顯示矩形而不是JButton。我究竟做錯了什麼?謝謝!JPanel只顯示矩形而不是JButton
*「我在做什麼錯了?」*在'paintComponent'方法內添加/修改組件 – MadProgrammer