|
10鱼币
主类与监听接口
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class SimpleGuiB implements ActionListener {
- JButton colorbutton;
- JFrame frame;
- JButton lablebutton;
- JLabel label;
- public static void main(String [] args){
- SimpleGuiB gui = new SimpleGuiB();
- gui.go();
- }public void go(){
- frame = new JFrame();
- JP jp = new JP();
- label = new JLabel("I am a lable!");
- lablebutton = new JButton("click here");
- colorbutton = new JButton("Change Circle");
- colorbutton.addActionListener(this);
- lablebutton.addActionListener(new testbutton());
- frame.getContentPane().add(BorderLayout.SOUTH,colorbutton);
- frame.getContentPane().add(BorderLayout.WEST,label);
- frame.getContentPane().add(BorderLayout.NORTH,lablebutton);
- frame.getContentPane().add(BorderLayout.CENTER,jp);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(500,500);
- frame.setVisible(true);
- }
- public void actionPerformed(ActionEvent event){
- frame.repaint();
- }
- class testbutton implements ActionListener{
- public void actionPerformed(ActionEvent event){
- lablebutton.setText("我已经被按过了");
- label.setText("ouch");
- }
- }
- }
复制代码
设置Circle渐变颜色的类
- import javax.swing.*;
- import java.awt.*;
- public class JP extends JPanel {
- public void paintComponent(Graphics graphics){
- Graphics2D g2d = (Graphics2D)graphics;
- int red = (int)(Math.random()*255);
- int green = (int)(Math.random()*255);
- int blue = (int)(Math.random()*255);
- Color starColor = new Color(red, green, blue);
- red = (int)(Math.random()*255);
- green = (int)(Math.random()*255);
- blue = (int)(Math.random()*255);
- Color endColor = new Color(red, green, blue);
- GradientPaint gradient = new GradientPaint(70,70,starColor,150,150,endColor);
- g2d.setPaint(gradient);
- g2d.fillOval(70,70,100,100);
- /*Image image = new ImageIcon("壁纸") .getImage();
- graphics.drawImage(image,3,4,this);*/
- }
- }
复制代码
基本结构就是,lablebutton(顶部按钮)的actionPerformed执行的是修改顶部按钮与右边面板的文字,可为啥点击后会导致圆圈的颜色发生变化,明明已经分开注册
|
|