Java JFrame .setSize(x, y) not working?(Java JFrame .setSize(x, y) 不工作?)
问题描述
当我执行这段代码时,会弹出一个小窗口,里面大约是 116x63,包括边框在内的整个大小约为 140x100.如何将内部设置为我需要的?
When I execute this bit of code, a tiny window pops up that, and the inside of it is about 116x63, and the entire size including the border of ~140x100. How do I set the inside to be what I need it to?
public static void graphics() {
JFrame frame = new JFrame();
String title = "test window";
frame.setTitle(title);
frame.setSize(gridRow, gridCol); //101 x 101
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
推荐答案
- 创建一个自定义组件,从
JPanel
扩展,覆盖其getPreferredSize
方法以返回您想要的窗口大小. - 将此添加到您的框架或将其设置为框架的内容窗格.
- 在框架上调用
pack
- Create a custom component, extending from
JPanel
, override itsgetPreferredSize
method to return the size of the window you would like. - Either add this to your frame or set it as the frame's content pane.
- Call
pack
on the frame
更新示例
在我的电脑上,Frame size = java.awt.Dimension[width=216,height=238]
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestFrameSize01 {
public static void main(String[] args) {
new TestFrameSize01();
}
public TestFrameSize01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("Frame size = " + frame.getSize());
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
String text = getWidth() + "x" + getHeight();
FontMetrics fm = g2d.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g2d.drawString(text, x, y);
g2d.dispose();
}
}
}
这篇关于Java JFrame .setSize(x, y) 不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java JFrame .setSize(x, y) 不工作?
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01