atm小程序 gui java 代码_一个 Java 入门 GUI 小程序,就是不太明白为什么我会多一个假 button 块...

这篇博客讨论了一个Java GUI应用,其中包含一个真实的按钮和一个假按钮。假按钮在鼠标悬停时不改变样式且无法点击,而真按钮则能正常触发颜色变化的程序。作者展示了如何通过ActionListener实现按钮点击后的重绘操作,导致色块颜色随机变化。代码中,MyDrawPanel类的paintComponent方法用于绘制和填充随机颜色的矩形。
摘要由CSDN通过智能技术生成

cd07d05d446283829ac110865f991fe8.png

上面的那个 button 按钮是假的,第一鼠标移动上去不变样子,第二点也点不了

点下面那个真的我代码安排好的,很正常.并且点了一次之后上面那个假的也会消失,程序正常运行,色块变色

程序代码如下:

package SimpleGui3;

import javax.swing.*;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.*;

public class SimpleGui3 {

public static void main(String[] args) {

MyFrame gui = new MyFrame();

}

}

class MyFrame extends JFrame implements ActionListener {

JButton button;

MyDrawPanel draw_panel;

public MyFrame() {

this.setVisible(true);

this.setSize(300, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

button = new JButton("click to change colors");

button.addActionListener(this);

this.getContentPane().add(BorderLayout.SOUTH,button);

draw_panel = new MyDrawPanel();

this.getContentPane().add(BorderLayout.CENTER,draw_panel);

}

public void actionPerformed(ActionEvent event) {

this.repaint();

}

}

//无关紧要的部分↓

class MyDrawPanel extends JPanel {

public void paintComponent(Graphics g) {

int[] color_array = new int[3];

for(int i=0;i

color_array[i] = (int)(Math.random() * 255);

}

Color c = new Color(color_array[0],color_array[1],color_array[2]);

g.setColor(c);

g.fillRect(20, 50, 100, 100);

}

}

到底是咋回事儿啊,求大佬解答,谢谢..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值