我自己的JButton首选大小无法正常工作? [英] My own JButton preferred size is not working correctly?

查看:136
本文介绍了我自己的JButton首选大小无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想创建自己的自定义 JButton ,我想设置首选大小,我该怎么做呢。我认为它可能很容易但是当我到达它时,我有一个尺寸,我将发送到我的首选尺寸。

If I want to create my own custom JButton and i want to set the preferred size how do I go about doing this. I thought that it might be easy but when I get to it I have a dimension that I will be sending into my preferred size.

但是如何设置正确的 x y 宽度,我的特定 JButton 组件的高度值。从同一方法的方面再次调用 setPreferredSize 似乎是多余的?

But then how do I set the correct x, y, width, and height values for my specific JButton component. It seems redundant to just call setPreferredSize again from in side of the same method?

这是我的事情发现奇怪的是覆盖 JComponent 。我知道它们应该如何使用 paintComponent(...)

This is the thing that I find odd about overriding a JComponent. I see how they are supposed to work with the paintComponent(...).

我想要我的新按钮有一个首选大小(这是默认大小) 20 x 20

I want my new button to have a preferred size (is this the default size) that is 20 x 20.

我想设置任何按钮上的这个大小都不是由构造函数设置的。

I want to set this on any button were the size is not set by the constructor.

创建自定义按钮时应该覆盖哪些方法?

Also what methods should be overridden when creating a custom button?

class myButton extends JButton {
    public myButton(String s) {
        super(s);   
    }
    public void setPrefferedSize(Dimension d) {
        this.setBounds(x, y, width, height)
        setPreferredSize(d);
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.RED);
    }
}


推荐答案

首先总之:在 setPrefferedSize 方法中添加 @Override 注释会明确表示它应该被称为 setPreferredSize

First of all: Adding an @Override annotation to your setPrefferedSize method would have made clear that it should be called setPreferredSize.

除此之外, getPreferredSize setPreferredSize 方法在 JComponent 中有一些特殊的语义。引用 JComponent#getPreferredSize 文档:

Apart from that, the getPreferredSize and setPreferredSize methods have some special semantics in a JComponent. Quoting from the JComponent#getPreferredSize documentation:


如果preferredSize已设置为非空值,则返回它。如果UI委托的getPreferredSize方法返回非null值,则返回该值;否则推迟到组件的布局管理器。

If the preferredSize has been set to a non-null value just returns it. If the UI delegate's getPreferredSize method returns a non null value then return that; otherwise defer to the component's layout manager.

使用 setPreferredSize ,一切都很好。 (我建议创建组件的子类只是为了在构造函数中调用一些 set ... 方法,但我认为你将有一些额外的方法来证明扩展这个类是正确的。

When setting the preferred size with setPreferredSize, everything is fine. (I'd not recommend to create a subclass of a component only to call some set... methods in the constructor, but I assume that you'll have some additional methods that will justify extending the class).

然而,当覆盖 getPreferredSize JComponent 时,应该尝试保留此方法的语义。这意味着你应该像那样覆盖它

However, when overriding getPreferredSize of JComponent, one should try to preserve the semantics of this method. That means that you should rather override it like that

@Override
public Dimension getPreferredSize()
{
    // If the preferred size was set manually, return this
    // size in order to be in line with the specification
    // that is described in the JavaDoc
    if (super.isPreferredSizeSet())
    {
        return super.getPreferredSize();
    }

    // Otherwise, return "your" preferred size. The
    // DEFAULT_WIDTH and DEFAULT_HEIGHT would be 20,20
    // in your case
    return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}

(实际上,人们必须先向UI询问首选尺寸,但这可能不是这里所希望的)

(actually, one would have to first ask the UI for a preferred size, but this is probably not desired here)

这篇关于我自己的JButton首选大小无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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