NetBeans IDE问题添加网格布局 [英] NetBeans IDE issue adding grid layout

查看:125
本文介绍了NetBeans IDE问题添加网格布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Net Beans程序创建了我的应用程序设计.我想添加一个类似于数独的30 * 30网格布局.实际上,我正在尝试为地图绘制30 * 30的正方形.但是,我无法将该视图添加到我的代码中.我搜索了很多地方,但找不到答案.我愿意接受您的想法.有人可以在这个问题上帮助我吗?谢谢 !

I have created a design of my application with net beans programme . I would like to add a 30*30 grid layout something similar to sudoku .Actually I am trying to draw a 30*30 of squares for map. However , I am not able to add that view to my code .I have searched lots of places but I am not able to find an answer .I am open to your ideas. Could anyone help me in this subject please ? Thank you !

/*
* 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];

}

推荐答案

如果您使用的是Netbeans,则可以使用GUI Builder.只需转到设计视图",右键单击要用于此的基础容器(某些JPanel),转到上下文菜单中的设置布局"选项,然后选择网格布局".要编辑其属性,请转到导航器"窗口(通常在左侧),浏览组件,直到看到有问题的容器,将其折叠并选择GridLayout.现在,您可以在属性窗口(通常在右侧)上更改某些属性,例如行和列.然后,将其他组件拖动到该容器的顶部.

If you're using Netbeans, you can do that with the GUI Builder. Simply go to the Design View, right click on the underlying container you're going to use for this (some JPanel), go to "Set Layout" option in the context menu and pick "Grid Layout". To edit its properties, go to the Navigator window (usually on the left), browse your components until you see the container in question, collapse it and select the GridLayout. You can now change some properties, like the rows and columns, on the properties window (usually on the right). Then, you drag other components on top of that container.

但是,根据您的描述,我想知道您真正要查找的是 JTable ,该组件可让您在网格中显示数据.

Though, by your description, I wonder if what you're really looking for is a JTable, which is a component that lets you display your data in a grid.

这篇关于NetBeans IDE问题添加网格布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆