java小窗打不开_JAVA做个小窗口时候为什么用SetLayout(null)时候窗口里什么东西都不显示?...

JAVA做个小窗口时候为什么用SetLayout(null)时候窗口里什么东西都不显示?

本人刚刚学习JAVA,所以按照教科书在myeclipse7.5 上照打了这个例子,但是发现当SetLayout(null)时候,弹出的窗口一片空白什么都没有。各位大哥帮我看看到底为什么呢?程序相当简单,请耐心看看,谢谢了~~~~

import java.awt.*;

import java.awt.event.*;

class MyWin extends Frame implements ActionListener {

Button button;

Label label;

MyWin (){

setLayout(null);

button =new Button ("press me!");

label =new Label ("Don't touch me !");

add(button);

add(label);

button.addActionListener(this);

button.setBackground(Color.pink);

button.setForeground(Color.black);

button.setBounds(0, 10, 30, 20);

label.setBounds(150, 10, 30, 20);

label.setBackground(Color.yellow);

Cursor c =Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

setCursor(c);

button.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));

setVisible(true);

setBounds(0,0,400,300);

validate();

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

Rectangle rect = button.getBounds();

int x = (int)rect.getX();

int y = (int)rect.getY();

if(rect.intersects(label.getBounds()))

label.setVisible(false);

if(label.isVisible()){

x+=2;

button.setLocation(x, y);

}

else{

y+=3;

button.setLocation(x, y);

button.setLabel("up and down");

}

}

}

public class Example7_23 {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

MyWin win =new MyWin();

}

}

ronszb

发布

1个回答

67b0a70febe552c922c54bb8560d6ef6.png

69327cdfb8eb11a1a0c4d160612f2adb.png

你把setLayout(null)放到那个方法最后一行就可以了.....

setLayout是设置布局管理器 , 你先设置了布局管理器再增加的页面元素(如:Button),那就等于没加,只有先加上元素再加上布局管理器,这时候布局管理器才起作用

◆◆

评论读取中....

请登录后再发表评论!

◆◆

修改失败,请稍后尝试

精华知识

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值