JOptionPane输出文本副本 [英] JOptionPane output text copy

查看:201
本文介绍了JOptionPane输出文本副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have no experience with JOptionpane but I need a simple program to simplify my life. The code I need help with is below:

public static void main (String[] args) {
    String input = "";

    input = JOptionPane.showInputDialog("Enter code");

    JOptionPane.showMessageDialog(null, toStringU(toArray(input)), "RESULT",
            JOptionPane.INFORMATION_MESSAGE);

}

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

toStringU method gives me a long long text

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

I want to run it without any compiler (a standalone application, double click, put info and take results).

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

And I cant copy the result from output panel, which I need to copy. So either I need to copy it and/or I want to write it to a txt file (second one would be great).

推荐答案

JOptionPane 允许您指定 Object 作为message参数,如果此值为组件某种,它将被添加到 JOptionPane String s是自动使用 JLabel 进行渲染

JOptionPane allows you to specify an Object as the message parameter, if this value is a Component of some kind, it will be added to the JOptionPane (Strings are rendered using JLabel automatically)

通过设置类似 JTextArea的内容这是不可编辑的,你可以利用它的复制功能,而你需要做更多的工作......

By setting up something like a JTextArea that is not editable, you can take advantage of its copying capabilities, with out much more work on your part...

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 为你...

Additional side effects

Another side effect is the fact that the JTextArea can actually write it's contents to a Writer for you...

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) {
    }
}

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

Which allows you to write a file as well...

这篇关于JOptionPane输出文本副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆