Java 使用JButton更改颜色,将for循环终止条件设置为无限
我正在为大学做一个课程作业,作业是用JPanel创建一个矩形UI,但是我对我创建的按钮有一个问题 按钮south应执行以下操作: 当用户单击南部地区的JButton时 填充颜色1的矩形应全部更改为随机颜色, 而用color2填充的矩形不应改变。一秒钟 单击JButton应该使矩形充满color2 all 更改为随机颜色,而矩形中填充随机颜色 第一次单击时的颜色应保持不变。用户应该是 能够无限期地继续单击按钮,每次单击 一组矩形将用随机颜色填充Java 使用JButton更改颜色,将for循环终止条件设置为无限,java,swing,jbutton,Java,Swing,Jbutton,我正在为大学做一个课程作业,作业是用JPanel创建一个矩形UI,但是我对我创建的按钮有一个问题 按钮south应执行以下操作: 当用户单击南部地区的JButton时 填充颜色1的矩形应全部更改为随机颜色, 而用color2填充的矩形不应改变。一秒钟 单击JButton应该使矩形充满color2 all 更改为随机颜色,而矩形中填充随机颜色 第一次单击时的颜色应保持不变。用户应该是 能够无限期地继续单击按钮,每次单击 一组矩形将用随机颜色填充 import javax.swing.*; impo
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);
}
}
}
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.*;
公共类矩形GUI{
JFrame框架;
矩形绘图板绘图板;
Color color1=Color.orange;
Color color2=Color.blue;
公共静态void main(字符串[]args){
矩形gui=新矩形gui();
gui.go();
}
//此方法设置JFrame
公开作废go(){
frame=新的JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel=新矩形drawPanel();
frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
frame.getContentPane().setBackground(颜色:黑色);
框架。设置尺寸(600600);
frame.setVisible(true);
CreateButton newButton=新建CreateButton();
CreateButton();
frame.repaint();
}
//创建新按钮的步骤
公共类创建按钮{
JButton buttonSOUTH;
JButton buttonNORTH;
公共void CreateButton(){
buttonSOUTH=新的JButton(“更改颜色”);
按钮输出设置首选尺寸(新尺寸(100,50));
按钮outh.setVisible(真);
框架。添加(按钮南部,边界布局。南部);
addActionListener(新的RandomColorListener());
buttonNORTH=新的JButton(“重置颜色”);
按钮北设置首选尺寸(新尺寸(100,50));
按钮北设置可见(真);
添加(按钮北,边框布局北);
addActionListener(新的ResetListener());
}
//按钮输出的ActionListener
私有类ResetListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
color1=Color.orange;
color2=Color.blue;
frame.repaint();
}
}
//buttonNORTH的ActionListener
私有类RandomColorListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
ChangeColor c=新的ChangeColor();
对于(int i=0;i<100;i++){
如果(i%2==0){
color1=c.getColor1();
frame.repaint();
}否则{
color2=c.getColor2();
frame.repaint();
}
}
}
}
//更改颜色类别
私有类更改颜色{
私有颜色getColor1(){
随机填充1=新的随机();
color1=新颜色(fill1.nextInt(256),fill1.nextInt(256),
填写1.nextInt(256));
返回颜色1;
}
私有颜色getColor2(){
随机填充2=新的随机();
color2=新颜色(fill2.nextInt(256),fill2.nextInt(256),
fill2.nextInt(256));
返回颜色2;
}
}
}
类RectangleDrawPanel扩展了JPanel{
公共组件(图形g){
超级组件(g);
图形2d g2=(图形2d)g;
int width=getWidth();
int height=getHeight();
对于(int i=0;i<5;i++){
对于(int j=0;j<5;j++){
intx=(getWidth()/5)*i;
int y=(getHeight()/5)*j;
如果((i%2)==(j%2)){
g2.setColor(color1);
}否则
g2.setColor(color2);
g2.fill3DRect(x,y,宽度,高度,真值);
}
}
}
}
}
但我不知道如何将它设为无限
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++){
如果(i%2==0){
color1=c.getColor1();
frame.repaint();
}
否则{
color2=c.getColor2();
frame.repaint();
}
}
此部分用于(int i=0;i<100;i++)
不用于检查。始终执行两个部件内部条件检查。这意味着color2和color1都会改变。因为i%2将返回0和1
将此属性添加到RectangleGui类
boolean status=false代码>
然后将类RandomColorListener中执行的方法ActionExecuted内的代码更改为:
if (status) {
color1 = c.getColor1();
frame.repaint();
} else {
color2 = c.getColor2();
frame.repaint();
}
status=!status;
如果没有在测试表达式部分添加任何内容(删除i<100
并使其为空),这将使for循环无限。我厌倦了,一旦单击按钮,循环执行将继续,程序将挂起,直到我终止它(我认为对于for循环无限,这是正常的),但我真正需要的是每次点击按钮时在颜色1和颜色2之间切换,没有限制(这里是100),我可以将其设置为99999999,但如何使其无限?哦,我想我误解了。你的意思是你只能用当前代码点击按钮100次吗?是的,我的意思是,也许我在这里使用了错误的循环来实现它,