组件宽度属性的意外变化 [英] Unexpected changes of the width attribute of a component

查看:22
本文介绍了组件宽度属性的意外变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到运行我在下面列出的程序有时会产生不需要的效果.

I have noticed that running the program I'm listing below sometimes produces an unwanted effect.

我已经简化了代码以使事情看起来清楚.我正在绘制一个打印出当前组件大小的字符串.我已经覆盖了 Component 类中的 getPrefferedSize() 方法,并将宽度和高度分别设置为 640 x 512.但是,运行程序后我仍然得到不同的结果:640 x 512 和 650 x 522.奇怪的是删除 frame.setResizable(false) 行修复了一些问题.但我希望窗口可以调整大小

<代码>

import java.awt.*;

import javax.swing.*;

public class DrawTest
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                DrawFrame frame = new DrawFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}


class DrawFrame extends JFrame
{

    private static final long serialVersionUID = 1L;
    public DrawFrame()
    {
        setTitle("DrawTest");
        setLocationByPlatform(true);
        Container contentPane = getContentPane();
        DrawComponent component = new DrawComponent();
        contentPane.add(component);
    }

}

class DrawComponent extends JComponent
{
    private static final long serialVersionUID = 1L;
    public Dimension getPreferredSize() {
        return new Dimension(640, 512);
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;         
        String msg = getWidth() + " x " + getHeight();      
        g2.setPaint(Color.BLUE);
        g2.drawString(msg, getWidth()/2, getHeight()/2);
    }
}

推荐答案

setLocationByPlatform() 的调用必须跟在 pack() 之后并在 setVisible() 之前,否则几何会出错.由于没有将 JFrame 子类化的原因,我省略了 DrawFrame 是下面的示例.请注意使用 FontMetrics 在调整封闭容器大小时保持文本居中.这种方法对于学习布局很方便.

The call to setLocationByPlatform() must follow pack() and precede setVisible(), or the geometry will be wrong. Absent a reason to subclass JFrame, I have elided DrawFrame is the example below. Note the use of FontMetrics to keep the text centered as the enclosing container is resized. The approach is handy for learning layouts.

import java.awt.*;
import javax.swing.*;

public class DrawTest {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("DrawTest");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DrawComponent());
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

class DrawComponent extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(320, 240);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        String msg = getWidth() + " x " + getHeight();
        g2.setPaint(Color.BLUE);
        int w = (getWidth() - g.getFontMetrics().stringWidth(msg)) / 2;
        g2.drawString(msg, w, getHeight() / 2);
    }
}

这篇关于组件宽度属性的意外变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆