Java / Swing: low key button height?

I want to reduce the vertical size of JButton The following code applies to k > 1, but I can't seem to reduce the size Any suggestions?

JButton button = /* ... get button here ... */
Dimension d = button.getPreferredSize();
d.setSize(d.getWidth(),d.getHeight()*K);
button.setPreferredSize(d);

Editor: I'm using javabuilders miglayout Looks like I have to do a button setMaxSize(d); Instead of setpreferredsize(), I don't know why

Solution

Some options:

import java.awt.*;

public class FrameTest {
    public static void main(String[] args) {
        JFrame jf = new JFrame("Demo");
        jf.getContentPane().setLayout(new FlowLayout());

        // Ordinary button
        jf.add(new JButton("button 1"));

        // Smaller font
        jf.add(new JButton("button 2") {{ setFont(getFont().deriveFont(7f)); }});

        // Similar to your suggestion:
        jf.add(new JButton("button 3") {{
            Dimension d = getPreferredSize();
            d.setSize(d.getWidth(),d.getHeight()*.5);
            setPreferredSize(d);
        }});

        jf.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }
}

produce

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>