我有一个JLabel,里面会有很多文字,所以我需要添加新行。我发现为了做到这一点,你需要在你的JLabel和<br>
中使用html来换行。好吧,只要我将HTML添加到我的JLabel就消失了!!
以下是JLabel的图片,其中包含以下代码:
instructionLabel = new JLabel("lol");
instructionLabel.setBounds(25,10,500,100);
add(instructionLabel);
现在,当我更改代码时,只要添加html,它就会消失:
instructionLabel = new JLabel("<html>lol</html>");
instructionLabel.setBounds(25,10,500,100);
add(instructionLabel);
该程序没有错误&amp;如果有必要,我愿意提供更多代码。
答案 0 :(得分:2)
避免使用null
布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正
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
我做了一个简单的GridBagLayout
并制作了这个......
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);
}
}
}
这只是一个粗略的开始,提供了一个基本的想法
答案 1 :(得分:0)
// You can try below code to solve your problem.
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
public class Tutorial extends JFrame {
public static void main(String[] args) {
new Tutorial();
}
public Tutorial() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
JLabel instructionLabel = new JLabel("<html><p>lol <br> Working</p></html>");
instructionLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
instructionLabel.setBounds(25,10,500,100);
getContentPane().add(instructionLabel);
setVisible(true);
}
}