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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:45:14+00:00 2026-06-09T12:45:14+00:00

I have this gui; and when the height is not big enough the panes

  • 0

I have this gui; and when the height is not big enough the panes will overlap each other. I have to set it at least 200, so I can completely see the two rows; but when it is set at 200, then I have like a big empty row at the end, and I don’t want that. How could I fix this? Thanks.

import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {

    JButton panicButton;
    JButton dontPanic;
    JButton blameButton;
    JButton newsButton;
    JButton mediaButton;
    JButton saveButton;
    JButton dontSave;

    public MyFrame() {
        super("Crazy App");
        setSize(400, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel row1 = new JPanel();
        panicButton = new JButton("Panic");
        dontPanic = new JButton("No Panic");
        blameButton = new JButton("Blame");
        newsButton = new JButton("News");
        //adding first row
        GridLayout grid1 = new GridLayout(4, 2, 10, 10);
        setLayout(grid1);
        FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
        row1.setLayout(flow1);
        row1.add(panicButton);
        row1.add(dontPanic);
        row1.add(blameButton);
        row1.add(newsButton);
        add(row1);
        //adding second row
        JPanel row2 = new JPanel();
        mediaButton = new JButton("Blame");
        saveButton = new JButton("Save");
        dontSave = new JButton("No Save");
        GridLayout grid2 = new GridLayout(3, 2, 10, 10);
        setLayout(grid2);
        FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
        row2.setLayout(flow2);        
        row2.add(mediaButton);
        row2.add(saveButton);
        row2.add(dontSave);
        add(row2);       

        setVisible(true);       

    }

    public static void main(String[] args) {
        MyFrame frame = new MyFrame();
    }   
}
  • 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-09T12:45:15+00:00Added an answer on June 9, 2026 at 12:45 pm
    1. The original code set the layout for one panel on two separate occasions. For clarity, set it once in the constructor.
    2. The 2nd layout specified 3 rows
    3. Call pack() on the top-level container to have the GUI reduce to the minum sze needed for the components.

    End result

    enter image description here


    import java.awt.FlowLayout;
    import javax.swing.*;
    import java.awt.*;
    
    public class MyFrame17 extends JFrame {
    
        JButton panicButton;
        JButton dontPanic;
        JButton blameButton;
        JButton newsButton;
        JButton mediaButton;
        JButton saveButton;
        JButton dontSave;
    
        public MyFrame17() {
            super("Crazy App");
            setLayout(new GridLayout(2, 2, 10, 10));
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel row1 = new JPanel();
            panicButton = new JButton("Panic");
            dontPanic = new JButton("No Panic");
            blameButton = new JButton("Blame");
            newsButton = new JButton("News");
            FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
            row1.setLayout(flow1);
            row1.add(panicButton);
            row1.add(dontPanic);
            row1.add(blameButton);
            row1.add(newsButton);
            add(row1);
            //adding second row
            JPanel row2 = new JPanel();
            mediaButton = new JButton("Blame");
            saveButton = new JButton("Save");
            dontSave = new JButton("No Save");
            FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
            row2.setLayout(flow2);        
            row2.add(mediaButton);
            row2.add(saveButton);
            row2.add(dontSave);
            add(row2);       
    
            pack();
            setVisible(true);       
        }
    
        public static void main(String[] args) {
            MyFrame17 frame = new MyFrame17();
        }   
    }
    

    Further tips

    1. Don’t extend frame, just use an instance of one.
    2. Build the entire GUI in a panel which can then be added to a frame, applet, dialog..
    3. When developing test classes, give them a more sensible name than MyFrame. A good word to add is Test, then think about what is being tested. This is about the layout of buttons, so ButtonLayoutTest might be a good name.
    4. GUIs should be started on the EDT.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a GUI have a slider, this slider's value will be
i have this GUI screen shots from the design team which i needs to
I have designed this gui in netBeans which has a canvas , a couple
I have a GUI program, which would call a cmd in this GUI program.
I had a huge file for creating this GUI and I have shortened it
Hello Guys! I started learning python GUI development. Let's say I have this script:
I have a class like this class GUI : public QWidget, public QThread When
I have a GUI program with one Exit button. When clicking this button, it
I have to play sounds on GUI events, like clicking buttons etc. For this
I have 2 Classes. GUI (with main method in it) Parser (this reads files(csv),

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.