//在面板上显示一条信息
import javax.swing.*;
import java.awt.*;
//import java.awt.event.*;
import java.awt.geom.*;
 
 public class SimpleFrameTest
{
public static void main(String[] args)
{
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
} //框架类
class SimpleFrame extends JFrame
{
public SimpleFrame()
{

 
setTitle("This is a Title");
setSize(WIDTH,HEIGHT);
  
//加入一个面板到框架

  HelloPanel panel = new HelloPanel ();
  Container contentPane = getContentPane();
  
  contentPane.add(panel);
 
}
  public static final int WIDTH = 400;
  public static final int HEIGHT = 400;
}

解决方案 »

  1.   

    就这些,我就是要让g.drawString("Hello this is message",MESSAGE_X,MESSAGE_Y);显示出来,可是出来的窗口空白的  why?
      

  2.   

    HelloPanel  是什么类型的  好像不对吧
      

  3.   

    请楼主把问题写详细点,HelloPanel类的代码也没给出来,又没提及画图的函数
      

  4.   

    呵呵,这是道填空题import javax.swing.*;
    import java.awt.*;
    //import java.awt.event.*;
    import java.awt.geom.*;
     
     public class SimpleFrameTest
    {
    public static void main(String[] args)
    {
    SimpleFrame frame = new SimpleFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    } //框架类
    class SimpleFrame extends JFrame
    {
    public SimpleFrame() {
     
    setTitle("This is a Title");
    setSize(WIDTH,HEIGHT);
      
    //加入一个面板到框架
    HelloPanel panel = 
    new HelloPanel("Hello this is message", 100, 100);
    Container contentPane = getContentPane();
      
    contentPane.add(panel);
     
    } class HelloPanel extends JPanel{
    String msg = null;
    int MESSAGE_X, MESSAGE_Y; HelloPanel(String msg, int x, int y){
    super();
    this.msg = msg;
    MESSAGE_X = x;
    MESSAGE_Y = y;
    }
    public void paint(Graphics g){
    g.drawString(msg, MESSAGE_X, MESSAGE_Y);
    }
    }  public static final int WIDTH = 400;
      public static final int HEIGHT = 400;
    }