问题
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);
}
}
}
回答1:
In about Line 55:
output = new JTextArea();
output.setFont ((output.getFont()).deriveFont (24.0f));
you could modify the font size to a new float value.
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.
来源:https://stackoverflow.com/questions/48577313/how-can-i-change-the-size-of-a-system-out-println-local-variable-inside-a-jframe