gpt4 book ai didi

java - 使用html时JLabel消失

转载 作者:行者123 更新时间:2023-11-30 03:06:07 24 4
gpt4 key购买 nike

我有一个 JLabel,其中将包含大量文本,因此我需要向其中添加新行。我发现为了做到这一点,您需要在 JLabel 和 <br> 中使用 html换一条新线。好吧,一旦我将 HTML 添加到 JLabel,它就会消失!!

这是带有以下代码的 JLabel 图片:

instructionLabel = new JLabel("lol");
instructionLabel.setBounds(25,10,500,100);
add(instructionLabel);

http://prntscr.com/9p1cme

现在,当我更改代码时,只要添加 html,它就会消失:

instructionLabel = new JLabel("<html>lol</html>");
instructionLabel.setBounds(25,10,500,100);
add(instructionLabel);

http://prntscr.com/9p1d4u

该程序没有错误,如果需要,我愿意提供更多代码。

最佳答案

避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计目的是与核心的布局管理器一起工作,放弃这些将导致无休止的问题和问题,您将花费越来越多的时间来尝试纠正

HtmlLabel

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LabelExample {

public static void main(String[] args) {
new LabelExample();
}

public LabelExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
add(new JLabel("Look ma, no html"));
add(new JLabel("<html>Look ma, html</html>"));
}

}

}

参见Laying Out Components Within a Container了解更多详情

基于此要求...

Requirement

我做了一个简单的 GridBagLayout 并生成了这个...

Score

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class LabelExample {

public static void main(String[] args) {
new LabelExample();
}

public LabelExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridBagLayout());
JLabel points = new JLabel("<html><b>Points: 0</b></html>");
JLabel highscore = new JLabel("<html><b>Highscore: 0</b></html>");
JLabel score = new JLabel("97");
score.setFont(score.getFont().deriveFont(Font.BOLD, 96));

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
add(points, gbc);
gbc.gridx = 2;
add(highscore, gbc);

gbc.weightx = 0;
gbc.weighty = 1;
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridy++;
gbc.ipadx = 50;
gbc.ipady = 50;
score.setHorizontalAlignment(JLabel.CENTER);
add(score, gbc);

JButton high = new JButton("High");
JButton low = new JButton("Low");

gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(high, gbc);
gbc.gridx = 2;
add(low, gbc);
}


}

}

这只是提供该想法的基本示例的一个粗略的开始

关于java - 使用html时JLabel消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34735667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com