java 获取 JFrame 内容的实际大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13474795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the real size of a JFrame content
提问by Michael Schmidt
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:
我得到一个 JFrame,我想显示一个 JLabel,其中有一个边框,内边距可能为 50px。当我将 JFrame 的大小设置为 750、750,将 JLabel 的大小设置为 650、650 并将位置设置为 50、50 时,它显示它很奇怪......这是我的代码:
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?
所以我认为顶部的标题栏也包含在大小中。在图形中,您可以使用getInsets()
. 现在 Swing/JFrame 有什么类似的东西吗?
回答by Sri Harsha Chilakapati
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.
这将使窗口在屏幕上居中。
回答by trashgod
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.
使用setPreferredSize()
是有问题的,因为它总是以任意选择否决组件的计算。取而代之的是,pack()
封闭Window
以容纳首选尺寸的组件,如下所示。
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();
}
});
}
}