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

标签 java html swing jbutton centering

我的 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

最佳答案

主要有两件事(也许是 3 件事)

  • 去掉setPreferredSize,让按钮根据文本的呈现方式决定它应该有多大。
  • 去掉“+”周围的空格,这些空格不允许文本居中正确对齐(通过使用任何计算来确定它)
  • 您也可以考虑删除 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 是否会更简单,但我真的不知道你想要实现什么......

关于java - 为什么我在 Swing 的 JButton 中的 html 代码偏离中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30496611/

相关文章:

java - 刷新引起的延迟初始化错误

java - 如何通过首先搜索整个单词来进行子串?

html - 在 MailChimp 中仅使链接目标可编辑

javascript - 按日期每天更改特定链接

java - 如何从 jSpinner 更改时间格式?

java - 登录 Spring security 后保留相同的 URL

java - 在远程服务器上部署、启动和停止 Scala 应用程序

javascript - 用于添加类宽度的 js 代码不起作用

java - 如何验证输入的文本字段是否是java swing中的手机号码

java - 当变量已经声明时,为什么我的 for 循环需要一个标识符?