我准备了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的长度,有人对如何处理该想法有想法吗?
答案 0 :(得分:0)
只需使用
fileLabel.setPreferredSize(new Dimension(100, 40));
代替
fileLabel.setMaximumSize(new Dimension(100, 40));
此设置器告诉您使用组件将其放置在带有布局管理器的容器中时必须使用什么尺寸作为“首选”。
PS:对不起,我的英语