JAVA中点击一个按钮,画一个圆形的代码点击另一个按钮画方形
importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener...
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class huahua extends Frame implements ActionListener{
int a;
JButton bt1 = null;
JButton bt2 = null;
public huahua(){
bt1 = new JButton("画圆");
bt2 = new JButton("画方");
this.add(bt1);this.add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
this.setLayout(new FlowLayout());
this.setSize(300,300);
this.show();
}
public static void main(String[] args) {
new huahua();
} public void paint(Graphics g){
g.drawOval(100, 100, 200, 200);
g.drawRect(10, 50, 50, 50);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bt1){
repaint();
} else if(e.getSource()==bt2){
repaint();
}
}
}
实现不了,麻烦大神帮看看 展开
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class huahua extends Frame implements ActionListener{
int a;
JButton bt1 = null;
JButton bt2 = null;
public huahua(){
bt1 = new JButton("画圆");
bt2 = new JButton("画方");
this.add(bt1);this.add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
this.setLayout(new FlowLayout());
this.setSize(300,300);
this.show();
}
public static void main(String[] args) {
new huahua();
} public void paint(Graphics g){
g.drawOval(100, 100, 200, 200);
g.drawRect(10, 50, 50, 50);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bt1){
repaint();
} else if(e.getSource()==bt2){
repaint();
}
}
}
实现不了,麻烦大神帮看看 展开
1个回答
展开全部
package image;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class huahua extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
int a;
JButton bt1 = null;
JButton bt2 = null;
int width = -1;
int height = -1;
public huahua ()
{
bt1 = new JButton ("画圆");
bt2 = new JButton ("画方");
bt1.addActionListener (this);
bt2.addActionListener (this);
this.setLayout (new FlowLayout ());
this.add (bt1);
this.add (bt2);
this.setSize (300, 300);
this.setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setVisible (true);
}
public static void main ( String[] args )
{
new huahua ();
}
public void paint ( Graphics g )
{
super.paint (g);
g.drawOval (100, 100, width, height);
g.drawRect (10, 50, width / 2, height / 2);
g.dispose ();
}
public void actionPerformed ( ActionEvent e )
{
if (e.getSource () == bt1)
{
width = width > 200 ? 5 : ( width += 5 );
height = height > 200 ? 5 : ( height += 5 );
repaint ();
}
else if (e.getSource () == bt2)
{
width = width < 5 ? 200 : ( width -= 5 );
height = height < 5 ? 200 : ( height -= 5 );
repaint ();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询