我正在为Uni进行课程作业,分配是用Jpanel创建一个矩形,但是我对创建的按钮有问题。
按钮应该执行以下操作:
用户点击南部地区的jbutton时填充有颜色的矩形应全部变为随机颜色,虽然填充有色2的矩形不应改变。一秒单击jbutton应该使矩形充满color2更改为随机颜色,而矩形充满了随机的颜色首次点击的颜色应保持相同的颜色。用户应该是能够继续无限地单击按钮,每次点击时一组矩形将充满随机颜色。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class RectanglesGUI {
JFrame frame;
RectangleDrawPanel drawPanel;
Color color1 = Color.orange;
Color color2 = Color.blue;
public static void main (String[] args) {
RectanglesGUI gui = new RectanglesGUI();
gui.go();
}
//this method sets up the JFrame
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new RectangleDrawPanel();
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().setBackground(Color.black);
frame.setSize(600,600);
frame.setVisible(true);
CreateButton newButton = new CreateButton();
newButton.CreateButton();
frame.repaint();
}
// To Create a new Button
public class CreateButton {
JButton buttonSOUTH;
JButton buttonNORTH;
public void CreateButton () {
buttonSOUTH = new JButton("Change Colors");
buttonSOUTH.setPreferredSize(new Dimension(100, 50));
buttonSOUTH.setVisible(true);
frame.add(buttonSOUTH, BorderLayout.SOUTH);
buttonSOUTH.addActionListener(new RandomColorListener());
buttonNORTH = new JButton("Reset Colors");
buttonNORTH.setPreferredSize(new Dimension(100, 50));
buttonNORTH.setVisible(true);
frame.add(buttonNORTH, BorderLayout.NORTH);
buttonNORTH.addActionListener(new ResetListener());
}
// ActionListener for buttonSOUTH
private class ResetListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
color1 = Color.orange;
color2 = Color.blue;
frame.repaint();
}
}
// ActionListener for buttonNORTH
private class RandomColorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
ChangeColor c = new ChangeColor();
for (int i = 0; i < 100; i++){
if (i % 2 == 0) {
color1 = c.getColor1();
frame.repaint();
} else {
color2 = c.getColor2();
frame.repaint();
}
}
}
}
// Change Color Class
private class ChangeColor {
private Color getColor1(){
Random fill1 = new Random();
color1 = new Color (fill1.nextInt(256), fill1.nextInt(256),
fill1.nextInt(256));
return color1;
}
private Color getColor2(){
Random fill2 = new Random();
color2 = new Color (fill2.nextInt(256), fill2.nextInt(256),
fill2.nextInt(256));
return color2;
}
}
}
class RectangleDrawPanel extends JPanel {
public void paintComponent (Graphics g){
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
int width = getWidth();
int height = getHeight();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int x = (getWidth() / 5) * i;
int y = (getHeight() / 5) * j;
if ((i % 2) == (j % 2)) {
g2.setColor(color1);
} else
g2.setColor(color2);
g2.fill3DRect(x,y,width,height,true);
}
}
}
}
}
,但我不知道如何将其设置为无限
for (int i = 0; i < 100; i++){
if (i % 2 == 0) {
color1 = c.getColor1();
frame.repaint();
}
else {
color2 = c.getColor2();
frame.repaint();
}
}
此部分for (int i = 0; i < 100; i++)
对检查没有用。
两者都将始终执行内部条件检查。这意味着color2和color1都会改变。因为我%2将返回0和1
将此属性添加到Rectanglegui类boolean status=false;
然后在类RandomColorListener中更改"方法"中的代码操作,为:
if (status) {
color1 = c.getColor1();
frame.repaint();
} else {
color2 = c.getColor2();
frame.repaint();
}
status=!status;