JOptionPane输出文本副本

我没有JOptionpane经验,但我需要一个简单的程序来简化我的生活。 我需要帮助的代码如下:

 public static void main (String[] args) { String input = ""; input = JOptionPane.showInputDialog("Enter code"); JOptionPane.showMessageDialog(null, toStringU(toArray(input)), "RESULT", JOptionPane.INFORMATION_MESSAGE); } 

toStringU方法给了我一个很长的文本

我想在没有任何编译器的情况下运行它(独立应用程序,双击,放置信息并获取结果)。

我无法从输出面板复制结果,我需要复制。 所以我需要复制它和/或我想将它写入txt文件(第二个会很棒)。

JOptionPane允许您指定一个Object作为消息参数,如果该值是某种类型的Component ,它将被添加到JOptionPaneString s将自动使用JLabel呈现)

通过设置不可编辑的JTextArea类的东西,您可以利用其复制function,而您需要做更多的工作……

在此处输入图像描述

 import java.awt.EventQueue; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class TestOptionPane11 { public static void main(String[] args) { new TestOptionPane11(); } public TestOptionPane11() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JTextArea ta = new JTextArea(10, 10); ta.setText("This is some really long text that is likely to run over " + "the viewable area and require some additional space to render " + "properly, but you should be able to select and copy parts or " + "whole sections of the text as you, but it should remain " + "no editable..."); ta.setWrapStyleWord(true); ta.setLineWrap(true); ta.setCaretPosition(0); ta.setEditable(false); JOptionPane.showMessageDialog(null, new JScrollPane(ta), "RESULT", JOptionPane.INFORMATION_MESSAGE); } }); } } 

额外的副作用

另一个副作用是JTextArea实际上可以为你写一个Writer的内容……

 FileWriter writer = null; try { writer = new FileWriter("SomeoutputFile.txt", false); ta.write(writer); } catch (IOException exp) { exp.printStackTrace(); } finally { try { writer.close(); } catch (Exception e) { } } 

这也允许你写一个文件……