我想使用JTextPane打印多行文本到窗口



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

我有一个窗口弹出在屏幕上,看起来像一个命令提示符,有两个按钮运行和停止。无论如何,我有在屏幕上,一旦你按下开始它开始"扫描文件",它说"扫描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();
    }
}

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

你必须附加文本,但必须找到一个机制,因为append方法不存在。在控制台方法中,您可以添加以下内容:

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()。这只是你的例子没有扫描你的代码。

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

相关内容

  • 没有找到相关文章

最新更新