package animals;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.Graphics;
public class TestMessagePanel extends JFrame{
public TestMessagePanel(){
MessagePanel messagePanel1 = new MessagePanel("A");
MessagePanel messagePanel2 = new MessagePanel("B");
MessagePanel messagePanel3 = new MessagePanel("C");
MessagePanel messagePanel4 = new MessagePanel("D");
messagePanel1.setFont(new Font("SansSerif",Font.ITALIC,20));
messagePanel2.setFont(new Font("Courier",Font.ITALIC,20));
messagePanel3.setFont(new Font("Times",Font.ITALIC,20));
messagePanel4.setFont(new Font("SansSerif",Font.ITALIC,20));
messagePanel1.setBackground(Color.RED);
messagePanel2.setBackground(Color.BLUE);
messagePanel3.setBackground(Color.BLACK);
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(" TestMessage ");
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MessagePanel extends JPanel{
private String message = "欢迎来到晋江文学城";
private int xCoordinate = 20;
private int yCoordinate = 20;
private boolean centered;//居中
int interval = 10;
public MessagePanel(){
}
public MessagePanel(String message){
this.message = message;
}
public void setMessage(String message){
this.message = message;
repaint();//重新绘制
}
public String getMessage(){
return message;
}
public int getXCoordinate(){
return xCoordinate;
}
public void setXCoordinate(int x){
this.xCoordinate = x;
repaint();
}
public int getYCoordinate(){
return yCoordinate;
}
public void setYCoordinate(int y){
this.yCoordinate = y;
repaint();
}
public boolean isCentered(){
return centered;
}
public void setCentered(boolean centered){
this.centered = centered;
repaint();
}
public int getInterval(){
return interval;
}
public void setInterval(int interval){
this.interval = interval;
repaint();
}
// public void moveLeft(){
// xCoordinate -= interval;
// repaint();
// }
// public void moveRight(){
// xCoordinate += interval;
// repaint();
// }
// public void moveUp(){
// yCoordinate -= interval;
// repaint();
// }
// public void moveDown(){
// yCoordinate += interval;
// repaint();
// }
public Dimension getPreferredSize(){
return new Dimension(200,30);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(centered){
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth(message);
int stringAscent = fm.getAscent();
xCoordinate = getWidth()/2 - stringWidth/2;
yCoordinate = getHeight()/2 + stringAscent/2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
}
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.Graphics;
public class TestMessagePanel extends JFrame{
public TestMessagePanel(){
MessagePanel messagePanel1 = new MessagePanel("A");
MessagePanel messagePanel2 = new MessagePanel("B");
MessagePanel messagePanel3 = new MessagePanel("C");
MessagePanel messagePanel4 = new MessagePanel("D");
messagePanel1.setFont(new Font("SansSerif",Font.ITALIC,20));
messagePanel2.setFont(new Font("Courier",Font.ITALIC,20));
messagePanel3.setFont(new Font("Times",Font.ITALIC,20));
messagePanel4.setFont(new Font("SansSerif",Font.ITALIC,20));
messagePanel1.setBackground(Color.RED);
messagePanel2.setBackground(Color.BLUE);
messagePanel3.setBackground(Color.BLACK);
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(" TestMessage ");
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MessagePanel extends JPanel{
private String message = "欢迎来到晋江文学城";
private int xCoordinate = 20;
private int yCoordinate = 20;
private boolean centered;//居中
int interval = 10;
public MessagePanel(){
}
public MessagePanel(String message){
this.message = message;
}
public void setMessage(String message){
this.message = message;
repaint();//重新绘制
}
public String getMessage(){
return message;
}
public int getXCoordinate(){
return xCoordinate;
}
public void setXCoordinate(int x){
this.xCoordinate = x;
repaint();
}
public int getYCoordinate(){
return yCoordinate;
}
public void setYCoordinate(int y){
this.yCoordinate = y;
repaint();
}
public boolean isCentered(){
return centered;
}
public void setCentered(boolean centered){
this.centered = centered;
repaint();
}
public int getInterval(){
return interval;
}
public void setInterval(int interval){
this.interval = interval;
repaint();
}
// public void moveLeft(){
// xCoordinate -= interval;
// repaint();
// }
// public void moveRight(){
// xCoordinate += interval;
// repaint();
// }
// public void moveUp(){
// yCoordinate -= interval;
// repaint();
// }
// public void moveDown(){
// yCoordinate += interval;
// repaint();
// }
public Dimension getPreferredSize(){
return new Dimension(200,30);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(centered){
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth(message);
int stringAscent = fm.getAscent();
xCoordinate = getWidth()/2 - stringWidth/2;
yCoordinate = getHeight()/2 + stringAscent/2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
}