java - 如何使用 actionListener 更改 JButton 的颜色
问题描述
我有以下 JFrame。 在此处输入图像描述
它包含一个由方形和圆形 JButton 组成的 JButton[100]。当我点击它们时,我想改变它们的颜色。所以我为此使用 actionListener 。但是当我写 actionListener 来改变颜色时,它给了我一个例外
线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引 100 超出长度 100 的范围
它说异常在 b[i].setBackground(Color.blue); 在actionListener里面。我已经搜索了很多,没有任何东西改变颜色,但只是给出了例外。这是我的代码
主题.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class Subject extends JFrame{
public Subject() {
super("Subject");
p = new JPanel(new GridLayout(10,10));
b = new JButton[100];
Random r = new Random();
for(i=0;i<100;i++) {
y = r.nextInt(2) +1;
if(y==1) {
b[i] = new JButton();
b[i].setBackground(Color.black);
b[i].setPreferredSize(new Dimension(35,35));
p.add(b[i]);
}else {
b[i] = new Circle();
b[i].setBackground(Color.black);
p.add(b[i]);
}
b[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b[i].setBackground(Color.blue);
}
});
}
add(p);
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(100,200);
}
private JPanel p;
private JButton[] b;
private int i, y;
}
Circle.java
import java.awt.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class Circle extends JButton{
public Circle() {
setBackground(Color.red);
setFocusable(false);
/*
These statements enlarge the button so that it
becomes a circle rather than an oval.
*/
Dimension size = getPreferredSize();
size.width = size.height = Math.min(35,35);
setPreferredSize(size);
/*
This call causes the JButton not to paint the background.
This allows us to paint a round background.
*/
setContentAreaFilled(false);
}
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {
g.setColor(Color.yellow);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);
JLabel l = new JLabel("Click Me");
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(Color.darkGray);
g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
}
// Hit detection.
Shape shape;
public boolean contains(int x, int y) {
// If the button has changed size, make a new shape object.
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}
}
我也尝试了以下方法,但没有任何改变。
b[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b[i])
{
b[i].setBackground(Color.blue);
}
}
});
有人对如何修复它有任何建议吗?谢谢!
解决方案
只需在您的方法中更改以下行actionPerformed()
...
b[i].setBackground(Color.blue);
对这个...
((JButton) e.getSource()).setBackground(Color.blue);
您正在ActionListener
为每个JButton
. 因此,ActionEvent
源只能是JButton
您为其创建匿名类的源。
请注意,编译完 java 代码后,每个ActionListener
. 类文件名将以 开头Subject$
和结尾.class
,例如:
Subject$1.class
Subject$2.class
Subject$3.class
至少有 100 个这样的类,因为您已经为每个类创建了一个JButton
。
推荐阅读
- reactjs - 使用 Fromik 2.01RC 使用 Formik 重置表单
- html - ng-repeat 有什么问题
- python - 如何将 python-binance 安装到 anaconda3 中?
- react-native - 从子调用父函数被调用但不会改变状态
- matlab - 在 Matlab 中将图像分割成块
- reactjs - 在博览会中运行时反应本机版本不匹配
- python - 使用自定义用户代理字符串时 Selenium 缺少标签
- alarm - 尝试抑制 Sensu 警报在触发后 30 秒解决
- python - 如何在不显式保存图像的情况下执行 OpenCV 转换
- java - FirefoxDriver 不要使用我的代理 (geckodriver)