MessagePanel

import java.awt.*;
import javax.swing.*;


public class TestMessagePanel extends JFrame {
    public TestMessagePanel() {
        MessagePanel messagePanel1 = new MessagePanel("Welcome to Java");
        MessagePanel messagePanel2 = new MessagePanel("Java is fun");
        MessagePanel messagePanel3 = new MessagePanel("Java is cool");
        MessagePanel messagePanel4 = new MessagePanel("I love Java");
       
        messagePanel1.setFont(new Font("SansSerif", Font.ITALIC, 20));
        messagePanel2.setFont(new Font("Courier", Font.BOLD, 20));
        messagePanel3.setFont(new Font("Times", Font.ITALIC, 20));
        messagePanel4.setFont(new Font("Californian FB", Font.PLAIN, 20));
       
        messagePanel1.setBackground(Color.RED);
        messagePanel2.setBackground(Color.CYAN);
        messagePanel3.setBackground(Color.GREEN);
        messagePanel4.setBackground(Color.WHITE);
        messagePanel1.setCentered(true);
       
        setLayout(new GridLayout(2, 2));
        add(messagePanel1);
        add(messagePanel2);
        add(messagePanel3);
        add(messagePanel4);
    }
   
    public static void main(String[] args) {
        TestMessagePanel frame = new TestMessagePanel();
        frame.setTitle("TestMessagePanel");
        frame.setSize(300, 200);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}







import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;


public class MessagePanel extends JPanel {
    /** The message to be displayed */
    private String message = "Welcome to Java";
   
    /** The x-coordinate where the message is displayed */
    private int xCoordinate = 20;
   
    /** The y-coordinate where the message is displayed */
    private int yCoordinate = 20;

    /** Indicate whether the message is displayed in the center */
    private boolean centered = false;
   
    /** The interval for moving the message horizontally and
     * vertically */
    private int interval = 10;
   
    /** Construct with default properties */
    public MessagePanel() {
    }
   
    /** Construct with default panel with a specified message */
    public MessagePanel(String message) {
        this.message = message;
    }
   
    /** Return message */
    public String getMessage() {
        return message;
    }
   
    /** Set a new message */
    public void setMessage(String message) {
        this.message = message;
        super.repaint();
    }
   
    /** Return xCoordiante */
    public int getXCoordinate() {
        return xCoordinate;
    }
   
    /** Return yCoordinate */
    public int getYCoordinate() {
        return yCoordinate;
    }
   
    /** Set a new xCoordinate */
    public void setXCoordinate(int xCoordinate) {
        this.xCoordinate = xCoordinate;
        super.repaint();
    }
   
    /** Set a new yCoordinate */
    public void setYCoordinate(int yCoordinate) {
        this.yCoordinate = yCoordinate;
        super.repaint();
    }
   
    /** Return centered */
    public boolean isCentered() {
        return centered;
    }
   
    /** Set a new Centered */
    public void setCentered(boolean centered) {
        this.centered = centered;
        super.repaint();
    }
   
    /** Return interval */
    public int getInterval() {
        return interval;
    }
   
    /** Set a new interval */
    public void setInterval(int interval) {
        this.interval = interval;
    }
   
    /** Move the message left */
    public void moveLeft() {
        xCoordinate -= interval;
        super.repaint();
    }
   
    /** Move the message right */
    public void moveRight() {
        xCoordinate += interval;
        super.repaint();
    }
   
    /** Move the message up */
    public void moveUp() {
        yCoordinate -= interval;
        super.repaint();
    }
   
    /** Move the message down */
    public void moveDown() {
        yCoordinate += interval;
        super.repaint();
    }
   
    /** Paint the message */
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        if(centered) {
            // Get font metrics for the current font
            FontMetrics fm = g.getFontMetrics(g.getFont());
           
            // Get the position of the leftmost character in the baseline
            xCoordinate = getWidth() / 2 - fm.stringWidth(message) / 2;
            yCoordinate = getHeight() / 2 + fm.getAscent() / 2;
        }
        g.drawString(message, xCoordinate, yCoordinate);
    }
   
    public Dimension getPreferredSize() {
        return new Dimension(200, 30);
    }
   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值