📄 centermessage.java
字号:
import javax.swing.*;import java.awt.*;public class CenterMessage extends JPanel { /** Main method */ public static void main(String[] args) { JFrame frame = new JFrame("CenterMessage"); CenterMessage m = new CenterMessage(); m.setBackground(Color.white); m.setFont(new Font("Californian FB", Font.BOLD, 30)); frame.getContentPane().add(m); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setVisible(true); } /** Paint the message */ protected void paintComponent(Graphics g) { super.paintComponent(g); // Get font metrics for the current font FontMetrics fm = g.getFontMetrics(); // Find the center location to display int stringWidth = fm.stringWidth("Welcome to Java"); int stringAscent = fm.getAscent(); // Get the position of the leftmost character in the baseline int xCoordinate = getWidth() / 2 - stringWidth / 2; int yCoordinate = getHeight() / 2 + stringAscent / 2; g.drawString("Welcome to Java", xCoordinate, yCoordinate); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -