Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

java - My own JButton preferred size is not working correctly?

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.

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?

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

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

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).

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);
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...