Java GUI的Button按钮事件
用窗口画一个圆,用3个按钮分别是红按钮,绿按钮,橙按钮实现圆的颜色转换。就是按红色按钮时圆的颜色就变成红色按绿色按时圆的颜色变成绿色,按橙色按钮时圆的颜色变成橙色。求代码...
用窗口画一个圆,用3个按钮分别是红按钮,绿按钮,橙按钮实现圆的颜色转换。就是按红色按钮时圆的颜色就变成红色按绿色按时圆的颜色变成绿色,按橙色按钮时圆的颜色变成橙色。求代码!
展开
3个回答
展开全部
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.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();
}
}
看你喜欢那个了,我编了很长时间啊,楼主给我分吧,哈哈!
展开全部
代码的话很多,我只能告诉你一下方法。
在JPanel里画,在里面取到graphics(paint方法的参数就是)。
按钮添加actionlistener,事件体是用graphics的setcolor改变颜色,然后调用jpanel的repaint方法重绘一下就行了。
在JPanel里画,在里面取到graphics(paint方法的参数就是)。
按钮添加actionlistener,事件体是用graphics的setcolor改变颜色,然后调用jpanel的repaint方法重绘一下就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在三个按钮的click事件里分别把red、blue和orange赋给圆的属性ForeColor的值就可以了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询