Ber*_*van 1 java swing text joptionpane output
我没有经验,JOptionpane但我需要一个简单的程序来简化我的生活.我需要帮助的代码如下:
public static void main (String[] args) {
String input = "";
input = JOptionPane.showInputDialog("Enter code");
JOptionPane.showMessageDialog(null, toStringU(toArray(input)), "RESULT",
JOptionPane.INFORMATION_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)
toStringU 方法给了我很长的文字
我想在没有任何编译器的情况下运行它(独立应用程序,双击,放置信息并获取结果).
我无法复制输出面板的结果,我需要复制它.所以要么我需要复制它和/或我想将它写入txt文件(第二个会很棒).
Mad*_*mer 16
JOptionPane允许你指定一个Object作为消息参数,如果这个值是Component某种类型,它将被添加到JOptionPane(Strings被JLabel自动渲染)
通过设置一个JTextArea不可编辑的东西,你可以利用它的复制功能,而你需要做更多的工作......

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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
另一个副作用是,它实际上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) {
}
}
Run Code Online (Sandbox Code Playgroud)
这也允许你写一个文件......