Thu*_*rge 3 java swing joptionpane jtextarea windows-8
我喜欢JOptionPane的便利性,但不喜欢它不包装文本的事实.所以我决定将这个问题的答案如下:
public static void main(String[] args)
{
String text = "one two three four five six seven eight nine ten ";
text = text + text + text + text + text
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.append(text);
textArea.setSize(textArea.getPreferredSize().width, 1); //Explanation for this line in the comments of the linked thread
JOptionPane.showMessageDialog(
null, textArea, "Not Truncated!", JOptionPane.WARNING_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)
这在Mac OS X上运行得很好:

但是在Windows 8上不起作用,因为窗口没有调整大小,因为JTextArea的高度随着多行的增加而增加,从而将按钮推开:

有什么我做错了吗?两个平台上的Java Swing表现不同吗?我该如何解决这个问题?
我发现问题实际上不是JTextArea,而是我在程序的其他地方使用以下命令为Windows 8设置了默认外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Run Code Online (Sandbox Code Playgroud)
删除该行并默认为Metal Look and Feel(就像我在顶部发布的代码片段中的情况一样)使一切看起来正确.在我意识到上面的主要方法中的对话框与我的屏幕截图中的对话框不同之后,我甚至都没想到它可能是问题.
我的一个朋友使用Windows 7尝试了包含该行代码的程序,同样的事情发生了.所以看来这是Java中的一个错误,至少有Windows 7和Windows 8(对我来说运行Java 7u10,对她来说是6或7).
根据提供的替代方案,我现在已经切换到使用带有CSS的JLabel,因为它对于我的纯文本目的而言很快且很脏(我知道它可以跨平台工作),但我可能会迁移到JEditorPanes中未来.