【问题标题】:Why is my html code in Swing's JButton off center?为什么我在 Swing 的 JButton 中的 html 代码偏离中心?
【发布时间】: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


【解决方案1】:

所以有两个主要的事情(可能是 3 个)

  • 去掉setPreferredSize,让按钮根据文本的呈现方式决定它应该有多大。
  • 去掉“+”周围的空格,空格不允许文本的中心正确对齐(通过任何计算来确定它)
  • 您也可以考虑删除 aButton.setFont(new Font("Monospaced", 0, 16));,但这取决于您的需求...

所以,左边那个没有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 是否会更简单,但我真的不知道你想要达到什么目的......

【讨论】:

  • 感谢您的回答,但我注意到,它错过了最后一个“”标签。我首先在我的问题中错过了它,然后你复制并错过了它,我刚刚修复了我的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多