【发布时间】:2015-08-10 08:58:44
【问题描述】:
我的 Swing JButton 代码如下所示:
Insets An_Inset=new Insets(0,0,0,0);
String Content="<Html>"+
" <Table Border=1 Cellspacing=0 Cellpadding=2 Width=48>"+
" <Tr><Td Align=Center BgColor=Blue><Font Size=3> + </Font></Td><Td Align=Center><Font Size=3> + </Font></Td></Tr>"+
" <Tr><Td Align=Center><Font Size=3> + </Font></Td><Td Align=Center><Font Size=3> + </Font></Td></Tr>"+
" </Table>"+
"</Html>";
JButton aButton=new JButton(Content);
aButton.setFont(new Font(Monospaced,0,16));
aButton.setPreferredSize(new Dimension(56,56));
aButton.setEnabled(false);
aButton.setMargin(An_Inset);
aButton.setHorizontalAlignment(SwingConstants.CENTER);
但是“+”标记偏离中心,如何解决?
【问题讨论】:
-
从摆脱
aButton.setPreferredSize(new Dimension(56,56));开始 -
表格的额外内边距/边框增加了按钮渲染文本所需的大小,但是因为您认为自己可以做得更好,所以它坏了
-
一个使按钮居中的建议是使用某个布局管理器重新设计整个事物,以包含诸如 Gridlayout 之类的按钮,并在初始化期间将其文本设置为 +。
标签: java html swing jbutton centering