簡體   English   中英

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

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

當我輸入所有值然后單擊生成時,它可以工作,但是當我再次嘗試時,它不工作。

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

請幫助我,我已經掙扎了很長時間。

您的程序可以正常工作,只是您沒有從上次運行中清除面板。 向下滾動,您將看到它。 添加panel.removeAll(); 采取行動

您可能要添加:

panel.removeAll();

某處actionPerformed以清除以前的面板。 另外,在paint ,應該調用JPanel#revalidateJPanel#repaint以便實際對面板進行重新繪制。

JPanel有一個稱為revalidate的方法,它將為該控件重新繪制基礎繪圖上下文。 繪制后,調用revalidate。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM