2013-04-20 26 views
0

这实际上是我的第一个完整程序,它最终将用于编写我正在制作的机器人的代码。一切正常,除了当我运行它时,我必须拖动窗口才能看到它里面的内容。直到我拖动打开的窗口才能看到JFrame的内容

有关如何修复它的任何建议。 frame2 - “点击建立文件”的作品。 框架 - 带按钮网格的框架,是我必须拖动打开才能看到的框架。

这里是帧中的设置文件(即不工作的那一个)

package Grid; 

//march 13 to April 11 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import java.awt.*; 
import java.awt.event.*; 


@SuppressWarnings("serial") 
public class ButtonGrid extends JFrame { 
    public static int clicked[][] = new int[20][40]; 
    static JButton button[] = new JButton[800]; 
    static int x; 
    static int count = 1; 
    public static int clickedfinal[][]; 
    int value; 

    public ButtonGrid() { 

     JFrame frame = new JFrame(); 
     frame.setSize(400, 200); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

     GridLayout grid = new GridLayout(20, 40, 10, 8); 
     frame.setLayout(grid); 

     for (int c = 0; c < 20; c++) { 
      for (int d = 0; d < 40; d++) { 
       clicked[c][d] = 0; 
      } 
     } 
     for (x = 0; x < 800; x++) { 
      button[x] = new JButton(); 
      button[x].setActionCommand(Integer.toString(x)); 
      frame.add(button[x]); 
      button[x].setBackground(Color.LIGHT_GRAY); 
      button[x].setOpaque(true); 

      thehandler handler = new thehandler(); 
      button[x].addActionListener(handler); 
     } 
    } 

    public class thehandler implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      for (;;) { 
       value = Integer.parseInt(e.getActionCommand()); 
       button[value].setBackground(Color.BLACK); 

       int r = value % 40; 
       int m = ((value - (value % 40))/40); 
       // learn how to round up 

       clicked[m][r] = 1; 

       break; 

      } 

     } 

    } 

    public static void main(String[] args) { 
     new ButtonGrid(); 

    } 

} 

这里是帧2的文件,即不工作的一个;测试只是运行这一个。

package Grid; 

import javax.swing.JButton; 
import javax.swing.JFrame; 

import Grid.ButtonGrid; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedWriter; 
import java.io.FileWriter; 
import java.io.IOException; 

@SuppressWarnings("serial") 
public class Arduinowriter extends JFrame { 

    static int t = 1; 
    static int buttonpressed; 
    static int total; 
    static String Code; 
    static String oldcode; 

    public Arduinowriter() { 
     JFrame frame2 = new JFrame("Build File"); 
     JButton button = new JButton("Click to Build File"); 
     frame2.setSize(400, 400); 
     frame2.add(button); 
     frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame2.setVisible(true); 

     button.setActionCommand(Integer.toString(1)); 
     thehandler handler = new thehandler(); 
     button.addActionListener(handler); 

    } 

    public class thehandler implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      buttonpressed = Integer.parseInt(e.getActionCommand()); 

      String newLine = System.getProperty("line.separator"); 
      String Motor2_half = "digitalWrite(Motor2, HIGH);" + newLine 
        + "delay(500)" + newLine + "digitalWrite(Motor2, LOW)"; 

      String Motor2_one = "digitalWrite(Motor2, HIGH);" + newLine 
        + "delay(1000);" + newLine + "digitalWrite(Motor2, LOW);"; 
      String Servo1_dispense = "digitalWrite(Servo1, HIGH);" + newLine 
        + "delay(1000)" + newLine + "digitalWrite(Servo1, LOW);"; 

      String Motor2_back = "digitalWrite(Motor2back, High" + newLine 
        + "delay(6000)" + newLine + "digitalWrite(Motor2back, Low)"; 

      String Motor1_forward = "digitalWrite(Motor1, HIGH);" + newLine 
        + "delay(1000);" + newLine + "digitalWrite(Motor1, LOW);"; 

      String dispenseCycle = (Motor2_one + newLine + Servo1_dispense 
        + " //Domino" + newLine); 

      String skipCycle = (Motor2_one + newLine); 

      String backCycle = (Motor1_forward + newLine + Motor2_back + newLine); 

      while (buttonpressed == 1) { 

       for (int x = 0; x < 20; x++) { 

        Code = oldcode + "//Line " + (x + 1); 
        oldcode = Code; 
        yloop: for (int y = 0; y < 40; y++) { 

         boolean empty = true; 
         for (int check = y; check < 39; check++) { 

          if (ButtonGrid.clicked[x][check] == 1) { 
           empty = false; 
           System.out.println(x + " not empty"); 
          } 
         } 

         if (ButtonGrid.clicked[x][y] == 1 && y == 0) { 
          Code = oldcode + newLine + Servo1_dispense 
            + " //Domino" + newLine; 
         } else if (ButtonGrid.clicked[x][y] == 1) { 
          Code = oldcode + newLine + dispenseCycle + newLine; 
         } 

         else if (ButtonGrid.clicked[x][y] == 0 
           && empty == false) { 
          Code = oldcode + newLine + skipCycle + newLine; 
         } else { 
          Code = oldcode + newLine + backCycle + newLine; 
          oldcode = Code; 
          break yloop; 
         } 
         oldcode = Code; 

        } 
       } 

       try { 

        BufferedWriter out = new BufferedWriter(new FileWriter(
          "C:\\ArduinoCode.txt")); 
        out.write(Code); 
        out.close(); 
       } catch (IOException g) { 
        System.out.println("Exception "); 

       } 

       return; 
      } 

     } 

    } 

    // button 

    public static void main(String[] args) { 

     new ButtonGrid(); 
     new Arduinowriter(); 

    } 
} 

注:我是一个非常初学者。这段代码花费了很多时间来编写,我使用google和youtube计算了所有内容。如果代码中有任何内容或我所说的内容不清楚或没有意义,请原谅我,然后问。

+1

设置layoutmanagers和其他影响其“视觉”状态的所有内容后,将其设置为可见'.setVisible(true)'。 – arynaq 2013-04-20 19:10:31

+0

你正在创造** 800 **'JButtons',那太多了... – 2013-04-20 19:17:21

+0

你只是把它做得更大。我不敢相信我没有尝试过。谢谢 – user2278269 2013-04-20 19:21:18

回答

3

调用frame.setVisible(true)您设置布局管理器和其他一切影响其“视觉”状态。

相关问题