鱼C论坛

 找回密码
 立即注册
查看: 3921|回复: 1

Gui问题

[复制链接]
发表于 2020-10-28 23:17:14 | 显示全部楼层 |阅读模式
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执行的是修改顶部按钮与右边面板的文字,可为啥点击后会导致圆圈的颜色发生变化,明明已经分开注册

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-10-28 23:18:30 | 显示全部楼层
图丢了补一下

点击前

点击前

点击后

点击后
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 19:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表