十万火急求助,分多多地给。关于Swing中JCheckBox的问题

dnpyy 2009-09-04 02:36:22
一个panel中放了两个JCheckBox,第二个总能显示,第一个显示有问题,如果鼠标放在第一个上面,它就会显示出来,如果移开,它就隐藏了。请高手急救,谢谢!


代码如下:

import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

public class Test extends JDialog {

JPanel panel = new JPanel();
JCheckBox cbox = new JCheckBox();
JCheckBox cbox2 = new JCheckBox();
public Test(){
panel.setLayout(null);
panel.setBounds(new Rectangle(0, 203, 378, 162));
cbox.setText("第一个");
cbox.setBounds(new Rectangle(11, 73, 178, 18));
cbox2.setText("第二个");
cbox2.setBounds(new Rectangle(11, 73, 178,100));
panel.add(cbox,null);
panel.add(cbox2,null);
cbox.setVisible(true);
cbox2.setVisible(true);
this.getContentPane().add(panel,null);
}
public static void main(String[] args){
Test t = new Test();
t.setSize(new Dimension(500,500));
t.setVisible(true);
}
}
...全文
73 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dnpyy 2009-09-04
  • 打赏
  • 举报
回复
多谢了,搞糊涂了,把坐标当成矩形的长宽了,多谢多谢,嘿嘿!


[Quote=引用 8 楼 dz007 的回复:]
还有就是,第二个控件高度太高了
new Rectangle(int x,int y,int width,int height)
前两个是定位坐标,后两个是控件的宽、高

Java code
cbox.setText("第一个");
cbox.setBounds(new Rectangle(10,50,178,18));
cbox2.setText("第二个");
cbox2.setBounds(new Rectangle(10,70,178,18));
[/Quote]
dz007 2009-09-04
  • 打赏
  • 举报
回复
还有就是,第二个控件高度太高了
new Rectangle(int x,int y,int width,int height)
前两个是定位坐标,后两个是控件的宽、高


cbox.setText("第一个");
cbox.setBounds(new Rectangle(10, 50, 178, 18));
cbox2.setText("第二个");
cbox2.setBounds(new Rectangle(10, 70, 178, 18));
java_object 2009-09-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dz007 的回复:]
笨笨,你两个控件叠在一起啦,在11,73处重叠

cbox.setBounds(new Rectangle(11, 73, 178, 18));
cbox2.setBounds(new Rectangle(11, 73, 178,100));
[/Quote]

确实是,2个 坐标重叠了。呵呵
java_object 2009-09-04
  • 打赏
  • 举报
回复
很奇诡,你要不 不要用那个null 了,换成 某一种布局方式 如 下面代码:

public class Test extends JDialog
{

JPanel panel = new JPanel();

JCheckBox cbox = new JCheckBox();

JCheckBox cbox2 = new JCheckBox();

public Test()
{
panel.setLayout(new BorderLayout());
//panel.setBounds(new Rectangle(0, 203, 378, 162));
cbox.setText("第一个");
//cbox.setBounds(new Rectangle(11, 73, 178, 18));
cbox2.setText("第二个");
// cbox2.setBounds(new Rectangle(11, 73, 178, 100));
panel.add(cbox, BorderLayout.NORTH);
panel.add(cbox2, BorderLayout.SOUTH);
this.getContentPane().add(panel);
}

public static void main(String[] args)
{
Test t = new Test();
t.setSize(new Dimension(500, 500));
t.setVisible(true);
}
}
dz007 2009-09-04
  • 打赏
  • 举报
回复
笨笨,你两个控件叠在一起啦,在11,73处重叠

cbox.setBounds(new Rectangle(11, 73, 178, 18));
cbox2.setBounds(new Rectangle(11, 73, 178,100));
healer_kx 2009-09-04
  • 打赏
  • 举报
回复
MARK
yangyanan1987 2009-09-04
  • 打赏
  • 举报
回复
行不通
[Quote=引用 1 楼 laorer 的回复:]
this.getContentPane().add(panel,null);

后面 加个 repaint()或者 refresh()
[/Quote]
  • 打赏
  • 举报
回复
repaint()通常是在component变化的时候让系统调用来重画这个component和这个component所包含的child components。通常repaint就是调用paint()或paintComponent()
laorer 2009-09-04
  • 打赏
  • 举报
回复
this.getContentPane().add(panel,null);

后面 加个 repaint()或者 refresh()

62,616

社区成员

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

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