2012-12-22 3 views
0

enter image description here net beans 프로그램을 사용하여 응용 프로그램 디자인을 만들었습니다. 스도쿠와 비슷한 30 * 30 그리드 레이아웃을 추가하고 싶습니다. 사실 맵의 30 * 30 그리기를 시도하고 있습니다. 그러나, 나는 내 코드에 그 견해를 추가 할 수 없다. 나는 많은 장소를 찾았지만 대답을 찾을 수 없다. 나는 당신의 아이디어에 열려있다. 누가이 주제에서 저를 도울 수 있습니까? 고맙습니다 !NetBeans IDE에서 그리드 레이아웃을 추가 할 때 문제가 발생했습니다.

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 


import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 
import java.awt.GridLayout; 

import javax.swing.JTextField; 


public class NewJFrame extends javax.swing.JFrame { 

/** 
* Creates new form NewJFrame 
*/ 
public NewJFrame() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    getDirectionButton = new javax.swing.JButton(); 
    questionTitle = new java.awt.Label(); 
    questionText = new javax.swing.JTextField(); 
    jScrollPane1 = new javax.swing.JScrollPane(); 
    directionDescriptionArea = new javax.swing.JTextArea(); 
    walkingManButton = new javax.swing.JButton(); 
    java.awt.Panel mapPanel = new java.awt.Panel(); 

    jButton1 = new javax.swing.JButton(); 


    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setTitle("DSL For Streets\n"); 
    setPreferredSize(new java.awt.Dimension(1365, 730)); 

    getDirectionButton.setText("Get Direction"); 

    questionTitle.setFont(new java.awt.Font("Dialog", 3, 12)); // NOI18N 
    questionTitle.setText("Please enter your question"); 

    directionDescriptionArea.setColumns(20); 
    directionDescriptionArea.setRows(5); 
    jScrollPane1.setViewportView(directionDescriptionArea); 

    walkingManButton.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N 
    walkingManButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("walk.png"))); // NOI18N 
    walkingManButton.setText("Get Suggested Route"); 
    walkingManButton.setFocusable(false); 

    jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("map.png"))); // NOI18N 


    cells = new int[ROWS][COLS]; 
    tfCells = new JTextField[ROWS][COLS]; // allocate JTextField array 

    // Container cp = getContentPane(); 
    //cp.setLayout(new GridLayout(ROWS, COLS)); 
mapPanel.setLayout(new GridLayout(ROWS, COLS)); 
    // Create 9x9 JTextFields and place on the GridLayout 
    for (int row = 0; row < ROWS; row++) { 
     for (int col = 0; col < COLS; col++) { 
      tfCells[row][col] = new JTextField(); // allocate element of array 
     mapPanel. add(tfCells[row][col]); // ContentPane adds JTextField 
      int number = puzzle[row][col]; 

      tfCells[row][col].setText(""); // empty 
      tfCells[row][col].setHorizontalAlignment(JTextField.CENTER); 
      tfCells[row][col].setFont(new Font("Monospaced", Font.BOLD, 20)); 
     } 
    } 

    javax.swing.GroupLayout mapPanelLayout = new javax.swing.GroupLayout(mapPanel); 
    mapPanel.setLayout(mapPanelLayout); 
    mapPanelLayout.setHorizontalGroup(
     mapPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(mapPanelLayout.createSequentialGroup() 
      .addGap(181, 181, 181) 
      .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(239, Short.MAX_VALUE)) 
     .addGroup(mapPanelLayout.createSequentialGroup() 

      .addGap(0, 0, Short.MAX_VALUE)) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(36, 36, 36) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) 
       .addComponent(getDirectionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(questionTitle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(questionText, javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jScrollPane1) 
       .addComponent(walkingManButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addComponent(mapPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(46, 46, 46)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(39, 39, 39) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(questionTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(18, 18, 18) 
        .addComponent(questionText, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(18, 18, 18) 
        .addComponent(getDirectionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(25, 25, 25) 
        .addComponent(walkingManButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)) 
       .addComponent(mapPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) 
    ); 

    pack(); 
}// </editor-fold> 

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new NewJFrame().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JTextArea directionDescriptionArea; 
private javax.swing.JButton getDirectionButton; 
private javax.swing.JButton jButton1; 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JTextField jTextField3; 
private javax.swing.JTextField questionText; 
private java.awt.Label questionTitle; 
private javax.swing.JButton walkingManButton; 
private javax.swing.JTextField a[]=new JTextField[30]; 
// End of variables declaration 

public static final int ROWS = 9; // ROWS by COLS cells 
public static final int COLS = 9; 
public static final int CELL_SIZE = 10; // Cell width/height 
public static final int CANVAS_WIDTH = CELL_SIZE * COLS; 
public static final int CANVAS_HEIGHT = CELL_SIZE * ROWS; 
private int[][] cells; 
private JTextField[][] tfCells; 
private int[][] puzzle =new int[9][9]; 

}

답변

2

당신이 넷빈즈를 사용하는 경우, 당신은 GUI 빌더 것을 할 수 있습니다. 디자인보기로 이동하여이 (일부 JPanel)에 사용할 기본 컨테이너를 마우스 오른쪽 버튼으로 클릭하고 컨텍스트 메뉴에서 "레이아웃 설정"옵션으로 이동하여 "그리드 레이아웃"을 선택하십시오. 속성을 편집하려면 네비게이터 창 (일반적으로 왼쪽)으로 이동하여 해당 컨테이너를 볼 때까지 구성 요소를 탐색하고 접은 다음 GridLayout을 선택합니다. 이제 속성 창 (일반적으로 오른쪽)에서 행 및 열과 같은 일부 속성을 변경할 수 있습니다. 그런 다음 다른 구성 요소를 해당 컨테이너 위에 끕니다.

설명에 따르면 실제 찾고있는 것이 JTable인데, 데이터를 그리드에 표시 할 수있는 구성 요소입니다.