2016-08-22 136 views
0

当我输入所有的值,然后点击生成,它的工作, ,但它不工作时,我再试一次。我的java程序只运行一次,然后停止

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

请帮助我,我已经很长一段时间struggeling。

+2

发布堆栈跟踪或您遇到的错误 –

+2

“但是当我再次尝试时它不起作用”您是什么意思? “再试一次”的确切步骤是什么?另外,会发生什么? –

+0

小方评论:你应该使用变量'w'和'h',而不是在循环中调用'parseInt()'。 –

回答

4

你编程的作品,只是你没有清除上一次运行的面板。向下滚动,你会看到它。 Add panel.removeAll(); 您的actionPerformed

0

JPanel有一个名为revalidate的方法,该方法将重绘该控件的底层绘图上下文。绘制完成后,调用revalidate。