作者cckk3333 (皓月)
看板java
标题[问题] JPanel的问题
时间Sat Sep 3 02:50:52 2011
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
1F:推 yanshencun:你可以利用 09/03 13:19
2F:→ yanshencun:System.err.print("1: "+panel.getBounds()+"\n");来看 09/03 13:20
3F:→ yanshencun:panel2的width只有10 所以看不到 09/03 13:22
4F:→ yanshencun:解panel2.setPreferredSize(new Dimension(120, 120)); 09/03 13:23