本文介绍了Java/Swing:低调的按钮高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想减小 JButton 的垂直尺寸.以下代码适用于 K > 1,但我似乎无法减小大小.有什么建议吗?
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 button = /* ... get button here ... */
Dimension d = button.getPreferredSize();
d.setSize(d.getWidth(), d.getHeight()*K);
button.setPreferredSize(d);
我正在使用 JavaBuilders + MigLayout.看起来我必须做 button.setMaxSize(d);
而不是 setPreferredSize(),不知道为什么.
edit: I'm using JavaBuilders + MigLayout. It looks like I have to do button.setMaxSize(d);
instead of setPreferredSize(), not sure why.
推荐答案
几个选项:
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);
}
}
生产
这篇关于Java/Swing:低调的按钮高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!