是否有一个Java JFrame等效的Win32函数“AdjustWindowRect”?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了是否有一个Java JFrame等效的Win32函数“AdjustWindowRect”?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2054字,纯文字阅读大概需要3分钟。
内容图文
如果您不知道“AdjustWidowRect”的作用,请参阅MSDN中的说明:
Calculates the required size of the window rectangle, based on the desired client-rectangle size.
更清楚:
在swing中,当你设置JFrame的大小时,包括边框.这意味着如果您将JFrame的大小设置为640乘480,那将不是客户端大小,因为您输入的大小会计算帧边框的大小.
我想要一个矩形,并且能够调整它,所以当JFrame的大小设置为该矩形时,JFrame的客户端大小是调整前矩形的大小.
解决方法:
AFAIU接受的答案不考虑混合中的菜单栏或其他组件.这可以通过覆盖组件的优选尺寸和包装框架来实现.
I’m drawing graphics to the JFrame and I need it to be a precise size.
不要绘制到顶级容器,如JFrame或JWindow.而是渲染到JPanel或BufferedImage并将其添加到TLC.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class SizedGUI {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(2,3,2,3));
gui.add(new FixedSizeComponent());
gui.setBackground(Color.RED);
JFrame f = new JFrame("Demo");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See https://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// tweak to ensure the GUI never gets too small
f.setMinimumSize(f.getSize());
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
class FixedSizeComponent extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(400,100);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
g.setColor(Color.BLACK);
g.drawString(w + "x" + h, w/2, h/2);
}
}
内容总结
以上是互联网集市为您收集整理的是否有一个Java JFrame等效的Win32函数“AdjustWindowRect”?全部内容,希望文章能够帮你解决是否有一个Java JFrame等效的Win32函数“AdjustWindowRect”?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。