我得到一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我认为顶部的标题栏也包含在尺寸中.在图形中,您可以使用getInsets().现在是否有类似Swing/JFrame的东西?
Sri*_*ati 12
首先得到帧修剪出的像素.
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);
Run Code Online (Sandbox Code Playgroud)
另一种更简单的方法.以前的作品,但建议这样做.
frame.getContentPane().setPreferredSize(750, 750);
frame.pack();
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
编辑:
在将组件添加到框架之前,在构造函数中添加此项.并将其设置在中间,使用
frame.setLocationRelativeTo(null);
Run Code Online (Sandbox Code Playgroud)
这将使窗口居中显示在屏幕上.
| 归档时间: |
|
| 查看次数: |
16718 次 |
| 最近记录: |