【发布时间】:2015-12-04 13:58:16
【问题描述】:
我正在尝试用 java 制作一个扫雷游戏,但是当按下新游戏按钮时我的组件没有显示出来。这是整个代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package campominado;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author Felipe
*/
public class MainUI extends javax.swing.JFrame {
/**
* Creates new form MainUI
*/
public MainUI() {
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() {
jMenuBar1 = new javax.swing.JMenuBar();
file = new javax.swing.JMenu();
novoJogo = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
file.setText("File");
novoJogo.setText("Novo Jogo");
novoJogo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
novoJogoActionPerformed(evt);
}
});
file.add(novoJogo);
jMenuBar1.add(file);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 277, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void novoJogoActionPerformed(java.awt.event.ActionEvent evt) {
JPanel area = new JPanel();
JPanel areaJogo = new JPanel();
JPanel areaMenu = new JPanel();
this.add(area);
area.setLayout(new BorderLayout());
area.add(areaMenu, BorderLayout.NORTH);
area.add(areaJogo, BorderLayout.SOUTH);
JLabel linhas, colunas, minas;
linhas = new JLabel("Linhas:");
colunas = new JLabel("Colunas:");
minas = new JLabel("Minas:");
JTextField lin, col, min;
lin = new JTextField();
col = new JTextField();
min = new JTextField();
areaMenu.setLayout(new FlowLayout());
areaMenu.add(linhas);
areaMenu.add(lin);
areaMenu.add(colunas);
areaMenu.add(col);
areaMenu.add(minas);
areaMenu.add(min);
areaMenu.add(new JButton("Iniciar"));
areaMenu.validate();
areaMenu.repaint();
System.out.println("teste");
}
/**
* @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(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new MainUI().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JMenu file;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem novoJogo;
// End of variables declaration
}
关于一个相关问题:在此方法开始工作后,我计划使用网格布局上的许多按钮为 OK 按钮创建一个侦听器,以使用游戏的单元格填充第二个面板。有没有更好的方法可以做到这一点,或者我的想法很好?
【问题讨论】:
-
你确定这个方法会被调用吗?你能发布链接“新游戏”按钮的代码吗?
-
你的程序打印“teste”吗? System.out.println("teste");
-
假设
this与您的游戏窗口相关,那么LayoutManager安装在那里? - 我问是因为this.add(area);没有布局参数。使用默认 -null布局 - 窗口按其绝对坐标排列控件,这意味着面板area将被置于 0、0(左上角)。这是一个疯狂的猜测;请发布您的完整代码或更好的代码,创建一个Minimal, Complete, and Verifiable example!... -
是的,程序会打印“test”。其余代码全部由 netbeans 自动生成,并在整个过程中进行了后期编辑。
标签: java swing repaint minesweeper