简体   繁体   English

在swing消息框中启用文本突出显示

[英]Enable text highlighting in swing message-box

Say i'm using the following code to prompt an error message in my simple swing application: 假设我使用以下代码在我的简单swing应用程序中提示错误消息:

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

Is there any way I could make it possible for the user to highlight text sections (for copy/paste purpose)? 有什么方法可以让用户突出显示文本部分(用于复制/粘贴目的)?

Many thanks. 非常感谢。

try this 尝试这个

 JTextArea textarea= new JTextArea("add your message here");
 textarea.setEditable(true);
 JOptionPane.showMessageDialog(null, textarea, "Error", JOptionPane.ERROR_MESSAGE);

JOptionPane can be constructed with any object, not just a string message. JOptionPane可以用任何对象构造,而不仅仅是字符串消息。 So you could construct a JTextArea and pass that to the JOptionPane as your message. 因此,您可以构造一个JTextArea并将其作为消息传递给JOptionPane。 That should allow copy paste. 这应该允许复制粘贴。

If you object to the white background shown by the default JTextArea, you can set the JTextArea background color equal to the JOptionPane's background color. 如果您反对默认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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM