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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:01:45+00:00 2026-06-12T05:01:45+00:00

I am writing a swing program. But the problem is that the JButton I

  • 0

I am writing a swing program. But the problem is that the JButton I want to be small. It should be up to me to decide its height and width but the code below creates a long horizontal button.

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

public class SwingExample 
{
         //Create the GUI and show it. For thread safety, this method should be
        //invoked from the event-dispatching thread

        private static void createAndShowGUI()
        {
            //create and setup the window
            JFrame frame=new JFrame("Swing Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Set the size of the window
            frame.setPreferredSize(new Dimension(300,300));            


            //Centre the window on the screen
            WinUtilities wu=new WinUtilities();
            wu.centerWindow(frame);

            //Create a panel
            JPanel panel=new JPanel(new BorderLayout());

            //Create three buttons

                //Button1
                    JButton But1=new JButton("Add");
                    But1.setText("Add Data");
                    But1.setSize(new Dimension(10,20)); //Using it has no effect
                    But1.setMnemonic('A');
                    But1.setMargin(new Insets(12,7,20,10)); //Using it has no effect
                    But1.setBorder(null); //Using it has no effect
                    panel.add(But1,BorderLayout.WEST);

                //Button2
                    JButton But2=new JButton("Edit");
                    But2.setText("Edit Data");
                    But2.setMnemonic(KeyEvent.VK_E);
                    But2.setPreferredSize(new Dimension(5,5));

                    panel.add(But2,BorderLayout.CENTER);

               //Button3
                    JButton But3=new JButton("Display");
                    But3.setText("Display Data");
                    But3.setMnemonic(KeyEvent.VK_D);
                    But3.setPreferredSize(new Dimension(5,5));
                    panel.add(But3,BorderLayout.EAST);


            //Set window characteristics
            frame.setContentPane(panel);
            //frame.add(panel);
            frame.pack();



            //Display the window
            frame.setVisible(true);
        }

        public static void main(String args[])
        {
            //Schedule a job for the event dispatching thread

            //creating and showing this application's GUI
            javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });

        }
}

class WinUtilities
{
    public void centerWindow(JFrame frm)
    {
        frm.setLocationRelativeTo(null);
    }
}

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-12T05:01:47+00:00Added an answer on June 12, 2026 at 5:01 am

    for example

    enter image description here

    suggestion

    • JPanel has implemented in API FlowLayout (pretty accepting PreferredSize),

    • then BoderLayout could be wrong LayoutManager,

    • or to use GridLayout (then all JButtons will be have the same size, based on largest Item)

    code

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SwingExample {
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Swing Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            JButton but1 = new JButton("Add");
            but1.setText("Add Data");
            but1.setMnemonic('A');
            but1.setBorderPainted(false);
            but1.setBackground(Color.red);
            but1.setBorder(null);
            but1.setFocusable(false);
            but1.setMargin(new Insets(0, 0, 0, 0));
            //but1.setContentAreaFilled(false);
            panel.add(but1);
            JButton but2 = new JButton("Edit");
            but2.setText("Edit Data");
            but2.setMnemonic(KeyEvent.VK_E);
            panel.add(but2);
            JButton but3 = new JButton("Display");
            but3.setText("Display Data");
            but3.setMnemonic(KeyEvent.VK_D);
            panel.add(but3);
            frame.add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small GUI program. Everything works except that I want to recognize
I've been writing Java swing GUI code for several years but the following syntax
I am writing a Java Swing-based program using and JFrame that is able to
I'm writing a text/code editing program for my own use in Java/Swing, and was
I am writing an application program using swing. I need to exit from the
I have zero experience writing applications with Swing, but I have one application with
I have a GUI problem that I would like to get sorted out, but
I'm writing a program that lets the user draw a hollow rectangle on the
I'm writing a small app with the help of Eclipse while learning Swing GUIs
I'm writing a program that uses File I/O to traverse through a directory given

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.