下面是上述代码的mcve,测试一种断言,即它以某种方式工作,而另一种方式不工作。在这里,它通过设置标准的字体来工作。
JLabel
或使用
JXLabel
.
看看你能不能:
-
在您的机器上确认结果
-
如果它按预期工作,请跟踪原始代码中的差异。
import java.awt.*;
import javax.swing.*;
public class JXLabelTest {
public static void main(String[] args) {
Runnable r = () -> {
String s = "The quick brown fox jumps over the lazy dog";
JLabel myLabel = new JLabel(s);
myLabel.setFont(new Font("Segoe UI", Font.PLAIN, 6));
JOptionPane.showMessageDialog(null, myLabel);
JOptionPane.showMessageDialog(null, new JXLabel(s));
};
SwingUtilities.invokeLater(r);
}
}
class JXLabel extends JLabel {
Font f = new Font("Segoe UI", Font.PLAIN, 6);
public JXLabel() {
super();
this.setFont(f);
}
public JXLabel(Icon icon) {
super(icon);
this.setFont(f);
}
public JXLabel(Icon icon, int horizontalAlignment) {
super(icon, horizontalAlignment);
this.setFont(f);
}
public JXLabel(String text) {
super(text);
this.setFont(f);
}
public JXLabel(String text, Icon icon, int horizontalAlignment) {
super(text, icon, horizontalAlignment);
this.setFont(f);
}
public JXLabel(String text, int horizontalAlignment) {
super(text, horizontalAlignment);
this.setFont(f);
}
}