如何在JFrame中更改System.out.println局部变量的大小 [英] How can I change the size of a System.out.println local variable inside a JFrame

查看:129
本文介绍了如何在JFrame中更改System.out.println局部变量的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了一些代码我发现的 ,以显示局部变量而不是在JFrame中设置文本,但是我找不到改变大小的方法的文字.我一直在阅读有关frameName.setFont(new Font(____))的信息,但这似乎不适用于println.如何修改文字大小?这是我当前的代码. (PS.我是新来的,对不起,我不知道我在说什么.)

I modified some code I found to display a local variable instead of set text inside of a JFrame, but I'm unable to find a way to change the size of the text. I keep reading about frameName.setFont(new Font(____)), but that doesn't seem to be working for the println. How would I modify the text size? Here's my current code. (P.S. I'm new, so I'm sorry that i don't know what I'm talking about.)

import java.util.*;
import java.io.*;
import java.awt.*; 
import javax.swing.*; 
import java.time.*;
import java.time.temporal.*;
public class Window {
    LocalDate today = LocalDate.now();
    // LocalDate bday = LocalDate.of(2018, Month.MAY, 13);
    LocalDate bday = LocalDate.parse("2018-05-13");
    Period until = Period.between(today, bday);
    long untilDay = ChronoUnit.DAYS.between(today, bday);
    String string = new String("There are " + until.getYears() + " years, " + until.getMonths() +
                   " months, and " + until.getDays() +
                   " days left! (" + untilDay + " days total)");
    public static void main(String[] args) {
        new Window();
    }
    public Window() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }
                CapturePane capturePane = new CapturePane();
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(capturePane);
                frame.setSize(1000, 1000);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                PrintStream ps = System.out;
                System.setOut(new PrintStream(new StreamCapturer("STDOUT", capturePane, ps)));
                // System.out.println("Hello, this is a test");
                System.out.println(string);
            }            
        });
    }

    public class CapturePane extends JPanel implements Consumer {

        private JTextArea output;

        public CapturePane() {
            setLayout(new BorderLayout());
            output = new JTextArea();
            add(new JScrollPane(output));
        }

        @Override
        public void appendText(final String text) {
            if (EventQueue.isDispatchThread()) {
                output.append(text);
                output.setCaretPosition(output.getText().length());
            } else {

                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        appendText(text);
                    }
                });

            }
        }        
    }

    public interface Consumer {        
        public void appendText(String text);        
    }

    public class StreamCapturer extends OutputStream {

        private StringBuilder buffer;
        private String prefix;
        private Consumer consumer;
        private PrintStream old;

        public StreamCapturer(String prefix, Consumer consumer, PrintStream old) {
            this.prefix = prefix;
            buffer = new StringBuilder(128);
            buffer.append("[").append(prefix).append("] ");
            this.old = old;
            this.consumer = consumer;
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            String value = Character.toString(c);
            buffer.append(value);
            if (value.equals("\n")) {
                consumer.appendText(buffer.toString());
                buffer.delete(0, buffer.length());
                buffer.append("[").append(prefix).append("] ");
            }
            old.print(c);
        }        
    }    
}

推荐答案

大约在55行:

output = new JTextArea();
output.setFont ((output.getFont()).deriveFont (24.0f));

您可以将字体大小修改为新的float值.

you could modify the font size to a new float value.

有关方法setFont,请参见JTextArea的文档,看看它在继承层次结构中的定义位置.查看Font类,如何处理它.

See the doc for JTextArea for the method setFont, look where it is defined in the inheritance hierarchy. Look into the Font class, what to do with it.

这篇关于如何在JFrame中更改System.out.println局部变量的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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