如何在JFrame中更改System.out.println局部变量的大小

时间:2018-02-02 06:59:19

标签: java jframe local-variables println text-size

我修改了一些代码我找到了 来显示一个局部变量,而不是在JFrame中设置文本,但是我找不到改变的方法文字的大小。我一直在阅读有关frameName.setFont(new Font(____))的内容,但这似乎并不适用于println。我该如何修改文字大小?这是我目前的代码。 (P.S.我是新人,所以我很抱歉我不知道我在说什么。)

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

1 个答案:

答案 0 :(得分:2)

在第55行:

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

您可以将字体大小修改为新的浮点值。

查看方法setFont的JTextArea文档,查看继承层次结构中定义的位置。查看Font类,如何处理它。