[問題] JPanel的問題
Java 新手
在看 head first 的時候 無聊嘗試做一些改寫
結果跑出來的結果出乎意外
不太了解
以下是code...
import java.awt.*;
import javax.swing.*;
class MyDrawPanel extends JPanel {
public void paintComponent (Graphics g){
g.setColor(Color.green);
g.fillRect(20,50,100,100);
}
}
class MyDrawPanel2 extends JPanel {
public void paintComponent (Graphics g){
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
}
public class MyDrawPanelTest {
public static void main (String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel panel = new MyDrawPanel();
MyDrawPanel2 panel2 = new MyDrawPanel2();
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.getContentPane().add(BorderLayout.EAST, panel2);
frame.setSize(600,600);
frame.setVisible(true);
}
}
結果只看到 panel 的綠色而已
MyDrawPanel panel = new MyDrawPanel(); 把這行註解
會變成一片空白
可以請問為什麼會這樣嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.166.68.114
推
09/03 13:19, , 1F
09/03 13:19, 1F
→
09/03 13:20, , 2F
09/03 13:20, 2F
→
09/03 13:22, , 3F
09/03 13:22, 3F
→
09/03 13:23, , 4F
09/03 13:23, 4F