Panel不能设置大小

lingdushanke 2009-07-27 11:01:22
大家帮帮忙,我设置了Frame是400*400,里边Panel大小是100*100。但是在命令行输出的Panel大小是[width=392,height=366],明显是Panel把整个Frame都覆盖了,同样对位置的设置也是无效。

帮我看看问题出在哪儿了,代码如下


import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;



public class test extends Frame{

public static void main(String[] arge){

new teFrame();
}
}


class teFrame extends Frame{
teFrame(){
this.setBounds(400, 200, 400, 400);
this.add(new tePanel());

this.setVisible(true);
}
}

class tePanel extends Panel{

tePanel(){
this.setBounds(10, 10, 100, 100);
}

public void paint(Graphics g) {
g.setColor(Color.red);
g.drawLine(0, 0, 200, 200);

System.out.println(this.getSize().toString());
}
}

...全文
634 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
绿豆面咕糕 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shadow55 的回复:]
setBounds是针对一个样式的,你先将Frame的布局设置为

Java code
setLayout(null)
;这时再去

Java code
this.setBounds(10, 10, 100, 100);

就对了
[/Quote]

非常感谢啊
lingdushanke 2009-07-27
  • 打赏
  • 举报
回复
已经好了,谢谢~
shadow55 2009-07-27
  • 打赏
  • 举报
回复
setBounds是针对一个样式的,你先将Frame的布局设置为
setLayout(null)
;这时再去
this.setBounds(10, 10, 100, 100);
就对了

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧