我在将侦听器与southButton相关联以处理事件时遇到了问题。
(southButton.addActionListener(new RandomColorListener(drawPanel));)
当用户点击南部地区的JButton时, 填充color1的矩形应该全部变为随机颜色, 而填充了color2的矩形不应该改变。一秒 点击JButton应该使矩形充满color2 all 更改为随机颜色,而矩形填充随机 第一次点击的颜色应该保持相同的颜色。
用户应该能够无限期地继续单击按钮,每次单击一组矩形将填充随机颜色。在每种情况下,要改变的矩形应该是保持不变的矩形 最后一次点击(换句话说,颜色变化应该在两组矩形之间交替)。这意味着每次单击只有一组矩形应该改变颜色。
我试图将drawPanel传递给randomListener,但是出现了这个Java错误。
Java错误:类中的构造函数RandomColorListener RectanglesGUI.RandomColorListener不能应用于给定类型。
有人可以告诉我如何在class randomListener
中使用drawPanel import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class RectanglesGUI
{
JFrame frame;
RectangleDrawPanel drawPanel;
Color color1 = Color.orange;
Color color2 = Color.blue;
//Set fixed number of rows and columns
protected static final int ROWS = 5;
protected static final int COLS = 5;
private List<Color>colors;
public static void main (String[] args)
{
RectanglesGUI gui = new RectanglesGUI();
gui.go();
}
//this method sets up the JFrame, adds the button and drawpanel to the frame and adds the ActionListener to the button
public void go()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new RectangleDrawPanel();
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
//Add a JButton to the SOUTH region of the JFrame using BorderLayout. Set the text on the button.
JButton southButton = new JButton("Click to CHANGE color on alternate set of Rectangles");
frame.getContentPane().add(BorderLayout.SOUTH, southButton);
frame.setSize(600,600);
frame.setVisible(true);
//Relate a listener to the southButton to handle events.
southButton.addActionListener(new RandomColorListener(drawPanel));
//Relate another listener to northButton.
northButton.addActionListener(new ResetListener() {});
}
//Inner class to implement actionListener and listen to the JButton in the SOUTH region
class RandomColorListener implements ActionListener
{
private RectangleDrawPanel myPanel;
public RectangleDrawPanel (RectangleDrawPanel aPanel)
{
this.myPanel = aPanel;
}
@Override
public void actionPerformed (ActionEvent e)
{
int r = (int)(Math.random()*256);
int g = (int)(Math.random()*256);
int b = (int)(Math.random()*256);
color1 = new Color(r,g,b);
RectangleDrawPanel();
}
}
class RectangleDrawPanel extends JPanel{
@Override
public void paintComponent (Graphics g)
{
//Store 2 colors Alternatively in the Arraylist using for loop
int length = ROWS * COLS;
colors = new ArrayList<>(length);
for(int i=1;i<=length;i++)
{
double count;
count = i;
if(count%2 == 0)
{colors.add(color2);}
else
{colors.add(color1);}
}
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//Distribute the rectangles Evenly using for loop and Fill them with colors (from ArrayList)
int rectWidth = getWidth()/5;
int rectHeight = getHeight()/5;
for (int y = 0; y < ROWS; y++)
{
for (int x = 0; x < COLS; x++)
{
int index=(y * COLS) + x;
g2.setColor(colors.get(index));
g2.fillRect(x * rectWidth, y * rectHeight, rectWidth, rectHeight);
}
}
}
}
}