用eclipse做出一个界面,要求在点击圆或者矩形的按钮时, 窗体上绘制出一个对应的图形
但是我在点击按钮后,窗体上的图形没有绘制出来,只有最小化然后最大化后才会绘出来,我想是窗体没有实现刷新的原因,怎么做才能实现啊 ,我是初学者,不太懂,还请各位高手不吝赐教啊
代码如下:
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class draw extends Frame implements ActionListener
{
Checkbox c1,c2;
CheckboxGroup cb;
Button b1,b2,b3,b4;
String st;
public draw()
{
super("画图程序");
setSize(640,480);
setVisible(true);
setLayout(null);
setResizable(false);
cb=new CheckboxGroup();
c1=new Checkbox("圆",false,cb);
c2=new Checkbox("矩形",false,cb);
b1=new Button("上移");
b2=new Button("下移");
b3=new Button("左移");
b4=new Button("右移");
st=new String("");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
c1.setBounds(200,50,80,23);
c2.setBounds(410,50,80,23);
b1.setBounds(100,425,75,30);
b2.setBounds(224,425,75,30);
b3.setBounds(346,425,75,30);
b4.setBounds(470,425,75,30);
add(c1);
add(c2);
add(b1);
add(b2);
add(b3);
add(b4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void paint(Graphics g)
{
g.setColor(Color.black);
if(c1.getState())
{
g.drawOval(300, 250, 40, 40);
}
if(c2.getState())
{
g.drawRect(300, 250, 40, 20);
}
else
{
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if(c1.getState())
{
}
if(e.getSource()==b1)
{
st=e.getActionCommand();
}
if(e.getSource()==b2)
{}
if(e.getSource()==b3)
{}
if(e.getSource()==b4)
{}
}
public static void main(String args[])
{
new draw();
}
}
------解决方案--------------------
写个线程 控制页面重新刷新
- Java code
class Monitor implements Runnable { public void run() { while(true){ repaint(); try { Thread.sleep(25); } catch (InterruptedException e) { e.printStackTrace(); } } } }