Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的java程序只运行一次,然后停止_Java_Swing - Fatal编程技术网

我的java程序只运行一次,然后停止

我的java程序只运行一次,然后停止,java,swing,Java,Swing,当我输入所有的值,然后单击generate,它就工作了, 但当我再试一次时,它就不起作用了 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class colRand { static JPanel[][] square; static JFrame colRand = new JFrame(); static JPanel settings = new JPanel()

当我输入所有的值,然后单击generate,它就工作了,
但当我再试一次时,它就不起作用了

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class colRand {

    static JPanel[][] square;
    static JFrame colRand = new JFrame();
    static JPanel settings = new JPanel();
    static JPanel panel = new JPanel();

    public static void main(String[] args) {
        colRand.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        colRand.setLocationRelativeTo(null);
        colRand.setTitle("Color Randomizer");
        JTextArea dim = new JTextArea("Grid Dimensions");
        dim.setEditable(false);
        JTextField width = new JTextField("Width");
        JTextField height = new JTextField("Height");
        JCheckBox reds = new JCheckBox("reds");
        JCheckBox greens = new JCheckBox("greens");
        JCheckBox blues = new JCheckBox("blues");
        JButton generate = new JButton("Generate!");
        settings.add(dim);
        settings.add(width);
        settings.add(height);
        settings.add(reds);
        settings.add(greens);
        settings.add(blues);
        settings.add(generate);
        settings.setVisible(true);
        generate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int w = Integer.parseInt(width.getText());
                int h = Integer.parseInt(height.getText());
                boolean R = reds.isSelected();
                boolean G = greens.isSelected();
                boolean B = blues.isSelected();
                square = new JPanel[w][h];
                for(int i = 1; i < Integer.parseInt(width.getText()); i++) {
                    for(int j = 1; j < Integer.parseInt(height.getText()); j++) {
                        square[i][j] = new JPanel();
                        square[i][j].setBackground(Color.black);
                        panel.add(square[i][j]);
                        square[i][j].setVisible(true);
                    }
                }
                paint(w, h, R, G, B);
                colRand.setSize(w * 10, h * 10);
            }
        });
        panel.setBackground(Color.black);
        colRand.add(panel);
        colRand.add(settings, BorderLayout.SOUTH);
        colRand.pack();
        colRand.setVisible(true);
    }

    public static void paint(int w, int h, boolean reds, boolean greens, boolean blues) {
        for(int i = 1; i < w; i++) {
            for(int j = 1; j < h; j++) {
                square[i][j].setBackground(randColor(reds, greens, blues));
            }
        }
    }

    public static Color randColor(boolean reds, boolean greens, boolean blues) {
        int R, G, B;

        R = (int)(Math.random() * 255);
        G = (int)(Math.random() * 255);
        B = (int)(Math.random() * 255);

        if(reds == false) {
            R = 0;
        }
        if(greens == false) {
            G = 0;
        }
        if(blues == false) {
            B = 0;
        }
        return new Color(R, G, B);
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类colRand{
静态JPanel[][]正方形;
静态JFrame colRand=newjframe();
静态JPanel设置=新建JPanel();
静态JPanel面板=新JPanel();
公共静态void main(字符串[]args){
colRand.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
colRand.setLocationRelativeTo(空);
colRand.setTitle(“颜色随机化器”);
JTextArea dim=新的JTextArea(“网格尺寸”);
dim.setEditable(false);
JTextField width=新的JTextField(“宽度”);
JTextField高度=新JTextField(“高度”);
JCheckBox reds=新JCheckBox(“reds”);
JCheckBox绿色=新的JCheckBox(“绿色”);
JCheckBox blues=新的JCheckBox(“blues”);
jbuttongenerate=新JButton(“generate!”);
设置。添加(dim);
设置。添加(宽度);
设置。添加(高度);
设置。添加(红色);
设置。添加(绿色);
设置。添加(蓝色);
设置。添加(生成);
setVisible(true);
generate.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
intw=Integer.parseInt(width.getText());
inth=Integer.parseInt(height.getText());
布尔R=reds.isSelected();
布尔G=greens.isSelected();
布尔B=blues.isSelected();
方形=新JPanel[w][h];
对于(inti=1;i

请帮帮我,我已经挣扎了很长时间。

您的程序运行正常,只是您没有从上一次运行中清除面板。向下滚动,您将看到它。 添加 panel.removeAll()
要执行操作

您的程序可以运行,只是您没有从上一次运行中清除面板。向下滚动,您将看到它。 添加 panel.removeAll()
您可能希望在执行的操作中添加:

panel.removeAll();

操作的某处执行
以清除以前的面板。另外,当您
绘制时,您应该调用,以便面板将实际重新绘制。

您可能需要添加:

panel.removeAll();

操作的某处执行
以清除以前的面板。另外,当您
绘制时,您应该调用,以便面板将实际重新绘制。

JPanel有一个名为
revalidate
的方法,该方法将重新绘制该控件的基础绘图上下文。绘制后,调用revalidate。

JPanel有一个名为
revalidate
的方法,该方法将重新绘制该控件的基础绘制上下文。绘制后,调用重新验证。

发布堆栈跟踪或您所看到的错误“但当我再次尝试时它不起作用”这是什么意思?“再试一次”的具体步骤是什么?还有,会发生什么?小的旁白:你应该使用变量
w
h
,而不是在循环中调用
parseInt()
。我测试了它,我看到的唯一大问题是每次单击Generate时我都必须调整窗口的大小。发布堆栈跟踪或你看到的错误“但是当我再试一次时,它不起作用”你是什么意思?当你“再试一次”时,具体步骤是什么?还有,发生了什么?小注释:你应该使用变量
w
h
,而不是调用
parseInt()
在您的循环中。我对它进行了测试,我看到的唯一大问题是每次单击“生成”时都必须调整窗口的大小。