How to control the centering of JButton HTML text in NetBeans?

I am trying to put a two-line piece of text in JButton; eg.

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

But I had trouble concentrating on the button. Go to the property editor for JButton and enter <html><center>READER<br>STOP for the text property. This causes the two words to be centered relative to each other, but together they still appear offset to the right side of the button’s face, as in:

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

There are also horizontal and vertical alignment and text position properties that I set for CENTER, but this does not have any effect that I can see.

EDIT: here is an illustration of how it is presented when I completely omit <center> , in case someone confuses my description that "READER and STOP are left-justified with respect to each other, 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 at me. I don’t know much about layouts, so it’s quite possible that I am omitting some important information. But basically, I let NetBeans do all the work except for the provision of HTML text.

EDIT: Set a replacement screenshot showing all buttons. Note that those that do not use HTML (single-word) are correctly aligned, and those that use HTML are confused.

better view of problem

+6
source share
3 answers

the reason is that you have limited the size of the button, by default there is a field for the button, you can delete the field 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); } } 
+3
source

Do not forget the closing tag:

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

See also How to use HTML in Swing components .

Application: I shamelessly cloned @mre the correct HTML above to create the following image:

enter image description here

+8
source

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


 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


HTML text is centered. It is hard to imagine that you see something else.

+4
source

Source: https://habr.com/ru/post/894418/


All Articles