如何在 NetBeans 中控制 JButton HTML 文本的居中?

发布于 2024-11-28 07:49:42 字数 1651 浏览 0 评论 0原文

我正在尝试在 JButton 上放置两行文本;例如

+----------+
|  READER  |
|   STOP   |
+----------+

,但我无法将其集中在按钮上。我转到 JButton 的属性编辑器,然后为 text 属性输入

READER
STOP
。这会导致两个单词相对于彼此居中,但它们一起看起来仍然向按钮表面的右侧移动,如:

+----------+
|    READER|
|     STOP |
+----------+

还有水平和水平。垂直对齐和文本位置属性,我已将其全部设置为 CENTER,但这没有任何我可以看到的效果。

编辑:这是当我完全省略

时它的布局方式的说明,以防有人对我的描述“READER 和 STOP 是左对齐的”感到困惑。其他,但在按钮上右对齐”:

+----------+
|    READER|
|    STOP  |
+----------+

编辑:这是 NetBeans 生成的代码:

    readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
    readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
    readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
    readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive.  ");
    readerStopButton_.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    readerStopButton_.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            readerStopButton_ActionPerformed(evt);
        }
    });
    operationButtons_.add(readerStopButton_);

编辑:这是按钮在我看来的屏幕截图。关于布局我有很多不了解的地方,所以我很可能遗漏了一些重要的信息。但基本上我让 NetBeans 完成所有工作,除了提供 HTML 文本。

编辑:放置显示所有按钮的替换屏幕截图。请注意,不使用 HTML 的(单个单词的)对齐正确,而使用 HTML 的两个则混乱。

更好地看待问题

I'm trying to put a two-line piece of text on a JButton; e.g.

+----------+
|  READER  |
|   STOP   |
+----------+

But I'm having trouble getting it centered on the button. I go to the Property editor for the JButton, and enter <html><center>READER<br>STOP for the text property. This causes the two words to be centered with respect to each other, but together they still appear to be shifted toward the right side of the face of the button, as in:

+----------+
|    READER|
|     STOP |
+----------+

There are also Horizontal & Vertical Alignment & Text Position properties, which I've set all to CENTER, but this doesn't have any effect that I can see.

EDIT: here is an illustration of how it is laid out when I omit <center> entirely, in case anyone is confused by my description that "READER and STOP are left-justified w/respect to each other, but right-justified on the button":

+----------+
|    READER|
|    STOP  |
+----------+

EDIT: here is the code generated by NetBeans:

    readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
    readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
    readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
    readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive.  ");
    readerStopButton_.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    readerStopButton_.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            readerStopButton_ActionPerformed(evt);
        }
    });
    operationButtons_.add(readerStopButton_);

EDIT: Here's a screen capture of how the button looks to me. There is a lot I don't know about doing layouts, so it's quite possible I'm omitting some crucial info. But basically I'm letting NetBeans do all the work, except for providing the HTML text.

EDIT: Putting up a replacement screen shot that shows all buttons. Note that the ones that don't use HTML (the single-word ones) are correctly aligned, while the two that do use HTML are messed up.

better view of problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人九事 2024-12-05 07:49:42

不要忘记结束标记:

JButton button = new JButton("<html><center>READER<br>STOP</center></html>");

另请参阅如何使用Swing 组件中的 HTML

附录:我无耻地克隆了@mre的更正上面的 HTML 以生成以下图像:

在此处输入图像描述

Don't forget the closing tag:

JButton button = new JButton("<html><center>READER<br>STOP</center></html>");

See also How to Use HTML in Swing Components.

Addendum: I have shamelessly cloned @mre's correct HTML above to produce the following image:

enter image description here

风轻花落早 2024-12-05 07:49:42

我不确定你在说什么。我使用了您包含的代码,它工作得很好。


public final class HTMLTextButtonDemo {

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JButton readerStopButton_ = new JButton();
        readerStopButton_.setFocusPainted(false);
        readerStopButton_.setBackground(UIManager.getDefaults().getColor("Button.light"));
        readerStopButton_.setFont(new Font("Geneva", 0, 12)); // NOI18N
        readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
        readerStopButton_.setHorizontalTextPosition(SwingConstants.CENTER);

        frame.add(readerStopButton_);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

在此处输入图像描述


HTML 文本居中对齐。很难想象你会看到不同的东西。

I'm not sure what you're talking about. I used the code you've included and it works just fine.


public final class HTMLTextButtonDemo {

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JButton readerStopButton_ = new JButton();
        readerStopButton_.setFocusPainted(false);
        readerStopButton_.setBackground(UIManager.getDefaults().getColor("Button.light"));
        readerStopButton_.setFont(new Font("Geneva", 0, 12)); // NOI18N
        readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
        readerStopButton_.setHorizontalTextPosition(SwingConstants.CENTER);

        frame.add(readerStopButton_);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

enter image description here


The HTML text is center justified. It's hard to imagine that you're seeing something different.

ヅ她的身影、若隐若现 2024-12-05 07:49:42

原因是您限制了按钮的大小,默认情况下按钮有一个边距,您可以按如下方式删除边距:

readerStopButton_.setMargin(new Insets(0,-30, 0,-30));

在此输入图像描述

package swing;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class HTMLTextTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {

        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JPanel operationButtons_ = new JPanel();

        JButton readerStopButton_ = new JButton();
        readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
        readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
        readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
        readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive.  ");
        readerStopButton_.setMargin(new Insets(0,-30, 0,-30));
        readerStopButton_.setPreferredSize(new Dimension(66, 40));
        operationButtons_.add(readerStopButton_);

        readerStopButton_ = new JButton();
        readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
        readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
        readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
        readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive.  ");
        System.out.println(readerStopButton_.getPreferredSize());
        readerStopButton_.setPreferredSize(new Dimension(66, 40));
        operationButtons_.add(readerStopButton_);

        operationButtons_.add(new JButton("yCoder.com"));

        frame.add(operationButtons_);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

the reason is you limited the size of the button, by default there is a margin for button, you can remove the margin as follows:

readerStopButton_.setMargin(new Insets(0,-30, 0,-30));

enter image description here

package swing;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class HTMLTextTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {

        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JPanel operationButtons_ = new JPanel();

        JButton readerStopButton_ = new JButton();
        readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
        readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
        readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
        readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive.  ");
        readerStopButton_.setMargin(new Insets(0,-30, 0,-30));
        readerStopButton_.setPreferredSize(new Dimension(66, 40));
        operationButtons_.add(readerStopButton_);

        readerStopButton_ = new JButton();
        readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
        readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
        readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
        readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive.  ");
        System.out.println(readerStopButton_.getPreferredSize());
        readerStopButton_.setPreferredSize(new Dimension(66, 40));
        operationButtons_.add(readerStopButton_);

        operationButtons_.add(new JButton("yCoder.com"));

        frame.add(operationButtons_);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文