使用 HTML 标记和对齐问题的多行 JLabel
Multiline JLabel using HTML tags and alignment issues
我在 JSsrollPane
中有 Jlabels
右对齐。我希望标签是多行的,所以我添加了 <html>hello<br>world<html>
。现在的问题是标签不再右对齐而是填满了整个布局。
没有 html 标签时的样子。
使用 html 个标签时的效果
MRE:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.WindowEvent;
public class Test extends JPanel {
static JScrollPane scroll_area = new JScrollPane();
static JPanel panel = new JPanel();
static JButton btn = new JButton("Add");
private static final Color lbl_color = new Color(171, 171, 171);
private static final EmptyBorder lbl_padding = new EmptyBorder(10, 10,10, 10);
Test(){
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
scroll_area.getViewport().add(panel);
scroll_area.createVerticalScrollBar();
scroll_area.createHorizontalScrollBar();
add(scroll_area);
add(btn);
btn.addActionListener(e -> addText());
setSize(300, 30);
}
public static void addText(){
String text = "<html><font color='#F9F6F6'>Hello world <br> Welcome to Earth</font></html>";
// String text = "Hello world Welcome to Earth";
JLabel mess_lbl = new JLabel(text, JLabel.RIGHT);
mess_lbl.setOpaque(true);
mess_lbl.setBackground(lbl_color);
mess_lbl.setAlignmentX(Component.RIGHT_ALIGNMENT);
panel.add(mess_lbl);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.updateUI();
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
frame.add(new Test());
frame.setSize(500, 300);
frame.setVisible(true);
}
}
我希望我的多行标签位于布局的右侧,如图 1 所示。
我对 BoxLayout
没有太多经验,所以您可能想等一下,看看其他人是否有任何想法或建议。但是只要付出一点点努力,我就可以使用 GridBagLayout
而不是
来让它工作
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class Test extends JPanel {
static JScrollPane scroll_area = new JScrollPane();
static JPanel panel = new JPanel();
static JButton btn = new JButton("Add");
private static final Color lbl_color = new Color(171, 171, 171);
private static final EmptyBorder lbl_padding = new EmptyBorder(10, 10, 10, 10);
private JPanel filler = new JPanel();
Test() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 0, 4, 0);
gbc.weighty = 1;
panel.add(filler, gbc);
scroll_area.getViewport().add(panel);
scroll_area.createVerticalScrollBar();
scroll_area.createHorizontalScrollBar();
add(scroll_area);
add(btn);
btn.addActionListener(e -> addText());
setSize(300, 30);
}
private int counter;
public void addText() {
counter += 1;
String text = "<html><font color='#F9F6F6'>Hello world <br> Welcome to Earth " + counter + "</font></html>";
// String text = "Hello world Welcome to Earth";
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 1.0;
gbc.insets = new Insets(4, 0, 4, 0);
JLabel mess_lbl = new JLabel(text, JLabel.RIGHT);
mess_lbl.setOpaque(true);
mess_lbl.setBackground(lbl_color);
mess_lbl.setAlignmentX(Component.RIGHT_ALIGNMENT);
panel.add(mess_lbl, gbc, panel.getComponentCount() - 1);
//panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.revalidate();
panel.repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
frame.add(new Test());
frame.setSize(500, 300);
frame.setVisible(true);
}
}
我在 JSsrollPane
中有 Jlabels
右对齐。我希望标签是多行的,所以我添加了 <html>hello<br>world<html>
。现在的问题是标签不再右对齐而是填满了整个布局。
没有 html 标签时的样子。
使用 html 个标签时的效果
MRE:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.WindowEvent;
public class Test extends JPanel {
static JScrollPane scroll_area = new JScrollPane();
static JPanel panel = new JPanel();
static JButton btn = new JButton("Add");
private static final Color lbl_color = new Color(171, 171, 171);
private static final EmptyBorder lbl_padding = new EmptyBorder(10, 10,10, 10);
Test(){
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
scroll_area.getViewport().add(panel);
scroll_area.createVerticalScrollBar();
scroll_area.createHorizontalScrollBar();
add(scroll_area);
add(btn);
btn.addActionListener(e -> addText());
setSize(300, 30);
}
public static void addText(){
String text = "<html><font color='#F9F6F6'>Hello world <br> Welcome to Earth</font></html>";
// String text = "Hello world Welcome to Earth";
JLabel mess_lbl = new JLabel(text, JLabel.RIGHT);
mess_lbl.setOpaque(true);
mess_lbl.setBackground(lbl_color);
mess_lbl.setAlignmentX(Component.RIGHT_ALIGNMENT);
panel.add(mess_lbl);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.updateUI();
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
frame.add(new Test());
frame.setSize(500, 300);
frame.setVisible(true);
}
}
我希望我的多行标签位于布局的右侧,如图 1 所示。
我对 BoxLayout
没有太多经验,所以您可能想等一下,看看其他人是否有任何想法或建议。但是只要付出一点点努力,我就可以使用 GridBagLayout
而不是
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class Test extends JPanel {
static JScrollPane scroll_area = new JScrollPane();
static JPanel panel = new JPanel();
static JButton btn = new JButton("Add");
private static final Color lbl_color = new Color(171, 171, 171);
private static final EmptyBorder lbl_padding = new EmptyBorder(10, 10, 10, 10);
private JPanel filler = new JPanel();
Test() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 0, 4, 0);
gbc.weighty = 1;
panel.add(filler, gbc);
scroll_area.getViewport().add(panel);
scroll_area.createVerticalScrollBar();
scroll_area.createHorizontalScrollBar();
add(scroll_area);
add(btn);
btn.addActionListener(e -> addText());
setSize(300, 30);
}
private int counter;
public void addText() {
counter += 1;
String text = "<html><font color='#F9F6F6'>Hello world <br> Welcome to Earth " + counter + "</font></html>";
// String text = "Hello world Welcome to Earth";
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 1.0;
gbc.insets = new Insets(4, 0, 4, 0);
JLabel mess_lbl = new JLabel(text, JLabel.RIGHT);
mess_lbl.setOpaque(true);
mess_lbl.setBackground(lbl_color);
mess_lbl.setAlignmentX(Component.RIGHT_ALIGNMENT);
panel.add(mess_lbl, gbc, panel.getComponentCount() - 1);
//panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.revalidate();
panel.repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
frame.add(new Test());
frame.setSize(500, 300);
frame.setVisible(true);
}
}