BorderLayout 为较小的 JLabel 保留 space
BorderLayout reserving space for smaller JLabel
我准备了 2 个 JUnit 风格的测试用例(抱歉我真的不知道如何断言这种奇怪的测试 UI)
对于第二个示例,我有选择地剪切字符串,但我希望能够粘贴长字符串并让 UI 正确剪切它
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.junit.jupiter.api.Test;
public class testing {
@Test
public void howItsSupposedToLook() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setSize(1000, 900);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.GREEN);
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
// this is how it should look
JLabel fileLabel3 = new JLabel(
"<html><div style=\"padding-left:1px;padding-top:10px\"><div style=\"border:1px;font-size: 10px;line-height: 11px; height:23px;overflow:hidden;width:"
+ (100) + "px;white-space:nowrap;\">" + "fsdfesfevvveeeg" + "</div></div></html>");
leftPanel.add(fileLabel3);
//no more differences between the two from here
frame.add(leftPanel, BorderLayout.LINE_START);
JPanel centerPAnel = new JPanel();
centerPAnel.setBackground(Color.RED);
frame.add(centerPAnel, BorderLayout.CENTER);
frame.validate();
frame.repaint();
boolean open = true;
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(WindowEvent winEvt) {
System.exit(0);
}
});
while (open) {
}
}
@Test
public void failedTest() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setSize(1000, 900);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.GREEN);
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
// this is how it actually looks
JLabel fileLabel = new JLabel(
"<html><div style=\"padding-left:1px;padding-top:10px\"><div style=\"border:1px;font-size: 10px;line-height: 11px; height:23px;overflow:hidden;width:"
+ (100) + "px;white-space:nowrap;\">"
+ "fsdfesfevvveeegggggggggggggggggggggggevevsdffffffffffffffffffffffffffffffffffffffffffffffffffffffffh"
+ "</div></div></html>");
fileLabel.setMaximumSize(new Dimension(100, 40));
//no more differences between the two from here
leftPanel.add(fileLabel);
frame.add(leftPanel, BorderLayout.LINE_START);
JPanel centerPAnel = new JPanel();
centerPAnel.setBackground(Color.RED);
frame.add(centerPAnel, BorderLayout.CENTER);
frame.validate();
frame.repaint();
boolean open = true;
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(WindowEvent winEvt) {
System.exit(0);
}
});
while (open) {
}
}
}
它的外观(第一个(制造)示例):
它的实际外观:
我觉得 Layout 没有正确识别 JPanel 的长度,有没有人知道该怎么办?
只需使用
fileLabel.setPreferredSize(new Dimension(100, 40));
而不是
fileLabel.setMaximumSize(new Dimension(100, 40));
这个 setter 告诉这个组件放置在带有布局管理器的容器中时必须使用什么尺寸作为 "preferred"
PS: 对不起我的英文
我准备了 2 个 JUnit 风格的测试用例(抱歉我真的不知道如何断言这种奇怪的测试 UI) 对于第二个示例,我有选择地剪切字符串,但我希望能够粘贴长字符串并让 UI 正确剪切它
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.junit.jupiter.api.Test;
public class testing {
@Test
public void howItsSupposedToLook() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setSize(1000, 900);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.GREEN);
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
// this is how it should look
JLabel fileLabel3 = new JLabel(
"<html><div style=\"padding-left:1px;padding-top:10px\"><div style=\"border:1px;font-size: 10px;line-height: 11px; height:23px;overflow:hidden;width:"
+ (100) + "px;white-space:nowrap;\">" + "fsdfesfevvveeeg" + "</div></div></html>");
leftPanel.add(fileLabel3);
//no more differences between the two from here
frame.add(leftPanel, BorderLayout.LINE_START);
JPanel centerPAnel = new JPanel();
centerPAnel.setBackground(Color.RED);
frame.add(centerPAnel, BorderLayout.CENTER);
frame.validate();
frame.repaint();
boolean open = true;
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(WindowEvent winEvt) {
System.exit(0);
}
});
while (open) {
}
}
@Test
public void failedTest() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setSize(1000, 900);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.GREEN);
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
// this is how it actually looks
JLabel fileLabel = new JLabel(
"<html><div style=\"padding-left:1px;padding-top:10px\"><div style=\"border:1px;font-size: 10px;line-height: 11px; height:23px;overflow:hidden;width:"
+ (100) + "px;white-space:nowrap;\">"
+ "fsdfesfevvveeegggggggggggggggggggggggevevsdffffffffffffffffffffffffffffffffffffffffffffffffffffffffh"
+ "</div></div></html>");
fileLabel.setMaximumSize(new Dimension(100, 40));
//no more differences between the two from here
leftPanel.add(fileLabel);
frame.add(leftPanel, BorderLayout.LINE_START);
JPanel centerPAnel = new JPanel();
centerPAnel.setBackground(Color.RED);
frame.add(centerPAnel, BorderLayout.CENTER);
frame.validate();
frame.repaint();
boolean open = true;
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(WindowEvent winEvt) {
System.exit(0);
}
});
while (open) {
}
}
}
它的外观(第一个(制造)示例):
它的实际外观:
我觉得 Layout 没有正确识别 JPanel 的长度,有没有人知道该怎么办?
只需使用
fileLabel.setPreferredSize(new Dimension(100, 40));
而不是
fileLabel.setMaximumSize(new Dimension(100, 40));
这个 setter 告诉这个组件放置在带有布局管理器的容器中时必须使用什么尺寸作为 "preferred"
PS: 对不起我的英文