Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set a specific dimension of a Button object in Java Swing?
    text
    copied!<p>I have this simple Java Swing test application that show an upper label and under this label a button:</p> <pre><code>package com.techub.exeute; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; import org.jdesktop.application.SingleFrameApplication; public class Main extends SingleFrameApplication{ public static void main(String[] args) { Main a = new Main(); a.startup(); } @Override protected void startup() { JFrame frame = new JFrame("FrameDemo"); frame.setMinimumSize(new Dimension(800, 400)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel myLabel = new JLabel("Hello World !!!", SwingConstants.CENTER); myLabel.setFont(new Font("Serif", Font.BOLD, 22)); myLabel.setBackground(Color.RED); myLabel.setOpaque(true); myLabel.setPreferredSize(new Dimension(100, 80)); frame.getContentPane().add(myLabel, BorderLayout.NORTH); Button myButton = new Button("Click Me !!!"); myButton.setMaximumSize(new Dimension(50, 25)); frame.getContentPane().add(myButton, BorderLayout.CENTER); //Display the window. frame.pack(); frame.setVisible(true); } } </code></pre> <p>The button is in the <code>BorderLayout.CENTER</code> position. The problem is that this button occupies all available space in the <code>CENTER</code> position also if I have set the Maximum Size property with a Dimension object.</p> <p>What am I wrong? What can I do to create a button having specific dimension?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload