Cha*_*hap 6 html java swing jbutton
我试图在JButton上放一行两行文字; 例如
+----------+
| READER |
| STOP |
+----------+
Run Code Online (Sandbox Code Playgroud)
但是我无法将它集中在按钮上.我去属性编辑器对JButton,并输入<html><center>READER<br>STOP了文本属性.这导致两个单词相对于彼此居中,但是它们一起看起来仍然朝向按钮面的右侧移动,如:
+----------+
| READER|
| STOP |
+----------+
Run Code Online (Sandbox Code Playgroud)
还有水平和垂直对齐和文本位置属性,我将它们全部设置为CENTER,但这没有任何我能看到的效果.
编辑:这里是一个例子,说明当我<center>完全省略时它是如何布局的,以防任何人对我的描述感到困惑"读者和停止是相互尊重的左对齐,但在按钮上右对齐":
+----------+
| READER|
| STOP |
+----------+
Run Code Online (Sandbox Code Playgroud)
编辑:这是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_);
Run Code Online (Sandbox Code Playgroud)
编辑:这是按钮对我的看法的屏幕截图.有很多我不知道做布局,所以我很可能省略一些关键信息.但基本上我让NetBeans完成所有工作,除了提供HTML文本.
编辑:打开一个显示所有按钮的替换屏幕截图.请注意,不使用HTML的单词(单字单词)正确对齐,而使用HTML的两个正在搞乱.

不要忘记结束标记:
JButton button = new JButton("<html><center>READER<br>STOP</center></html>");
Run Code Online (Sandbox Code Playgroud)
另请参见如何在Swing组件中使用HTML.
附录:我无耻地克隆了@mre 上面正确的HTML以生成以下图像:

原因是您限制了按钮的大小,默认情况下按钮有边距,您可以按如下方式删除边距:
readerStopButton_.setMargin(new Insets(0,-30, 0,-30));
Run Code Online (Sandbox Code Playgroud)

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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7595 次 |
| 最近记录: |