📄 9.8-testfontmetrics.java
字号:
// TestFontMetrics.java: Draw a message at the center of a panel
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.Dimension;
import javax.swing.JPanel;
public class TestFontMetrics extends JFrame
{
// Default constructor
public TestFontMetrics()
{
MessagePanel messagePanel = new MessagePanel("我们应相互理解,不要相互拆台");
// Set font SansSerif 20-point bold
messagePanel.setFont(new Font("SansSerif", Font.BOLD, 20));
// Center the message
messagePanel.setCentered(true);
getContentPane().add(messagePanel);
}
// Main method
public static void main(String[] args)
{
TestFontMetrics frame = new TestFontMetrics();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setTitle("TestFontMetrics");
frame.setVisible(true);
}
}
// MessagePanel: Display a message on a JPanel
class MessagePanel extends JPanel
{
private String message = "Welcome to Java"; // Message to display
// (x, y) coordinates where the message is displayed
private int xCoordinate = 20;
private int yCoordinate = 20;
// Indicating whether the message is displayed in the center
private boolean centered;
// Default constructor
public MessagePanel()
{
}
// Contructor with a message parameter
public MessagePanel(String message)
{
this.message = message;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public int getXCoordinate()
{
return xCoordinate;
}
public void setXCoordinate(int x)
{
this.xCoordinate = x;
}
public int getYCoordinate()
{
return yCoordinate;
}
public void setYCoordinate(int y)
{
this.yCoordinate = y;
}
public boolean isCentered()
{
return centered;
}
public void setCentered(boolean centered)
{
this.centered = centered;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (centered)
{
// Get font metrics for the current font
FontMetrics fm = g.getFontMetrics();
// Find the center location to display
int w = fm.stringWidth(message); // Get the string width
int h = fm.getAscent(); // Get the string height
xCoordinate = (getWidth()-w)/2;
yCoordinate = (getHeight()+h)/2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
public Dimension getPreferredSize()
{
return new Dimension(200, 100);
}
public Dimension getMinimumSize()
{
return new Dimension(200, 100);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -