【发布时间】:2015-08-09 06:25:30
【问题描述】:
我有这个表格:
它有一个按钮、三个标签、三个文本字段和它们上方的内部框架。
这将是一个Nurikabe游戏,用户将输入矩阵大小msize简称,编号单元格(包含数字的单元格)nncells简称,以及一个编号单元格可以拥有的最大数量简称max。
我创建了一个 msize*msize 网格并填充了它的 nncells,我选择了哪些单元格?好吧,我随机选择它们,我用什么数字填充它们?区间 [1,max] 中的随机数。
我用 Netbeans Designer 创建了这个表单,这是整个 JFrame 文件的代码: 包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;
}
此代码不起作用!它没有在内部框架中显示任何东西。
我正在删除所有组件所以为了清除之前的新闻添加的内容,我最后调用了 validate 和 repaint。
我在 SO 上找到了this 问题,但是当我知道网格的大小时答案有效,我不知道它,因为用户会输入它。(也没有
那么问题出在哪里?
PS
当我多次按下按钮时,内部框架变大但没有任何内容时,我将它的布局设置为来自 netbeans 的 null 并在按钮中设置它的布局。
【问题讨论】:
-
@AndrewThompson 我通过 netbeans 设计器添加了事件和控件,我编写的唯一代码是发布的代码
-
您可以自己回答这个问题。方法如下:运行时问题的 MCVE 需要(至少)干净地编译,以便它可以运行。在您的环境中创建一个名为(或至少称为主类)
PrologTest的新项目。粘贴上面看到的代码。它编译干净吗?然后,一旦你对它进行了排序:它可以运行吗?它显示了问题吗?您尝试创建的是一个示例,可以让其他人轻松帮助 - 所以它必须是复制/粘贴/编译/运行查看问题。 -
@AndrewThompson 考虑到你在这里所经历的痛苦,我觉得有必要告诉你你的成功......
-
为什么要使用内部框架?这不是它的用途。