Java/Swing: low-profile button height?

后端 未结 3 1355
遇见更好的自我
遇见更好的自我 2020-11-27 23:30

I would like to reduce the vertical size of a JButton. The following code works fine for K > 1 but I can\'t seem to reduce the size. Any suggestions?

JButton         


        
相关标签:
3条回答
  • 2020-11-27 23:39

    Maybe just play with the Border of the button:

    Insets insets = button.getInsets();
    insets.top = 0;
    insets.bottom = 0;
    button.setMargin( insets );
    
    0 讨论(0)
  • 2020-11-27 23:40

    As an alternative, some L&Fs (e.g. Nimbus, Aqua) support a JComponent.sizeVariant, as discussed in Resizing a Component and Using Client Properties. Several variations are illustrated here.

    0 讨论(0)
  • 2020-11-27 23:59

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

    Produces

    enter image description here

    0 讨论(0)
提交回复
热议问题