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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:05:42+00:00 2026-06-07T21:05:42+00:00

Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public

  • 0

Please have a look the following code

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

public class GUI extends JFrame
{
    private JButton open, process;
    private JLabel center;
    private JScrollPane scroll;
    private Box box;
    private IplImage image;

    public FirstGUI()
    {
        open = new JButton("Open Image");
        open.setPreferredSize(new Dimension(70,20));
        open.setMaximumSize(new Dimension(100,20));

        open.addActionListener(new OpenImageAction());
        process = new JButton("Process");
        process.setPreferredSize(new Dimension(100,20));
        process.setMinimumSize(new Dimension(100,20));
        process.addActionListener(new ProcessAction());
        System.out.println("Open Size: "+open.getSize()+"      Process size: "+process.getSize());


        box = new Box(BoxLayout.Y_AXIS);
        box.add(open);
        box.add(process);

        center = new JLabel();
        scroll = new JScrollPane(center);

        getContentPane().add(box,"West");
        getContentPane().add(scroll,"Center");

        this.setSize(300,300);
        this.pack();
        this.validate();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            new GUI();
        }
        catch(java.lang.Exception e)
        {
            JOptionPane.showMessageDialog(null,"GUI Error");
        }
    }

I want to make all the buttons in same size. In here, first one is wider than the second one. I need same width ans hight for both. As you can see, I have used all the availables like setPrefferedSize(), setMaximumSize(), setMinimumSize(), but it is still not working properly. Please help!

  • 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-07T21:05:43+00:00Added an answer on June 7, 2026 at 9:05 pm

    Here is one way to achieve this using a GridLayout. I also introduced an additional JPanel so that the buttons are not stretched when the JFrame is resized and I chose the GridBagLayout for it so that it will center the button panel vertically. There are definitely other ways to solve your issue.

    One thing you should try to avoid is to force preferred/maximum/minimum sizes. Delegate this to the L&F and the LayoutMananager’s.

    If you call pack() on a JFrame, it is useless to set its size previously, as pack() will change that anyway. Try to make the call to setVisible(true); the last line of your GUI initialization.

    If you want to understand properly how layout, positioning, sizing, etc… works in Swing, I would strongly recommend to read the tutorial on LayoutManager’s.

    import java.awt.BorderLayout;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    
    public class GUI extends JFrame {
        private JButton open, process;
        private JLabel center;
        private JScrollPane scroll;
        private JPanel box;
    
        public GUI() {
            open = new JButton("Open Image");
            // open.addActionListener(new OpenImageAction());
            process = new JButton("Process");
            // process.addActionListener(new ProcessAction());
    
            box = new JPanel(new GridLayout(2, 1));
            box.add(open);
            box.add(process);
            JPanel west = new JPanel(new GridBagLayout());
            west.add(box);
    
            center = new JLabel("Some center label");
            scroll = new JScrollPane(center);
    
            getContentPane().add(west, BorderLayout.WEST);
            getContentPane().add(scroll);
    
            this.pack();
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (java.lang.Exception e) {
                JOptionPane.showMessageDialog(null, "GUI Error");
            }
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new GUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code. import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*;
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout;
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue;
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
Please have a look at the following code package normal; import java.io.*; import java.util.*;
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {

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.