8

假设我正在使用以下代码在我的简单 Swing 应用程序中提示错误消息:

JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);

有什么方法可以让用户突出显示文本部分(用于复制/粘贴目的)?

非常感谢。

4

3 回答 3

7

try this

 JTextArea textarea= new JTextArea("add your message here");
 textarea.setEditable(true);
 JOptionPane.showMessageDialog(null, textarea, "Error", JOptionPane.ERROR_MESSAGE);
于 2013-01-06T15:15:22.800 回答
2

JOptionPane 可以用任何对象构造,而不仅仅是字符串消息。因此,您可以构造一个 JTextArea 并将其作为您的消息传递给 JOptionPane。那应该允许复制粘贴。

于 2013-01-06T15:08:41.517 回答
2

如果您反对默认 JTextArea 显示的白色背景,您可以将 JTextArea 背景颜色设置为等于 JOptionPane 的背景颜色。

String title = "foo";
String message = "Select me";

JTextArea msg = new JTextArea(message);
JOptionPane pane = new JOptionPane(msg, JOptionPane.INFORMATION_MESSAGE);
msg.setBackground(pane.getBackground());
JDialog dialog = pane.createDialog(null, title);
dialog.setVisible(true);
于 2019-05-10T03:11:55.760 回答