centermessage.java

来自「java课件」· Java 代码 · 共 35 行

JAVA
35
字号
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 + =
减小字号Ctrl + -
显示快捷键?