I get a JFrame and i want to display a JLabel with a border in it with a padding of maybe 50px. When i set the size of the JFrame to 750, 750, and the size of the JLabel to 650, 650 and the location to 50, 50, it display it strange... Here's my code:
public class GUI {
/**
* Declarate all
*/
public int height = 750;
public int width = 750;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width / 2) - (width / 2); // Center horizontally.
int y = (screen.height / 2) - (height / 2); // Center vertically.
/**
* Create the GUI
*/
JFrame frame = new JFrame();
Border border = LineBorder.createBlackLineBorder();
JLabel label = new JLabel();
public GUI(){
label.setBorder(border);
label.setSize(700, 700);
label.setLocation(0, 0);
frame.getContentPane().setLayout(null);
frame.add(label);
}
public void createGUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(x,y,width,height);
frame.setVisible(true);
}
}
So I think the title-bar at the top is also include in the size. In Graphics you can use getInsets()
. Now is there anything like that for Swing / JFrame?
First get the pixels trimmed out by the frame.
int reqWidth = reqHeight = 750;
// first set the size
frame.setSize(reqWidth, reqHeight);
// This is not the actual-sized frame. get the actual size
Dimension actualSize = frame.getContentPane().getSize();
int extraW = reqWidth - actualSize.width;
int extraH = reqHeight - actualSize.height;
// Now set the size.
frame.setSize(reqWidth + extraW, reqHeight + extraH);
An alternate simpler way. The previous works but this is recommended.
frame.getContentPane().setPreferredSize(750, 750);
frame.pack();
Hope this helps.
EDIT:
Add this in your constructor before adding components to the frame. and to set it in the middle, use
frame.setLocationRelativeTo(null);
This will center the window on the screen.
Using setPreferredSize()
is problematic, as it always overrules the component's calculation with an arbitrary choice. Instead, pack()
the enclosing Window
to accommodate the preferred sized of the components, as shown below.
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
/**
* @see https://stackoverflow.com/a/13481075/230513
*/
public class NewGUI extends JPanel {
private static final int S1 = 10;
private static final int S2 = 50;
private JLabel label = new JLabel("Hello, world!");
public NewGUI() {
label.setHorizontalAlignment(JLabel.CENTER);
Border inner = BorderFactory.createEmptyBorder(S1, S1, S1, S1);
Border outer = BorderFactory.createLineBorder(Color.black);
label.setBorder(new CompoundBorder(outer, inner));
this.setBorder(BorderFactory.createEmptyBorder(S2, S2, S2, S2));
this.add(label);
}
private void display() {
JFrame f = new JFrame("NewGUI");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewGUI().display();
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With