你可以加一个JTextArea,然后用JTextArea.setText(String str)方法把文件内容加上去。import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;import javax.swing.*;
import java.io.*;public class Test
    extends JFrame {
  JPanel pane;
  JScrollPane scrollPane;
  JTextArea textArea;
  public Test() {
    super("pic and text");
    pane = new JPanel();
    textArea = new JTextArea(30,3);
    scrollPane = new JScrollPane(textArea);
    textArea.setEditable(false);
    scrollPane.setPreferredSize(new Dimension(335, 130));
    this.getContentPane().add(pane, BorderLayout.CENTER);
    pane.add(scrollPane, null);
    try {
      //System.in.read( inChar,0,255);
      BufferedReader in = new BufferedReader(new FileReader("c:/1.txt"));
      int n = 0;
      String s;
      while ( (s = in.readLine()) != null) {
        n++;
        if (n == 1) {
          //System.out.println("Title: " + s);
          textArea.setText(s);
          System.out.println("Data");
          continue;
        }        System.out.println(s);
        //super( s );
      }
    }
    catch (Exception e) {
      System.out.println("Read Err");
      e.printStackTrace();
    }
  }  public void paint(Graphics g) {    String s = "haahahaahah";
    super.paint(g);
    Graphics2D Graphics2D = (Graphics2D) g;
    Graphics2D.setPaint(new GradientPaint
                        (5, 30, Color.blue, 35, 100, Color.yellow, true));
    Graphics2D.fill(new Ellipse2D.Double(240, 170, 150, 150));    g.drawString(s.substring(0, 5), 8, 50);
  }  public static void main(String args[]) {    Test application = new Test();
    application.setDefaultCloseOperation(
        JFrame.EXIT_ON_CLOSE);
    application.setSize(425, 460);
    application.setVisible(true);
  }
}

解决方案 »

  1.   

    我看了但是那个文字没有显示出来,实际上我想在public void paint(Graphics g) {
    }
    里面用g.drawString(s.substring(0, 5), 8, 50);实现显示文本,因为文件里面的数据和我下一步显示饼图有关,如何在Shape里面调用g.drawString(s.substring(0, 5), 8, 50)呢?
      

  2.   

    你以前是用System.out.println();来显示的,当然只能出现在Dos窗口了,我对2D类不熟,如果要显示的字符串不长的话,你可以设一个JLabel来显示 label.setText(String str); ,再用绝对定位把JLabel放在你希望出现文字的地方。<------ 树欲静而风不止 ------>
      

  3.   

    我自己已经实现了上部,但现在要求做一个饼图来分析数据,数据已经有了,我如何画饼图呢?
    文件内容:
    First Data Set 
    14 
    12 
    37 
    62
    打开文件后,窗口里面显示2个东西,1,是第一行的title:First Data Set 。2,是一个饼图,饼图里面显示这4个数字的百分比,简单算法如下:
    14+12+37+62=125,14/125*360=40.32 4舍5入成40 就是14这个数字在饼图里面的圆周度数。依次类推,画出这个饼图。import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.*;import javax.swing.*;
    import java.io.*;public class cw2c
           extends JFrame {
      public cw2c(  ) {
           super("pic and text");    
      }
        
    public void paint(Graphics g) {
      try{
          BufferedReader in = new BufferedReader(new FileReader("1.txt"));
          int n = 0;
          String s;
          super.paint(g);
          Graphics2D Graphics2D = (Graphics2D) g;
       // Graphics2D.setPaint(new GradientPaint
       //                     (5, 30, Color.blue, 35, 100, Color.yellow, true));
          Graphics2D.fill(new Ellipse2D.Double(240, 170, 150, 150));
          while ( (s = in.readLine()) != null) {
              n++;
              if (n == 1) {
                 g.drawString( "Title:" + s,8,50 );
                 g.drawString( "Data",8,70 );
                 continue;
              }
            g.drawString(s, 8, 60+10*n );
          }
          }
          catch(Exception e){
                     System.out.println("Read Err");
                     e.printStackTrace();
          }
        }  public static void main(String args[]) {    cw2c application = new cw2c(  );
        application.setDefaultCloseOperation(
                    JFrame.EXIT_ON_CLOSE);
        application.setSize(425, 460);
        application.setVisible(true);
      }
    }