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 6734953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:57:03+00:00 2026-05-26T10:57:03+00:00

I’ve been learning to hand write Swing, and have been using the Sun Doc

  • 0

I’ve been learning to hand write Swing, and have been using the Sun Doc tutorials. I am using a GridLayout and on my first go it made all the buttons fill the available space. On my second go I’ve implemented a second grid to hold some controls at the top. I’m sure this is something really easy but after reading and playing for ages I can’t get the second lower pane to fill with the buttons like it did on my first attempt. Help pls?

package layout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Game extends JFrame {
    GridLayout gameLayout = new GridLayout(6,6);
    JLabel score = new JLabel("Score:");

    public Game(String name) {
        super(name);
        setResizable(true);
    }

    public void addComponentsToPane(final Container pane) {
        final JPanel mainGameWindow = new JPanel();
        mainGameWindow.setLayout(gameLayout);
        JPanel controls = new JPanel();
        controls.setLayout(new GridLayout(2,4));

        //Add buttons to experiment with Grid Layout
        mainGameWindow.add(new JButton("sqrOne1"));
        mainGameWindow.add(new JButton("sqrOne2"));
        mainGameWindow.add(new JButton("sqrOne3"));
        mainGameWindow.add(new JButton("sqrOne4"));
        mainGameWindow.add(new JButton("sqrOne5"));
        mainGameWindow.add(new JButton("sqrOne6")); 
        mainGameWindow.add(new JButton("sqrTwo1"));
        mainGameWindow.add(new JButton("sqrTwo2"));
        mainGameWindow.add(new JButton("sqrTwo3"));
        mainGameWindow.add(new JButton("sqrTwo4"));
        mainGameWindow.add(new JButton("sqrTwo5"));
        mainGameWindow.add(new JButton("sqrTwo6")); 
        mainGameWindow.add(new JButton("sqrThree1"));
        mainGameWindow.add(new JButton("sqrThree2"));
        mainGameWindow.add(new JButton("sqrThree3"));
        mainGameWindow.add(new JButton("sqrThree4"));
        mainGameWindow.add(new JButton("sqrThree5"));
        mainGameWindow.add(new JButton("sqrThree6"));   
        mainGameWindow.add(new JButton("sqrFour1"));
        mainGameWindow.add(new JButton("sqrFour2"));
        mainGameWindow.add(new JButton("sqrFour3"));
        mainGameWindow.add(new JButton("sqrFour4"));
        mainGameWindow.add(new JButton("sqrFour5"));
        mainGameWindow.add(new JButton("sqrFour6"));    
        mainGameWindow.add(new JButton("sqrFive1"));
        mainGameWindow.add(new JButton("sqrFive2"));
        mainGameWindow.add(new JButton("sqrFive3"));
        mainGameWindow.add(new JButton("sqrFive4"));
        mainGameWindow.add(new JButton("sqrFive5"));
        mainGameWindow.add(new JButton("sqrFive6"));        
        mainGameWindow.add(new JButton("sqrSix1"));
        mainGameWindow.add(new JButton("sqrSix2"));
        mainGameWindow.add(new JButton("sqrSix3"));
        mainGameWindow.add(new JButton("sqrSix4"));
        mainGameWindow.add(new JButton("sqrSix5"));
        mainGameWindow.add(new JButton("sqrSix6"));

        //Add controls to set up horizontal and vertical gaps
        controls.add(new Label("Match pairs of Promient Tech Heads!"));
        controls.add(score);
        controls.add(new JButton("Solve"));
        controls.add(new JButton("Scrabble")); 

        //Process the Apply gaps button press    
        gameLayout.layoutContainer(mainGameWindow);

        pane.add(controls, BorderLayout.NORTH);
        pane.add(new JSeparator(), BorderLayout.CENTER);        
        pane.add(mainGameWindow, BorderLayout.SOUTH);
    }

    private void setLabelText(String text)
    {
        score.setText(text);
        score.paintImmediately(score.getVisibleRect());
    }


    private static void createAndShowGUI() {
        //Create and set up the window.
        Game frame = new Game("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set up the content pane.
        frame.addComponentsToPane(frame.getContentPane());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setSize(600,600);
        frame.setMinimumSize(new Dimension(400, 400));            
    }

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
  • 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-05-26T10:57:03+00:00Added an answer on May 26, 2026 at 10:57 am

    I think I found the problem for future readers, the buttons need to be declared as:

    JButton btnOne1 = new JButton("btnOne1");
    btnOne1.setPreferredSize(new Dimension(100, 100));
    

    and then

    mainGameWindow.add(btnOne1);
    

    This then makes them fill the window they are in.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
this is what i have right now Drawing an RSS feed into the php,

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.