//MessagePanel.java
import javax.swing.*;
import java.awt.*;
public class MessagePanel extends JPanel{
private String message="Hello World";
private int x=20;
private int y=20;
private boolean centered;
public MessagePanel(){

}
public MessagePanel(String message){
this.message=message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isCentered() {
return centered;
}
public void setCentered(boolean centered) {
this.centered = centered;
}
public void paintComponent(Graphics g){
super.paintComponents(g);
g.setColor(Color.red);
if(centered){
FontMetrics fm=g.getFontMetrics();
int w=fm.stringWidth(message);
int h=fm.getAscent();
x=(getSize().width-w)/2;
y=(getSize().height+h)/2;
}
g.drawString(message, x,y);
}
}
//TestFrame.java
import java.awt.*;
import javax.swing.*;
public class TestFrame extends JFrame {
public TestFrame() {
MessagePanel messagePanel = new MessagePanel("Hello World");
messagePanel.setFont(new Font("ScansSerif", Font.ITALIC, 20));
messagePanel.setCentered(true);
getContentPane().add(messagePanel);
} public static void main(String[] args){
TestFrame frame=new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setTitle("Hello");
frame.setVisible(true);
}
}
要求用红色、ScanSeri、斜体、20磅的规格居中显示信息Hello World,我运行的时候怎么字一闪而过,g.drawString(message, x,y);改成g.drawString(message, 20,20);怎么就行了,求解决,谢谢。

解决方案 »

  1.   

    public void paintComponent(Graphics g){
    super.paintComponents(g);
    g.setColor(Color.red);
    if(centered){
    FontMetrics fm=g.getFontMetrics();
    int w=fm.stringWidth(message);
    int h=fm.getAscent();
    x=(getSize().width-w)/2;
    y=(getSize().height+h)/2;
    g.drawString(message, 80,70);
    System.out.println("x: " + x);
    System.out.println("y: " + y);
    System.out.println("w: " + w);
    System.out.println("h: " + h);
    System.out.println("width:"+getSize().width);
    System.out.println("height:"+getSize().height);
    }
    我把你的这个类加了一些用于调试的语句
    x: 89
    y: 91
    w: 105
    h: 21
    width:284
    height:162
    这是输出结果,我把参数改成80,70刚好在右下角落里显示。显示不了的原因,应该是你计算有问题。
      

  2.   

    this.x=(getSize().width-w)/2;
    this.y=(getSize().height+h)/2;这样写试试看
      

  3.   

    g.drawString(message, x,y);不改的话,加上调试语句System.out.println("x: " + x);
    System.out.println("y: " + y);
    System.out.println("w: " + w);
    System.out.println("h: " + h);
    System.out.println("width:"+getSize().width);
    System.out.println("height:"+getSize().height);
    怎么还是一闪而过,而且调试语句怎么输出两次?
    2楼的方法好像也不行。
    那么,怎么居中显示信息Hello World呢。
    还有,Jpanel类对象的getSize()返回的是什么的宽和高?
      

  4.   

    楼主的代码写得太乱,我改了一下,你试试我这个
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.font.*;class MessagePanel extends JPanel{
    private double x;
    private double y;
    private double basey;
    private double ascent;
    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    String message = new String("Hello World");
    Font f = new Font("Serif", Font.BOLD, 25);
    g2.setColor(Color.red);
    g2.setFont(f);
    //测量字体
    FontRenderContext content = g2.getFontRenderContext();
    Rectangle2D bounds = f.getStringBounds(message, content);
    x = (getWidth() - bounds.getWidth()) / 2;
    y = (getHeight() - bounds.getHeight()) / 2;
    ascent = -bounds.getY();
    basey = y + ascent;
    //打印出message
    g2.drawString(message, (int)x, (int)basey);
    g2.setPaint(Color.GRAY);
    }
    }
    class TestFrame extends JFrame {
    public TestFrame() {
    MessagePanel messagePanel = new MessagePanel();
    messagePanel.setFont(new Font("ScansSerif", Font.ITALIC, 20));
    getContentPane().add(messagePanel);
    }public static void main(String[] args){
    TestFrame frame=new TestFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,200);
    frame.setTitle("Hello");
    frame.setVisible(true);
    }}
      

  5.   

    我改了一下你的代码,其中有些没有影响到最后显示的方法我没有写。import java.awt.*;import javax.swing.*;
    import javax.swing.border.*;class MessagePanel extends JPanel {

    private String message = "hello world";
    private boolean centered;

    public MessagePanel() {
        setFont(new Font("ScansSerif", Font.ITALIC, 20));
    }

    public void setCentered(boolean centered) {
        this.centered = centered;
    }
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);


    g.setColor(Color.RED);

    if(centered == true){
    FontMetrics fm=g.getFontMetrics();
    int w=fm.stringWidth(message);
    int h=fm.getAscent();
    g.drawString(message, getWidth()/2 - w/2, getHeight()/2 + h/2);
    }

    }
    }class MyFrame extends JFrame {
    public MyFrame() {
    MessagePanel panel = new MessagePanel();
    panel.setCentered(true);
    add(panel);
    }
    }
    public class Test3 {
    public static void main(String[] args) {
    MyFrame frame = new MyFrame();


    frame.setSize(300, 200);
    frame.setVisible(true);
    }}
      

  6.   

    说实话这种程度的swing编程我都用matisse
      

  7.   

    怎么还是一闪而过,而且调试语句怎么输出两次?
    2楼的方法好像也不行。
    那么,怎么居中显示信息Hello World呢。
    还有,Jpanel类对象的getSize()返回的是什么的宽和高?输出两次,因为这个方法调用了两次。你的程序结构不太好,我参考书上的,重新编了一个,你看看。
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;public class FontTest
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(new Runnable()
             {
                public void run()
                {
                   FontFrame frame = new FontFrame();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setVisible(true);
                }
             });
       }
    }/**
     * 框架的构建
     */
    class FontFrame extends JFrame
    {
       public FontFrame()
       {
          setTitle("Hello");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);      // 加上框架的组件      FontComponent component = new FontComponent();
          add(component);
       }   public static final int DEFAULT_WIDTH = 300;
       public static final int DEFAULT_HEIGHT = 200;
    }/**
     *
     */
    class FontComponent extends JComponent
    {
       public void paintComponent(Graphics g)
       {
          Graphics2D g2 = (Graphics2D) g;      String message = "Hello, World!";      Font f = new Font("Serif", Font.BOLD, 36);
          g2.setFont(f);
          g2.setPaint(Color.RED);
          // 计算信息的大小      FontRenderContext context = g2.getFontRenderContext();
          Rectangle2D bounds = f.getStringBounds(message, context);//返回的是一个矩形      // 计算信息开始写的地方,即信息矩形的左上角坐标(x,baseY)      double x = (getWidth() - bounds.getWidth()) / 2;
          double y = (getHeight() - bounds.getHeight()) / 2;
          double ascent = -bounds.getY();//计算上坡度
          double baseY = y + ascent;
          g2.drawString(message, (int) x, (int) baseY);   
       }
    }