public class Myframe extends Frame{
public void LaunchFrame(){
setSize(500,500);
setLocation(200,200);
setVisible(true);
Button b = new Button("开始");
b.setSize(10,10);
add(b);
new Threader().start();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void paint(Graphics g){
g.drawRect(100, 100, 300, 300);
}
class Threader extends Thread{
public void run(){
while(true){
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String args[]){
Myframe my = new Myframe();
my.LaunchFrame();
}
}
.............
Button b = new Button("开始");
b.setSize(10,10);
add(b);
............
修改为:
...............
Button b = new Button("开始");
setLayout( null ); //这行必不可少
b.setBounds(100, 150, 50, 25); //设置按钮的大小位置
add(b);
............
其它部分不变。
用心回答每个问题,如果有帮助,请采纳答案好吗,非常感谢~~。
add(b);
有问题,不能直接add.
add(new JPanel().add(b));