重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1、先给“自动运算”按钮添加一个点击事件监听器
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站制作、永嘉网络推广、小程序开发、永嘉网络营销、永嘉企业策划、永嘉品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供永嘉建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
2、再该监听器对象内部有相应方法,修改该方法。
3、当点击按钮时,会自动调用上面的方法。
but.addMouseListener(new MouseListener(){
重写接口里面的方法:
});
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Xiti13
{ public static void main(String args[])
{ new WindowCanvas();
}
}
class Mycanvas extends Canvas
{ int x,y,r;
Color c=new Color(0,0,0);
Mycanvas()
{ setBackground(Color.cyan);
}
public void setColor(Color c)
{ this.c=c;
}
public void setX(int x)
{ this.x=x;
}
public void setY(int y)
{ this.y=y;
}
public void setR(int r)
{ this.r=r;
}
public void paint(Graphics g)
{ g.setColor(c);
g.fillOval(x,y,2*r,2*r);
}
}
class WindowCanvas extends JFrame implements ActionListener
{ Mycanvas canvas;
TextField inputR,inputX,inputY;
Button buttonRed,buttonGreen,buttonYellow,buttonEnter;
Color c=new Color(0,0,0);
int x=0,y=0,r=0;
WindowCanvas()
{ canvas=new Mycanvas();
inputR=new TextField(5);
inputX=new TextField(4);
inputY=new TextField(4);
buttonRed=new Button("red");
buttonRed.setBackground(Color.red);
buttonGreen=new Button("green");
buttonGreen.setBackground(Color.green);
buttonYellow=new Button("yellow");
buttonYellow.setBackground(Color.yellow);
buttonEnter=new Button("确定");
buttonEnter.addActionListener(this);
buttonRed.addActionListener(this);
buttonGreen.addActionListener(this);
buttonYellow.addActionListener(this);
Panel pNorth=new Panel(),pSouth=new Panel();
pNorth.add(new Label("颜色:"));
pNorth.add(buttonRed);
pNorth.add(buttonGreen);
pNorth.add(buttonYellow);
pNorth.add(new Label("圆的位置坐标:"));
pNorth.add(inputX);
pNorth.add(inputY);
pSouth.add(new Label("圆的半径:"));
pSouth.add(inputR);
pSouth.add(buttonEnter);
add(canvas,BorderLayout.CENTER);
add(pNorth,BorderLayout.NORTH);
add(pSouth,BorderLayout.SOUTH);
setBounds(100,100,500,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{ Button b=(Button)e.getSource();
if(b==buttonEnter)
{ try{x=Integer.parseInt(inputX.getText());
y=Integer.parseInt(inputY.getText());
r=Integer.parseInt(inputR.getText());
canvas.setX(x);
canvas.setY(y);
canvas.setR(r);
canvas.setColor(c);
canvas.repaint();
}
catch(NumberFormatException ee)
{ x=0;y=0;r=0;
}
}
else
{ c=b.getBackground();
canvas.setX(x);
canvas.setY(y);
canvas.setR(r);
canvas.setColor(c);
canvas.repaint();
}
}
}
上面是第一个代码,那个可以随便改圆的位置,下面是第二个:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SignalFrame extends JFrame implements ActionListener
{ Button button1,button2,button3;
Color c;
SignalFrame()
{
button1=new Button("红灯");
button2=new Button("绿灯");
button3=new Button("黄灯");
button1.setBackground(Color.red);
button2.setBackground(Color.green);
button3.setBackground(Color.yellow);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
add(button1);
add(button2);
add(button3);
setLayout(new FlowLayout());
setBounds(100,100,200,300);
setVisible(true);
validate();
}
public void paint(Graphics g)
{ g.setColor(c);
g.fillOval(50,80,100,100);
}
public void update(Graphics g)
{ g.clearRect(50,80,100,100);
paint(g);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button1)
{ c=Color.red;
repaint();
}
else if(e.getSource()==button2)
{ c=Color.green;
repaint();
}
else if(e.getSource()==button3)
{ c=Color.yellow;
repaint();
}
}
}
public class HLD
{ public static void main(String args[])
{ SignalFrame frame;
frame=new SignalFrame();
}
}
看你喜欢那个了,我编了很长时间啊,楼主给我分吧,哈哈!
可以使用按钮单击事件,改变其它组件的状态,例如下面是一个计数器的例子:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Demo7 extends JFrame {
private int count = 1;
public Demo7() {
this.setSize(600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());
JLabel label = new JLabel(String.valueOf(count));
this.add(label);
JButton button = new JButton("Click Me!");
// 按钮单击事件,改变 Label 显示文本
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
count++;
label.setText(String.valueOf(count));
}}
);
this.add(button);
}
public static void main(String[] args) {
new Demo7().setVisible(true);
}
}