无法在java netbeans

时间:2015-05-27 09:06:38

标签: java swing netbeans

我有这样的表格:

enter image description here

有一个按钮,三个标签,三个文本字段和上面的内部框架。

这将是一个Nurikabe游戏,用户将输入矩阵大小msize为简短的编号单元格数(包含数字的单元格)nncells简称,最大数字为a编号的单元格可以简称max

我创建一个msize * msize的网格并填充它的nncells,我选择哪些单元格?我随机选择它们,我用它填充了多少个?区间[1,max]中的随机数。

我使用Netbeans Designer创建了这个表单,这是整个JFrame文件的代码:     package javaapplication1;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class PrologTest extends javax.swing.JFrame {

public PrologTest() {
    initComponents();
}
private void initComponents()
{
    jLabel1 = new javax.swing.JLabel();
    matrixsize = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    numcells = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    maximumnumber = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jButton3 = new javax.swing.JButton();
    Nurikabe = new javax.swing.JInternalFrame();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(255, 0, 0));

    jLabel1.setText("matrix size");
    jLabel1.setToolTipText("");

    matrixsize.setToolTipText("");
    matrixsize.setName("matrixsize"); // NOI18N

    jButton1.setBackground(new java.awt.Color(255, 255, 255));
    jButton1.setText("validate");
    jButton1.setToolTipText("");

    jButton2.setText("show solutions");
    jButton2.setToolTipText("");

    numcells.setToolTipText("");
    numcells.setName("numcells"); // NOI18N

    jLabel2.setText("number of numbered cells");
    jLabel2.setToolTipText("");

    maximumnumber.setToolTipText("");
    maximumnumber.setName("maximumnumber"); // NOI18N

    jLabel3.setText("maximum number for a cell");
    jLabel3.setToolTipText("");

    jButton3.setText("create");
    jButton3.setToolTipText("");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    Nurikabe.setVisible(true);
    Nurikabe.getContentPane().setLayout(new java.awt.GridLayout(1, 0));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(numcells)
                .addComponent(maximumnumber, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                .addComponent(matrixsize))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
            .addContainerGap())
        .addComponent(Nurikabe)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(Nurikabe, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(matrixsize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton3))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton2)
                    .addGap(37, 37, 37))
                .addGroup(layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(numcells, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(maximumnumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
    );

    pack();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
    Nurikabe.removeAll();
    int matsize=Integer.parseInt(matrixsize.getText());
    int numberedcells=Integer.parseInt(numcells.getText());
    int maximumcellnum=Integer.parseInt(maximumnumber.getText());
    Random r=new Random();           
    Cells=new JButton[matsize][matsize];
    Nurikabe.getContentPane().setLayout(new GridLayout(matsize,matsize));            
    for(int i=0;i<matsize;i++)                            
    {
        for(int j=0;j<matsize;j++)    
        {
            Cells[i][j]=new JButton();
            Cells[i][j].setContentAreaFilled(false);  
            Cells[i][j].setBackground(Color.white);
            final JButton Cell=Cells[i][j];
            Cells[i][j].addMouseListener(new java.awt.event.MouseAdapter() 
            {
                @Override
                public void mousePressed(java.awt.event.MouseEvent evt) 
                {
                    if(SwingUtilities.isRightMouseButton(evt))
                    {
                        Cell.setBackground(Color.green);
                    }
                    else if(SwingUtilities.isLeftMouseButton(evt))
                    {
                        Cell.setBackground(Color.blue);
                    }                                 
                }
            });  
            Nurikabe.add(Cells[i][j]);
        }
    }
    for(int i=0;i<numberedcells;i++)    
    {
        int k=r.nextInt(matsize-1)+1,l=r.nextInt(matsize-1)+1;
        String text=String.valueOf(r.nextInt(maximumcellnum));
        Cells[k][l].setText(text);
        Cells[k][l].setBackground(Color.green);                
    }            
    Nurikabe.validate();
    Nurikabe.pack();
    Nurikabe.repaint();                
}
public static void main(String[] args)
{
    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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(PrologTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }                
    java.awt.EventQueue.invokeLater(() -> {
        new PrologTest().setVisible(true);
    });
}
private javax.swing.JButton Cells[][];
// Variables declaration - do not modify                     
private javax.swing.JInternalFrame Nurikabe;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField matrixsize;
private javax.swing.JTextField maximumnumber;
private javax.swing.JTextField numcells;
}

这段代码不起作用!!它没有显示内部框架中的东西。

我删除了所有的权限因此,为了清除之前的新闻所添加的内容,我最后调用了验证并重新绘制。

我在SO上发现了this的问题,但当我知道网格的大小时,答案就会起作用,我不知道用户是否会输入它。(也没有&#39; t < / p>

那么问题是什么?

PS

当我按下按钮多次,内部框架变大但没有任何内容时,我将它的布局设置为null,并在按钮中设置它的布局。

1 个答案:

答案 0 :(得分:2)

而不是n nurikabe = new JPanel(new GridLayout(1, 0)); ,只需使用nurikabe.setLayout(new GridLayout(matsize, matsize)); 。用以下内容初始化:

cells

在创建过程中,使用:

nurikabe

注意:

  • 变量名称应以小写字母开头(parseIntrevalidate ...)。
  • 使用用户输入中的validate检查例外情况。
  • 通常,请致电setLayout而不是repaint。在这里,您不需要其中任何一个,因为您致电GroupLayout
  • 无需再拨打{{1}}。
  • 重要:GUI构建器通常具有错误的编码标准,因为它以编程方式创建GUI并具有处理代码的自己的方式。对于一个人来说,以后阅读它会花费更多的时间(不,不抱怨{{1}})。我建议你学会自己编写GUI代码。即使你现在拥有的也不是那么难。