为什么我在Swing的JButton中的html代码偏离中心?

时间:2015-05-28 03:16:58

标签: java html swing jbutton centering

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

但“+”标记偏离中心,如何解决?

enter image description here

1 个答案:

答案 0 :(得分:5)

所以两个主要的事情(也许是3个)

  • 摆脱setPreferredSize,让按钮根据文本的呈现方式决定它应该有多大。
  • 摆脱&#34; +&#34;周围的空间,空格不允许文本的居中正确对齐(通过用于确定它的计算)< / LI>
  • 你也可以考虑摆脱aButton.setFont(new Font("Monospaced", 0, 16));,但这取决于你的需求......

WithoutFont WithFont

因此,左边的那个没有setFont,右边的那个没有setFont

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Buttons {

    public static void main(String[] args) {
        new Buttons();
    }

    public Buttons() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            add(makeButton());
        }

        public JButton makeButton() {
            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></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);
            return aButton;
        }

    }

}

我有点想知道使用GridLayout是否更简单,但我真的不知道你想要实现的是什么......