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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:19:36+00:00 2026-06-18T07:19:36+00:00

I have a panel something like this: class A extends JPanel{ private JButton button;

  • 0

I have a panel something like this:

class A extends JPanel{
    private JButton button;

    A(int width, int height){
        setSize(width, height);
        button = new JButton("text");
        button.setIcon(IconLoadedHere);
        button.setBounds(50, 50, getWidth()/5, getHeight()/5);
        button.setBorder(BorderFactory.createEmptyBorder());
        add(button);
     }
}

The JFrame is like this:

public class window extends JFrame{
    private JPanel panel;

    public window(){
        panel = new JPanel();
         setTitle("test");
         setSize(1280, 720);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
    }
    public void loadA(){
         remove(panel);
         panel = new A(getWidth(), getHeight());
             add(panel);
         validate();
    }
}

And have 2 problems with the button:

  1. I’d like to have the text over the image, is that possible (preferably in the middle of the image)? currently it appears on the left of it. (SOLVED)

  2. for some weird reason setBounds is not working, button always appear on top center (seems that only can be used without layout, any alternative to set the buttons place and size to however i preffer?)

And also 1 question:
If I want to resize the window, what do I do to make the button and text to resize accordingly? (currently only found the solution on changing them one by one, is there any other 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-06-18T07:19:37+00:00Added an answer on June 18, 2026 at 7:19 am

    To control the way the text is displayed in the button you should use:

    button.setHorizontalTextPosition(JButton.CENTER);
    button.setVerticalTextPosition(JButton.CENTER);
    

    This will put the text in the center of the button.

    By default, the horizontal text position is set to TRAILING, and that is why it is positionned on the side of the icon.

    For the layout part of the question, you should really read the relevant part of the official tutorial. TLDR: use layout managers, they will do the resizing and positioning for you.

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

Sidebar

Related Questions

I have grid something like this: Ext.define('Exp.view.dashboard.Tv', { extend: 'Ext.grid.Panel', initComponent: function() { this.columns
I have a custom editor composed of several components. Something like: class MyCellEditor extends
Have a class definition that looks something like this class someClass(models.Model): field1 = models.ForeignKey('other_app.field4')
I have a class MyPanel that extends JPanel . MyPanel is a child panel:
I have a class like this: public class Stretcher : Panel { public static
How do I write a class for something like this Example: I have a
Lets say I have a set of model classes like this: public class Person
i have some panel in page (Like Visual studio), i Want when user click
I would like use a panel whose children have coordinates specified as percentage of
When i write something like this: <Style x:Key=panelS> <Setter Property=Orientation Value=Horizontal /> <Setter Property=DockPanel.Dock

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.