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

前端之家收集整理的这篇文章主要介绍了java – 如何在NetBeans中控制JButton HTML文本的居中?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在JButton上放一行两行文字;例如
+----------+
|  READER  |
|   STOP   |
+----------+

但是我无法将它集中在按钮上.我转到JButton的属性编辑器,并为text属性输入< html>< center> READER< br> STOP.这导致两个单词相对于彼此居中,但是它们一起看起来仍然朝向按钮面的右侧移动,如:

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

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

编辑:这里是我省略< center>时如何布局的说明完全,如果有人对我的描述感到困惑,“READER和STOP是相互尊重的左对齐,但在按钮上右对齐”:

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

编辑:这是NetBeans生成代码

readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
    readerStopButton_.setFont(new java.awt.Font("Geneva",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的两个正在搞乱.

解决方法

原因是你限制了按钮的大小,默认情况下按钮有一个余量,你可以删除边距如下:
readerStopButton_.setMargin(new Insets(0,-30,-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",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));
        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",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);
    }
}

猜你在找的Java相关文章