Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:20:58+00:00 2026-06-12T12:20:58+00:00

I am making a game but whenever i run the second jFrame I have

  • 0

I am making a game but whenever i run the second jFrame I have to resize it in order to get the right size for the second jFrame, anyone know why?

here is the method in the first jFrame class that opens the second one:

  private void playButtonMouseClicked(java.awt.event.MouseEvent evt) {                                        
        if (playerOneNameText.getText().equals(""))
        {
        }

        if (playerTwoNameText.getText().equals(""))
        {
        }
        else{
        pOneName = playerOneNameText.getText();
        pTwoName = playerTwoNameText.getText();

        ChessBoardUI class1 = new ChessBoardUI(); // Creating object of Class1
        class1.setVisible(true);

        this.setVisible(false);
        }
    } 

here is the second jFrame class that is the one that I have to resize in order to ge tit right:

package chess;

public class ChessBoardUI extends javax.swing.JFrame {

    public ChessBoardUI() {
        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() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        blackTeamName = new javax.swing.JLabel();
        whiteTeamName = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);
        getContentPane().add(jLabel2);
        jLabel2.setBounds(21, 49, 0, 0);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/chess/board.jpg"))); // NOI18N
        getContentPane().add(jLabel3);
        jLabel3.setBounds(0, 30, 400, 400);

        jLabel1.setText("Black:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(400, 100, 34, 16);

        jLabel4.setText("White:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(400, 150, 38, 16);

        blackTeamName.setText("jLabel5");
        getContentPane().add(blackTeamName);
        blackTeamName.setBounds(400, 120, 41, 16);

        whiteTeamName.setText("jLabel5");
        getContentPane().add(whiteTeamName);
        whiteTeamName.setBounds(400, 170, 41, 16);

        pack();
    }// </editor-fold>

    /**
     * @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(ChessBoardUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChessBoardUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChessBoardUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChessBoardUI.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 ChessBoardUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel blackTeamName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel whiteTeamName;
    // End of variables declaration
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:20:59+00:00Added an answer on June 12, 2026 at 12:20 pm

    I read very quickly looking for a particular method.

    That method is:

    pack();
    

    This method in JFrame can be very useful but also very difficult to handle, you need to understand very well how to set the correct size of each component. Also with a “null” layout can be trivial to have all working.

    My advice is to forget to use

    setLayout(null)
    

    Because for my experience it give rarely better results than other layout.

    The best thing I can tell you is to use BoxLayout. With this layout you can precisely set locations and size for each Component. The trick is also that you can use rigidArea and Glue.

    your code can be:

    // This is a BoxLayout with top to bottom orientation, the trick is to nest many JPanel
    // with BoxLayout in both the direction to have all working
    // What I want to achieve is:
    //
    //     1. Having a main box top to bottom where I will put:
    //           - Top margin (a rigid area with dimension (0,MARGIN)
    //           - Main JPanel with BoxLayout and LINE_AXIS (left to right) orientation
    //           - Bottom margin (a rigid area like top margin)
    //
    //     2. In the main panel I will put:
    //           - Left Margin (a rigid area with dimensions (MARGIN,0)
    //           - A JPanel (leftPanel) Boxed top to bottom containing the things on the left that actually are jLabel3 and jLabel2
    //           - A little separator between the two panel, a rigid area (10,0) i.e.
    //           - A JPanel (rightPanel) Boxed top to bottom containing the remaining 4 JLabels
    //           - Right Margin (as left)
    //
    //     3. In rightPanel JPanel (BoxLayout.PAGE_AXIS, top to bottom) I will have:
    //           - a rigid area space to match the position that I want
    //           - the first label
    //           - a rigid area.. etc so on
    //     
    //     For each JLabel I must set all:
    //           - setPreferredSize(dimension)
    //           - setMinimumSize(dimension)
    //           - setMaximumSize(dimension)
    //
    //     Then, if you specify also the JFrame is better, but you can try to pack().
    //     BoxLayout take care of sizes, not exceeding maximum and not making it smaller than minimum. Yust try it and you will love it.
    
    getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS);
    
    JPanel main = new JPanel();
    main.setLayout(new BoxLayout(main, BoxLayout.LINE_AXIS));
    
    this.add(Box.createRigidArea(new Dimension(0,20))); // VERTICAL SPACE (top margin)
    this.add(main);
    this.add(Box.createRigidArea(new Dimension(0,20))); // VERTICAL SPACE (bottom margin)
    
    JPanel rightPanel = new JPanel();
    rightPanel.setLayout( new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
    JPanel leftPanel = new JPanel();
    leftPanel.setLayout( new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
    
    main.add(Box.createRigidArea(new Dimension(20,0))); // HORIZONTAL SPACE (left margin)
    main.add(leftPanel);
    main.add(Box.createRigidArea(new Dimension(10,0))); // HORIZONTAL SPACE (between the two)
    main.add(rightPanel);
    main.add(Box.createRigidArea(new Dimension(20,0))); // HORIZONTAL SPACE (right margin)
    
    // now you should have understood how it works, just try to fill the right and left panel with your labels. Remember to set preferredm, maximum and minimum sizes.
    

    I hope this is useful to you
    EDIT, i miss some “)”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making some game, and I have made it pretty much all, but not
I'm making a Asteroids game but I can not get to play more than
I've been making a game using the rokonandroid game engine, but I randomly get
I am making a game, so I want the JFrame to start maximized, but
I'm making a game in XNA, but since I have people with different operating
I built a slideshow/decision-making game in Flash but would like to try to redo
I'm using a specific game making framework but I think the question applies to
so i've had multiple problems with a tic tac toe game i'm making, but
I am making a game, and have a Game model and a User model.
In C# WinForms whenever making a small game here or there, I would write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.