在 JFrame 上更改 JPanel 组件时无法显示它

Posted

技术标签:

【中文标题】在 JFrame 上更改 JPanel 组件时无法显示它【英文标题】:I can't display my JPanel components when I change it on a JFrame 【发布时间】:2012-03-11 03:14:04 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于在 JFrame 上更改 JPanel 组件时无法显示它的主要内容,如果未能解决你的问题,请参考以下文章

java swing编程问题:一个jframe中添加一个jpanel后,为jpanel添加一个滚动条,当jpanel中内容过多时滑动

如何更改 JFrame 中 JPanel 的大小?

JFrame 中的沙箱 JPanel

JPanel 未显示在 JFrame 中,但 JFrame 仍会更改大小

JPanel 在调整 Jframe 大小之前不会更新

鼠标在内部组件悬停时退出