zxiaopp 2015-05-23 15:36 采纳率: 0%
浏览 4799

java 点一下按钮就能画一个图形,怎么弄呢

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class huatu {
static public void main(String[] args) {
new test();
}
}
class test extends JFrame {
JButton b1=new JButton("rect");
int f1=0,f2=0;
test() {
this.setBounds(100,100,500,500);
setLayout(new FlowLayout());
add(b1);
b1.addActionListener(new addActionListener());
this.setVisible(true);
}
class addActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        if(b1==e.getSource()) f1=1;
    }
}

}
class draw extends Canvas {
test t=null;
public draw (test t) {
this.t=t;
}
public void paint(Graphics g) {
if(t.f1==1)
g.drawRect(100, 100, 200, 200);
}

}


  • 写回答

2条回答 默认 最新

  • devmiao 2015-05-23 15:48
    关注
    评论

报告相同问题?