java - 当我在 JFrame 上更改 JPanel 组件时,我无法显示它

标签 java swing components jframe jpanel

我似乎无法将面板的组件显示到框架上。有人可以帮忙吗? 我只是给了我一个空白的 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
}

谁能帮我解决这个问题吗?请

最佳答案

您是否将 NameGameSettings 面板添加到父 JFrame 中?如果不是,这可能就是问题所在。

关于java - 当我在 JFrame 上更改 JPanel 组件时,我无法显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9366909/

相关文章:

java - 如何在 Swing 桌面应用程序中部署 JavaFX

java - 如何更改哪个组件正在监听事件

Java 正则表达式模式问题

java - 在 Android 应用程序中托管可执行文件

Java - 事件处理和反射(reflect)结果的最佳实践

java - 如何限制 JTextField 不接受超过 'x' 数量的输入

java - 情绪评分 Stanford Core NLP

java - 无法在 macOS 上打开文件 - FileNotFoundException,不允许操作

javascript - 根据 Vue 中的数据变化切换对象(使用 Vuex)

angular - 组件和指令有什么区别?