2013-04-20 25 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)您設置佈局管理器和其他一切影響其「視覺」狀態。

相關問題