Java setBounds() 方法 (JFrame)
Java setBounds() method (JFrame)
您好,我正在使用 setBounds 方法打开具有特定大小的 window。但是我在参数中传递的大小是 window 的大小,包括框架的条形图。如何仅为内容设置尺寸?
设置内容的大小,然后在 JFrame 上调用 pack()。
编辑:因为 Guillaume Polet 不会停止让我伤心,这里是一个完整的工作示例。请注意您根本不需要(错误)使用继承,并且它可以用更少的行完成工作:
import java.awt.Dimension;
import javax.swing.JFrame;
public class Main {
public static void main(String... args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
}
}
有两种方法可以解决这个问题:
- 设置框架的边界时考虑框架的边界
- 覆盖
JFrame
的内容窗格的 getPreferredSize()
,然后在框架上调用 pack()
。
下面是这两种技术的演示:
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
protected void initUI() {
JFrame frame = new JFrame("Insets technique");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Insets i = frame.getInsets();
System.out.println(i);
Rectangle bounds = new Rectangle(50, 100, 400, 500);
bounds.width += i.right + i.left;
bounds.height += i.bottom + i.top;
frame.setBounds(bounds);
}
protected void initUI2() {
JFrame frame = new JFrame("Preferred size technique");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 500);
}
});
frame.pack();
frame.setLocation(50, 100);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test test = new Test();
test.initUI();
test.initUI2();
}
});
}
}
您好,我正在使用 setBounds 方法打开具有特定大小的 window。但是我在参数中传递的大小是 window 的大小,包括框架的条形图。如何仅为内容设置尺寸?
设置内容的大小,然后在 JFrame 上调用 pack()。
编辑:因为 Guillaume Polet 不会停止让我伤心,这里是一个完整的工作示例。请注意您根本不需要(错误)使用继承,并且它可以用更少的行完成工作:
import java.awt.Dimension;
import javax.swing.JFrame;
public class Main {
public static void main(String... args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
}
}
有两种方法可以解决这个问题:
- 设置框架的边界时考虑框架的边界
- 覆盖
JFrame
的内容窗格的getPreferredSize()
,然后在框架上调用pack()
。
下面是这两种技术的演示:
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
protected void initUI() {
JFrame frame = new JFrame("Insets technique");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Insets i = frame.getInsets();
System.out.println(i);
Rectangle bounds = new Rectangle(50, 100, 400, 500);
bounds.width += i.right + i.left;
bounds.height += i.bottom + i.top;
frame.setBounds(bounds);
}
protected void initUI2() {
JFrame frame = new JFrame("Preferred size technique");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 500);
}
});
frame.pack();
frame.setLocation(50, 100);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test test = new Test();
test.initUI();
test.initUI2();
}
});
}
}