gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:15:35 25 4
gpt4 key购买 nike

我修改了一些代码我发现来显示局部变量而不是在 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);
}
}
}

最佳答案

大约第 55 行:

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

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

请参阅 JTextArea 文档中的 setFont 方法,查看它在继承层次结构中的定义位置。查看 Font 类,看看它能做什么。

关于java - 如何更改 JFrame 内 System.out.println 局部变量的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48577313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com