我们都知道drawString方法可以这样使用:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Driver {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame Heading");
frame.setSize(1000,500);
myComponent comp = new myComponent();
frame.add(comp);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class myComponent extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawString("Hello World", 300, 300);
}
}
但是,我们如何调整显示字符串的字体大小呢?我想让它变大。
最佳答案
class myComponent extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int fontSize = 20;
Font f = new Font("Comic Sans MS", Font.BOLD, fontSize);
g2.setFont(f);
g2.drawString("Hello World", 300, 300);
}
}
您可以更改样式、大小和字体,因为您可能不想使用 Comic Sans。
另见 Graphics#setFont
.
关于java - Swing 图形用户界面 : drawString - How to set the font size of the String being displayed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460622/