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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:20:25+00:00 2026-05-15T12:20:25+00:00

I have a sample code : import java.awt.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class

  • 0

I have a sample code :

import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class AWT extends JFrame {

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setPreferredSize(new Dimension(600, 450));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.green.darker());

        Button btn_1 = new Button("Button 1");
        btn_1.setBackground(Color.green.darker());
        btn_1.setSize(40, 100);
        Button btn_2 = new Button("Button 2");
        btn_2.setBackground(Color.green.darker());
        btn_2.setSize(40, 100);
        Button btn_3 = new Button("Button 3");
        btn_3.setBackground(Color.green.darker());
        btn_3.setSize(40, 100);

        JPanel players = new JPanel(new GridLayout(1, 3));
        players.add(btn_1);
        players.add(btn_2);
        players.add(btn_3);
        players.setBackground(Color.green.darker());
        players.setPreferredSize(new Dimension(450, 80));

        JPanel game = new JPanel();
        game.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        game.setBackground(Color.green);
        game.setPreferredSize(new Dimension(600, 370));

        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        frame.setContentPane(content);
        content.add(players, BorderLayout.NORTH);
        content.add(game, BorderLayout.SOUTH);
        frame.pack();
 frame.setVisible(true);
    }
}

This as a result gives me such a window :

alt text http://img710.imageshack.us/img710/1573/21672037.gif

, while it should result in something more like this :

alt text http://img686.imageshack.us/img686/8948/44650673.gif

Why it is not rendering in this way ?

  • 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-15T12:20:26+00:00Added an answer on May 15, 2026 at 12:20 pm

    GridLayout ignores the buttons’ preferred size. Replace

    JPanel players = new JPanel(new GridLayout(1, 3));
    

    with

    JPanel players = new JPanel(); // default FlowLayout
    

    Addendum: Here’s a related example of letting the layout and default preferred sizes do the work. By specifying as few constraints as possible, the appearance adjusts to each platform’s look & feel when pack() is invoked.

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.BevelBorder;
    
    public class AWT extends JFrame {
    
        public static void main(String[] args) {
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel players = new JPanel();
            players.add(new GameButton("Button 1"));
            players.add(new GameButton("Button 2"));
            players.add(new GameButton("Button 3"));
            players.setBackground(GameButton.color);
    
            JPanel game = new JPanel();
            game.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            game.setBackground(Color.green);
            game.setPreferredSize(new Dimension(600, 450));
    
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
            frame.setContentPane(content);
            content.add(players, BorderLayout.NORTH);
            content.add(game, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        }
    
        private static class GameButton extends JButton {
    
            private static final Color color = Color.green.darker();
            private static final Font font = new Font("SanSerif", Font.BOLD, 20);
            private String name;
    
            public GameButton(String name) {
                super(name);
                this.setBackground(color);
                this.setFont(font);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 468k
  • Answers 469k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When it can be broken up in modules, go for… May 16, 2026 at 2:32 am
  • Editorial Team
    Editorial Team added an answer It looks like the page uses the showMessage jQuery plugin.… May 16, 2026 at 2:32 am
  • Editorial Team
    Editorial Team added an answer This should work for you: SELECT * FROM pages p1… May 16, 2026 at 2:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.