你可以加一个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);
}
}
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);
}
}
}
里面用g.drawString(s.substring(0, 5), 8, 50);实现显示文本,因为文件里面的数据和我下一步显示饼图有关,如何在Shape里面调用g.drawString(s.substring(0, 5), 8, 50)呢?
文件内容:
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);
}
}