简体   繁体   English

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

[英]My java program only runs once, then stops

When I input all of the values then click generate, it works, but it doesn't work when I try it again. 当我输入所有值然后单击生成时,它可以工作,但是当我再次尝试时,它不工作。

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);
    }
}

Please help me i've been struggeling for a long time. 请帮助我,我已经挣扎了很长时间。

You program works, just that you didn't clear the panel from the previous run. 您的程序可以正常工作,只是您没有从上次运行中清除面板。 Scroll down and you'll see it. 向下滚动,您将看到它。 Add panel.removeAll(); 添加panel.removeAll(); to your actionPerformed 采取行动

You probably want to add: 您可能要添加:

panel.removeAll();

somewhere in actionPerformed to clear the previous panels. 某处actionPerformed以清除以前的面板。 Also, when you paint , you should call JPanel#revalidate and JPanel#repaint so the panel will be actually repainted. 另外,在paint ,应该调用JPanel#revalidateJPanel#repaint以便实际对面板进行重新绘制。

JPanel has a method called revalidate that will redraw the underlying drawing context for that control. JPanel有一个称为revalidate的方法,它将为该控件重新绘制基础绘图上下文。 After you draw, call revalidate. 绘制后,调用revalidate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM