想实现点开始,方块自动下落。不知道为什么,点了开始就不动了,按钮也不弹不上来。
谁解释下。import java.awt.*;
import javax.swing.*;
import java.awt.event.*;class huabu extends Canvas
{
int i=30,j=50;
huabu()
{
getSize();
}
public void paint(Graphics g)
{
setBackground(Color.red);
//g.drawString("hello",20,30);
g.setColor(Color.yellow);
g.fillRect(100,i,20,20);
g.fillRect(100,j,20,20);
g.fillRect(120,j,20,20);
g.fillRect(140,j,20,20);
i=i+10;
j=j+10;
}
public Dimension getPreferredSize()
{return new Dimension(300,400);}
}
class mianban extends JPanel implements ActionListener
{
JButton b1;
JButton b2;
huabu cav;
mianban()
{
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
b1=new JButton("开始");
b2=new JButton("重置");
cav=new huabu();
add(cav);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
thread1 th=new thread1();
th.start(); /* for(int j=1;j<20;j++)
{
// cav.repaint();
try{Thread.sleep(500);}
catch(InterruptedException e1){}
cav.repaint();
}*/
}
if(e.getSource()==b2)
{cav.i=30;
cav.j=50;
cav.repaint();
}
}
class thread1 extends Thread
{
public void run()
{
for(int i=20;i>0;i--)
{
cav.repaint();
try{sleep(500);}
catch(InterruptedException e){}
}
}
}
}public class Fangkuai extends JFrame
{
mianban pane=new mianban();
Fangkuai()
{
super("方块程序");
getContentPane().add(pane);
setSize(400,600);
setVisible(true);
addWindowListener(new shut());
}
public static void main(String[] args)
{
Fangkuai fang=new Fangkuai();
}
}
class shut extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
}
谁解释下。import java.awt.*;
import javax.swing.*;
import java.awt.event.*;class huabu extends Canvas
{
int i=30,j=50;
huabu()
{
getSize();
}
public void paint(Graphics g)
{
setBackground(Color.red);
//g.drawString("hello",20,30);
g.setColor(Color.yellow);
g.fillRect(100,i,20,20);
g.fillRect(100,j,20,20);
g.fillRect(120,j,20,20);
g.fillRect(140,j,20,20);
i=i+10;
j=j+10;
}
public Dimension getPreferredSize()
{return new Dimension(300,400);}
}
class mianban extends JPanel implements ActionListener
{
JButton b1;
JButton b2;
huabu cav;
mianban()
{
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
b1=new JButton("开始");
b2=new JButton("重置");
cav=new huabu();
add(cav);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
thread1 th=new thread1();
th.start(); /* for(int j=1;j<20;j++)
{
// cav.repaint();
try{Thread.sleep(500);}
catch(InterruptedException e1){}
cav.repaint();
}*/
}
if(e.getSource()==b2)
{cav.i=30;
cav.j=50;
cav.repaint();
}
}
class thread1 extends Thread
{
public void run()
{
for(int i=20;i>0;i--)
{
cav.repaint();
try{sleep(500);}
catch(InterruptedException e){}
}
}
}
}public class Fangkuai extends JFrame
{
mianban pane=new mianban();
Fangkuai()
{
super("方块程序");
getContentPane().add(pane);
setSize(400,600);
setVisible(true);
addWindowListener(new shut());
}
public static void main(String[] args)
{
Fangkuai fang=new Fangkuai();
}
}
class shut extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
}
解决方案 »
- 一个for循环就能打印出菱形!
- 写个贪吃蛇小程序事件响应有问题
- 请教问题! 欢迎近来,给分20!
- 勇闯黄金十二宫--天秤宫
- 请高手指教啊!class$还有关于exception,谢谢!
- JInternalFrame如何从内存中释放掉(马上加分)
- 调用什么函数可以将文本框的内容清空?
- 菜鸟的问题:“Exception in thread "main" java.lang.NoClassDefFoundError: Hello”
- jbuilder6如何配置jndi
- java基本概念的问题
- 请问在使用getRGB时为什么黑白图像读出来的rgb不完全是0或者255
- 关于import,大家进来看看!讨论下!
现在又好了。。
重复点开始 方块确实加快了。不知道为什么?刚学java 写来练习练习。多谢大家指导。