java窗口输出_java 将输出的信息显示到一个窗口中

该博客探讨了如何修改Java代码,将原本在多个窗口中显示的随机读取文本文件内容整合到一个窗口中展示。代码示例中,通过`JOptionPane.showInputDialog`获取用户输入,并使用`Random`生成数组,读取指定行数的文本文件内容。目前的实现每个读取的行都会打开新的窗口,问题在于如何集中这些输出。作者寻求解决方案来统一所有输出到一个窗口。
摘要由CSDN通过智能技术生成

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();

}

}

这是一段随机读取文本文件多行的代码 但是输出是多个窗口 怎么将所有的输出都在一个窗口上显示

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值