我似乎无法将我的面板组件显示在框架上。有人可以帮忙吗? 我给了我一个空白的JFrame。没有其他组件。
import javax.swing.JPanel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Main.java
*
* Created on 20 Feb 2012, 6:51:55 PM
*/
/**
*
* @author User
*/
public class Main extends javax.swing.JFrame {
NewGameSettings ng = new NewGameSettings();
/** Creates new form Main */
public Main() {
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() {
newgame = new javax.swing.JButton();
loadgame = new javax.swing.JButton();
exit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Java game");
setResizable(false);
newgame.setFont(new java.awt.Font("Arial", 0, 18));
newgame.setForeground(new java.awt.Color(0, 0, 255));
newgame.setText("New Game");
newgame.setBorder(null);
newgame.setBorderPainted(false);
newgame.setContentAreaFilled(false);
newgame.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
newgame.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newgameActionPerformed(evt);
}
});
loadgame.setFont(new java.awt.Font("Arial", 0, 18));
loadgame.setForeground(new java.awt.Color(0, 0, 255));
loadgame.setText("Load Game");
loadgame.setBorder(null);
loadgame.setBorderPainted(false);
loadgame.setContentAreaFilled(false);
loadgame.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
loadgame.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loadgameActionPerformed(evt);
}
});
exit.setFont(new java.awt.Font("Arial", 0, 18));
exit.setForeground(new java.awt.Color(0, 0, 255));
exit.setText("Exit");
exit.setBorder(null);
exit.setBorderPainted(false);
exit.setContentAreaFilled(false);
exit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(527, 527, 527)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(loadgame)
.addComponent(newgame)))
.addGroup(layout.createSequentialGroup()
.addGap(570, 570, 570)
.addComponent(exit)))
.addContainerGap(580, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {loadgame, newgame});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(157, 157, 157)
.addComponent(newgame)
.addGap(164, 164, 164)
.addComponent(loadgame)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 252, Short.MAX_VALUE)
.addComponent(exit)
.addGap(108, 108, 108))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {loadgame, newgame});
pack();
}// </editor-fold>
private void newgameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.getContentPane().removeAll();
this.getContentPane().add(ng);
this.invalidate(); //I dont know how it isnt working
}
private void loadgameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void exitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
/**
* @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.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 Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton exit;
private javax.swing.JButton loadgame;
private javax.swing.JButton newgame;
// End of variables declaration
}
面板............
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewGameSettings.java
*
* Created on 20 Feb 2012, 8:32:08 PM
*/
/**
*
* @author User
*/
public class NewGameSettings extends javax.swing.JPanel {
/** Creates new form NewGameSettings */
public NewGameSettings() {
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() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();//a
username = new javax.swing.JTextField();//b
password = new javax.swing.JPasswordField();//k
eco = new javax.swing.JRadioButton();
engi = new javax.swing.JRadioButton();
gen = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jLabel1.setText("Username");
jLabel2.setText("Password");
buttonGroup1.add(eco);
eco.setText("Economist");
eco.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ecoActionPerformed(evt);
}
});
buttonGroup1.add(engi);
engi.setText("Engineer");
engi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
engiActionPerformed(evt);
}
});
buttonGroup1.add(gen);
gen.setText("General");
jButton1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
jButton1.setForeground(new java.awt.Color(0, 0, 255));
jButton1.setText("ready");
jButton1.setBorderPainted(false);
jButton1.setContentAreaFilled(false);
jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(314, 314, 314)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(145, 145, 145)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(eco)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(engi)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(gen))
.addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
.addComponent(password)))
.addGroup(layout.createSequentialGroup()
.addGap(1037, 1037, 1037)
.addComponent(jButton1)))
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(107, 107, 107)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(99, 99, 99)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(eco)
.addComponent(engi)
.addComponent(gen))
.addGap(380, 380, 380)
.addComponent(jButton1)
.addContainerGap(45, Short.MAX_VALUE))
);
}// </editor-fold>
private void ecoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null,"With the economist class you get 25% intrest income on money in the bank ","Economist advantages ",JOptionPane.INFORMATION_MESSAGE);
}
private void engiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JRadioButton eco;
private javax.swing.JRadioButton engi;
private javax.swing.JRadioButton gen;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPasswordField password;
private javax.swing.JTextField username;
// End of variables declaration
}
任何人都可以帮我解决这个问题吗?请
答案 0 :(得分:0)
您是否将NameGameSettings面板添加到父JFrame?如果不是这可能是问题。