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



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

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

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

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

最新更新