Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Add Panels To JFrame

  1. #1
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Add Panels To JFrame

    Hello - I have a main jframe that I am using to launch a secondary jframe - now my secondary jframe launches as I would expect and shows the header info, however the left panel (or the right panel which I have left out) is not being added for some reason.

    I am using NETBEANS - and this is my code for the JFrame

    What do I need to add so that the two panels will be added?
    package gametest;
     
    import java.awt.Color;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
     
    public class JFrameGamePlay extends javax.swing.JFrame {
     
        String playerimage;
        String cpuimage;
        String BattlesWon = "0";
        String BattlesLost = "0";
        String Gold = "0";
        String PlayerLevel = "1";
     
        public JFrameGamePlay() {
            initComponents();
        }
     
        public JFrameGamePlay(String playername, String playerselected) {
            initComponents();
            JPanel leftpanel = new JPanel();
            JPanel rightpanel = new JPanel();
            JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftpanel, rightpanel);
     
            pnlPlayerInfo.setBackground(Color.WHITE);
     
                JLabel lblPlayer = new JLabel(new ImageIcon("/resources/House.png"));
                leftpanel.add(lblPlayer);
                leftpanel.validate();
                leftpanel.repaint();
                leftpanel.setVisible(true);
     
     
            //Setting variables
            txtPlayerName.setText(playername);
            txtPlayerName.setEditable(false);
            txtBattlesWon.setText(BattlesWon);
            txtBattlesWon.setEditable(false);
            txtBattlesLost.setText(BattlesLost);
            txtBattlesLost.setEditable(false);
            txtGold.setText(Gold); 
            txtGold.setEditable(false);
            txtPlayerLevel.setText(PlayerLevel);
            txtPlayerLevel.setEditable(false);
        }
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            pnlPlayerInfo = new javax.swing.JPanel();
            lblPlayerName = new javax.swing.JLabel();
            txtPlayerName = new javax.swing.JTextField();
            lblGold = new javax.swing.JLabel();
            txtGold = new javax.swing.JTextField();
            lblPlayerLevel = new javax.swing.JLabel();
            txtPlayerLevel = new javax.swing.JTextField();
            lblBattlesWon = new javax.swing.JLabel();
            txtBattlesWon = new javax.swing.JTextField();
            lblBattlesLost = new javax.swing.JLabel();
            txtBattlesLost = new javax.swing.JTextField();
            pnlPlayer = new javax.swing.JPanel();
            pnlCPU = new javax.swing.JPanel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            pnlPlayerInfo.setBackground(new java.awt.Color(255, 255, 255));
     
            lblPlayerName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            lblPlayerName.setLabelFor(txtPlayerName);
            lblPlayerName.setText("Player Name:");
            lblPlayerName.setToolTipText("");
     
            txtPlayerName.setText("jTextField1");
     
            lblGold.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            lblGold.setLabelFor(txtGold);
            lblGold.setText("Gold:");
     
            txtGold.setText("jTextField1");
     
            lblPlayerLevel.setText("Player Level:");
     
            txtPlayerLevel.setText("jTextField1");
     
            lblBattlesWon.setLabelFor(txtBattlesWon);
            lblBattlesWon.setText("Battles Won:");
            lblBattlesWon.setToolTipText("");
     
            txtBattlesWon.setText("JTextField1");
            txtBattlesWon.setToolTipText("");
            txtBattlesWon.setName("txtBattlesWon"); // NOI18N
     
            lblBattlesLost.setLabelFor(txtBattlesLost);
            lblBattlesLost.setText("Battles Lost:");
     
            txtBattlesLost.setText("jTextField1");
     
            javax.swing.GroupLayout pnlPlayerInfoLayout = new javax.swing.GroupLayout(pnlPlayerInfo);
            pnlPlayerInfo.setLayout(pnlPlayerInfoLayout);
            pnlPlayerInfoLayout.setHorizontalGroup(
                pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlPlayerInfoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtPlayerName)
                        .addComponent(lblPlayerName, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                    .addGap(36, 36, 36)
                    .addGroup(pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtGold)
                        .addComponent(lblGold, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(34, 34, 34)
                    .addGroup(pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblPlayerLevel)
                        .addComponent(txtPlayerLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(40, 40, 40)
                    .addGroup(pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblBattlesWon)
                        .addComponent(txtBattlesWon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(44, 44, 44)
                    .addGroup(pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtBattlesLost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblBattlesLost))
                    .addContainerGap(18, Short.MAX_VALUE))
            );
            pnlPlayerInfoLayout.setVerticalGroup(
                pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlPlayerInfoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblPlayerName)
                        .addComponent(lblGold)
                        .addComponent(lblPlayerLevel)
                        .addComponent(lblBattlesWon)
                        .addComponent(lblBattlesLost))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(pnlPlayerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtPlayerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtGold, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtPlayerLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtBattlesWon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtBattlesLost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(15, Short.MAX_VALUE))
            );
     
            pnlPlayer.setBorder(new javax.swing.border.MatteBorder(null));
     
            javax.swing.GroupLayout pnlPlayerLayout = new javax.swing.GroupLayout(pnlPlayer);
            pnlPlayer.setLayout(pnlPlayerLayout);
            pnlPlayerLayout.setHorizontalGroup(
                pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 141, Short.MAX_VALUE)
            );
            pnlPlayerLayout.setVerticalGroup(
                pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 244, Short.MAX_VALUE)
            );
     
            pnlCPU.setBorder(new javax.swing.border.MatteBorder(null));
     
            javax.swing.GroupLayout pnlCPULayout = new javax.swing.GroupLayout(pnlCPU);
            pnlCPU.setLayout(pnlCPULayout);
            pnlCPULayout.setHorizontalGroup(
                pnlCPULayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 141, Short.MAX_VALUE)
            );
            pnlCPULayout.setVerticalGroup(
                pnlCPULayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(pnlPlayerInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(pnlPlayer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlCPU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(pnlPlayerInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(pnlPlayer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(pnlCPU, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(0, 84, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        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(JFrameGamePlay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(JFrameGamePlay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(JFrameGamePlay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(JFrameGamePlay.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 JFrameGamePlay().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JLabel lblBattlesLost;
        private javax.swing.JLabel lblBattlesWon;
        private javax.swing.JLabel lblGold;
        private javax.swing.JLabel lblPlayerLevel;
        private javax.swing.JLabel lblPlayerName;
        private javax.swing.JPanel pnlCPU;
        private javax.swing.JPanel pnlPlayer;
        private javax.swing.JPanel pnlPlayerInfo;
        private javax.swing.JTextField txtBattlesLost;
        private javax.swing.JTextField txtBattlesWon;
        private javax.swing.JTextField txtGold;
        private javax.swing.JTextField txtPlayerLevel;
        private javax.swing.JTextField txtPlayerName;
        // End of variables declaration                   
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Add Panels To JFrame

    I am using NETBEANS
    I see a lot of GUI code that was built by NB. Having that NB code in the class makes it hard to work with outside of the NB IDE.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Add Panels To JFrame

    I would stay away from GUI designers in general. It is cleaner and you'll learn more by laying out the various components yourself. And it's poor form to extend JFrame. Just use an instance of it and work with that. Whenever possible, prefer composition instead of inheritance.

    Regards,
    Jim

  4. #4
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Add Panels To JFrame

    Quote Originally Posted by Norm View Post
    I see a lot of GUI code that was built by NB. Having that NB code in the class makes it hard to work with outside of the NB IDE.
    Yes, I am using Netbeans GUI to write this code. - I just did a straight copy/paste of the class to ensure I did not omit any relevant code that could be causing my issue.

    --- Update ---

    Quote Originally Posted by jim829 View Post
    I would stay away from GUI designers in general. It is cleaner and you'll learn more by laying out the various components yourself. And it's poor form to extend JFrame. Just use an instance of it and work with that. Whenever possible, prefer composition instead of inheritance.

    Regards,
    Jim
    So do not launch a new JForm from my main JForm, I should for example, have the one main JForm and have 2 JPanels one with the initial layout, then when the button is pressed, 'hide" that layout and show the secondary layout?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Add Panels To JFrame

    There is code in the second constructor that does not look like it was added by the NB IDE. That will be a problem. Remove your code and
    Try re-creating the GUI by using only the IDE's GUI creator. It is not easy or sometimes even possible to merge your own code in with the code generated by NB.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Adding Panels to JFrame
    By jocdrew21 in forum AWT / Java Swing
    Replies: 13
    Last Post: September 6th, 2017, 05:55 AM
  2. Replies: 0
    Last Post: February 15th, 2013, 05:24 PM
  3. Replies: 5
    Last Post: February 15th, 2013, 05:01 PM
  4. How Do i add pictures to a jframe?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 21st, 2012, 12:34 PM
  5. adding or removing panels to panels
    By luisp88 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 04:37 PM