Java 获取JFrame内容的实际大小
我得到一个JFrame,我想显示一个JLabel,其中有一个边框,填充可能是50px。当我将JFrame的大小设置为750750,将JLabel的大小设置为650650,将位置设置为50,50时,它会显示奇怪。。。这是我的密码:Java 获取JFrame内容的实际大小,java,swing,Java,Swing,我得到一个JFrame,我想显示一个JLabel,其中有一个边框,填充可能是50px。当我将JFrame的大小设置为750750,将JLabel的大小设置为650650,将位置设置为50,50时,它会显示奇怪。。。这是我的密码: public class GUI { /** * Declarate all */ public int height = 750; public int width = 750; Dimension scree
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);
}
}
因此,我认为顶部的标题栏也包括在尺寸中。在图形中,您可以使用
getInsets()
。现在Swing/JFrame有类似的功能吗?首先将像素按帧修剪掉
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);
另一种更简单的方法。这是以前的工作,但这是推荐的
frame.getContentPane().setPreferredSize(750, 750);
frame.pack();
希望这有帮助
编辑:
在将组件添加到框架之前,将其添加到构造函数中。并将其设置在中间,使用
frame.setLocationRelativeTo(null);
这将使窗口在屏幕上居中。使用
setPreferredSize()
是,因为它总是用任意选择推翻组件的计算。相反,pack()
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();
}
});
}
}
请回答您的问题,为什么(现在不是开玩笑)在屏幕上设置最终大小很重要获取JFrame
contentpane的尺寸-是的,在计算JFrame大小时还包括标题栏!!但我不明白你的问题@dTDesign:您可能必须重写JFrame
类的getInset()
方法才能返回对您的情况有效的内容,例如,如图所示。@mKorbel:Hm?对不起,我不明白你的问题…谢谢你的回答。但很抱歉问了这个愚蠢的问题:我必须在哪里插入这个?我想让屏幕中间的窗口,所以我需要StimeBuffes()方法…对不起,我不太使用Swing……dtDebug:把窗口放在屏幕中间,你只需要使用<代码>框架对象。StestOffice相对(NULL);<代码>,就是这样,在调用JFrame
上的setVisible(true)
之前。Eclipse给我一个“setPreferedSize”错误。。。它将使用“Object”对其进行强制转换…可能必须将其更改为frame.getContentPane().setPreferredSize(新维度(750750))
并添加此导入:导入java.awt.Dimension代码>