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

时间:2016-08-22 23:05:47

标签: java swing

当我输入所有值然后单击生成时,它可以工作, 但是当我再次尝试时它不起作用。

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

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

3 个答案:

答案 0 :(得分:4)

您的程序有效,只是您没有清除上一次运行的面板。向下滚动,你会看到它。 加     的 panel.removeAll(); 你的行动已成型

答案 1 :(得分:0)

您可能想要添加:

panel.removeAll();

actionPerformed中的某处清除前面的面板。此外,当您paint时,您应该致电JPanel#revalidate and JPanel#repaint,以便实际重新绘制该面板。

答案 2 :(得分:0)

JPanel有一个名为string query = "select * from employee where empName = @empName"; string empName = "abc/abc"; using (DbCommand cmd = db.GetSqlStringCommand(query)) { db.AddInParameter(cmd, "empName", DbType.String, empName); using (IDataReader reader = db.ExecuteReader(cmd)) { } } 的方法,它将重绘该控件的基础绘图上下文。绘制后,调用revalidate。