-->

我怎样才能改变一个地方的System.out.println变量的大小一个JFrame内(How c

2019-09-29 05:00发布

我修改一些代码, 我发现显示一个局部变量,而不是一个JFrame内集文字,但我无法找到一个方法来改变文字大小。 我一直在阅读有关frameName.setFont(新字体(____)),但似乎并没有被工作中的println。 我将如何修改文本大小? 这里是我当前的代码。 (PS我是新的,所以我很抱歉,我不知道我在说什么。)

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

Answer 1:

在约55线:

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

你可以修改字体大小为一个新的浮动值。

见JTextArea中的文档的方法setfont程序,看看它在继承层次的定义。 考虑Font类,有什么用它做。



文章来源: How can I change the size of a System.out.println local variable inside a JFrame