鱼C论坛

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

Gui问题

[复制链接]
发表于 2020-10-28 23:17:14 | 显示全部楼层 |阅读模式
10鱼币
主类与监听接口

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;

  4. public class SimpleGuiB implements ActionListener {
  5.     JButton colorbutton;
  6.     JFrame frame;
  7.     JButton lablebutton;
  8.     JLabel label;

  9.     public static void main(String [] args){
  10.         SimpleGuiB gui = new SimpleGuiB();
  11.         gui.go();
  12.     }public void go(){
  13.         frame = new JFrame();
  14.         JP jp = new JP();
  15.         label = new JLabel("I am a lable!");

  16.         lablebutton = new JButton("click here");
  17.         colorbutton = new JButton("Change Circle");
  18.         colorbutton.addActionListener(this);
  19.         lablebutton.addActionListener(new testbutton());

  20.         frame.getContentPane().add(BorderLayout.SOUTH,colorbutton);
  21.         frame.getContentPane().add(BorderLayout.WEST,label);
  22.         frame.getContentPane().add(BorderLayout.NORTH,lablebutton);
  23.         frame.getContentPane().add(BorderLayout.CENTER,jp);
  24.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.         frame.setSize(500,500);
  26.         frame.setVisible(true);

  27.     }
  28.     public void actionPerformed(ActionEvent event){
  29.         frame.repaint();
  30.         }
  31.     class testbutton implements ActionListener{
  32.         public void actionPerformed(ActionEvent event){
  33.             lablebutton.setText("我已经被按过了");
  34.             label.setText("ouch");
  35.         }
  36.     }
  37. }
复制代码

设置Circle渐变颜色的类
  1. import javax.swing.*;
  2. import java.awt.*;

  3. public class JP extends JPanel {
  4.     public void paintComponent(Graphics graphics){
  5.         Graphics2D g2d = (Graphics2D)graphics;
  6.         int red = (int)(Math.random()*255);
  7.         int green = (int)(Math.random()*255);
  8.         int blue = (int)(Math.random()*255);
  9.         Color starColor = new Color(red, green, blue);

  10.         red = (int)(Math.random()*255);
  11.         green = (int)(Math.random()*255);
  12.         blue = (int)(Math.random()*255);
  13.         Color endColor = new Color(red, green, blue);

  14.         GradientPaint gradient = new GradientPaint(70,70,starColor,150,150,endColor);
  15.         g2d.setPaint(gradient);
  16.         g2d.fillOval(70,70,100,100);
  17.         /*Image image = new ImageIcon("壁纸") .getImage();
  18.         graphics.drawImage(image,3,4,this);*/
  19.     }
  20. }
复制代码

基本结构就是,lablebutton(顶部按钮)的actionPerformed执行的是修改顶部按钮与右边面板的文字,可为啥点击后会导致圆圈的颜色发生变化,明明已经分开注册

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

点击前

点击前

点击后

点击后
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-15 17:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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