classText{publicstaticvoidmain(String[]args)throwsIOException{intn=Integer.parseInt(JOptionPane.showInputDialog("inputanumber:"));Randomr=newRandom();int[]a=newint[50];//...
class Text{
public static void main(String[] args) throws IOException {
int n = Integer
.parseInt(JOptionPane.showInputDialog("input a number:"));
Random r = new Random();
int[] a = new int[50];
// 指定读取的行号
for (int i = 0; i < n; i++) {
a[i] = r.nextInt(30) + 0;
for (int j = i - 1; j >= 0; j--) {
if (a[i] == a[j])
i--;
}
}
// 读取指定行的内容
for (int i = 0; i < n; i++)
new ReadSelectedLine("E:/测试/11.txt", a[i]);
}
}
class ReadSelectedLine extends JFrame {
private static final long serialVersionUID = 1L;
JTextArea ta = null;
JScrollPane jsp = null;
ReadSelectedLine(String fileName, int lineNumber) throws IOException {
this.setVisible(true);
this.setBounds(500, 200, 800, 600);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
ta = new JTextArea();
jsp = new JScrollPane(ta);
ta.setText(null);
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName)));
String line = reader.readLine();
int num = 0;
while (line != null) {
if (lineNumber == ++num) {
ta.append(line);
ta.append("\r\n");
}
line = reader.readLine();
add(jsp);
validate();
}
reader.close();
}
}
这是一段随机读取文本文件多行的代码 但是输出是多个窗口 怎么将所有的输出都在一个窗口上显示
展开