【问题标题】:I want to Print multiple lines of Text using a JTextPane to a window我想使用 JTextPane 将多行文本打印到窗口
【发布时间】:2015-07-05 14:36:49
【问题描述】:

在过去的一两天里,我已经用不同的措辞搜索了这个问题,但我无法解决它:/,

我在屏幕上弹出一个看起来像命令提示符的窗口,有两个按钮运行和停止。无论如何,一旦你按下开始,我在屏幕上就会开始“扫描文件”它说“扫描 1-1900 的东西”计数到 1900,然后说扫描完成,之后我想要文本在现有文本下 例如,写多行文字来惹我的朋友。

{
Scan Completed
"wait time inbetween each line of text"
Hack initialized
"wait time inbetween each line of text"
Hack installing...
"wait time inbetween each line of text"
Hack installed
ECT
}

希望有人可以帮助我,我看到的每个人都无法使用我的代码:/ 我也是编码新手,所以...

在此先谢谢你,我的代码不会太长:P。

   public static void main(String[] args) throws IOException {

        JFrame frame = new JFrame("Happy Monday v0.05");
        Container contentPane = frame.getContentPane();

        JTextPane jta = new JTextPane();

        JButton button = new JButton("Run");
        JButton buttonstop = new JButton("Stop"); 

        contentPane.add(button);
        contentPane.add(buttonstop);  

        button.setBounds(-1,283,465,40);
        buttonstop.setBounds(465,283,469,40);

        frame.add(jta).setBackground(Color.black);
        console(jta);

        //Window        
        frame.setSize(950, 650 / 16 * 9);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

            button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Object>(){
                    @Override
                    protected Void doInBackground() throws Exception {
                        outputTest("Scanning...");
                        return null;

                    }}.execute();
            }});

    }

    //Testing OUTPUTS:/
    public static void outputTest(String msg){
        for(int i=0;i<1969;i++){
            System.out.println(i+" "+msg);
            try {
                Thread.sleep(01);
                System.out.println("Scan Complete");

            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }       
        }
    }

    public static void console(final JTextPane area) throws IOException {
        area.setContentType("text/html");
        final PipedInputStream outPipe = new PipedInputStream();
        System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));       
        new SwingWorker<Void, String>() {
            @Override
            protected Void doInBackground() throws Exception {
                Scanner s = new Scanner(outPipe);
                while (s.hasNextLine()){
                    String line = s.nextLine();
                    publish(line + "\n");
                }
                return null;
            }
            @Override
            protected void process(List<String> chunks) {
                for (String line : chunks){
                    area.setText("<font size=\"5\" color=\"green\">"+line+"</font>");
                    }   
                 }
        }.execute();
    }

}

【问题讨论】:

标签: java user-interface text window jtextpane


【解决方案1】:

setText 方法不会追加行。它会覆盖文本。因此之前的文本将不可见。

您必须附加文本,但必须找到一种机制,因为附加方法不存在。在您的控制台方法中,您可以添加这些:

Document doc = area.getDocument();
Thread.sleep(2000);
doc.insertString(doc.getLength(),"hack installing....\n", null);
Thread.sleep(2000);
doc.insertString(doc.getLength(),"Hack installed...\n", null);

注意:

不建议直接调用Thread.sleep(),因为您使用的是多线程环境。这只是为了您的示例没有扫描您的代码。

【讨论】:

    【解决方案2】:
    public class NotEditableOutputArea extends JTextPane {
    
            public NotEditableOutputArea() {
                setEditable(false);
                DefaultCaret caret = (DefaultCaret) getCaret();
                caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
                Font defaultFont = new Font("monospaced", Font.PLAIN, 12);
                setFont(defaultFont);
            }
    
            public void appendColorText(String text, Color c) {
                StyledDocument doc = getStyledDocument();
                Style style = addStyle("Style", null);
                StyleConstants.setForeground(style, c);
                try {
                    doc.insertString(doc.getLength(), text, style);
                } catch (BadLocationException e) {
                }
    
            }
    
            public void setColorText(String text, Color c) {
                setText(null);
                appendColorText(text, c);
            }
        }
    

    【讨论】:

    • 请至少稍微解释一下您的解决方案,例如对所实现的每个方法进行小型可解释的 cmets
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多