
java 将输出的信息显示到一个窗口中
classText{publicstaticvoidmain(String[]args)throwsIOException{intn=Integer.parseInt(J...
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();
}
}
这是一段随机读取文本文件多行的代码 但是输出是多个窗口 怎么将所有的输出都在一个窗口上显示 展开
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();
}
}
这是一段随机读取文本文件多行的代码 但是输出是多个窗口 怎么将所有的输出都在一个窗口上显示 展开
1个回答
展开全部
import java.io.*;
import java.util.*;
import javax.swing.*;
public 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--;
}
}
StringBuilder sb = new StringBuilder();
// 读取指定行的内容
for (int i = 0; i < n; i++) {
sb.append(getFileString("D:/abc.txt", a[i]));
}
new ReadSelectedLine(sb.toString());//新建界面写到了for循环里,就会出现很多的文本框
//只需要一个界面,界面需要一个参数就是字符串参数显示到文本框
}
//把读取文件内容的文本的代码提取成一个方法,用于读取文字
static String getFileString(String fileName, int lineNumber) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line = reader.readLine();
int num = 0;
while (line != null) {
if (lineNumber == ++num) {
sb.append(line);
sb.append("\r\n");
}
line = reader.readLine();
}
} catch (Exception e) {
}
return sb.toString();
}
}
//界面
class ReadSelectedLine extends JFrame {
private static final long serialVersionUID = 1L;
JTextArea ta = null;
JScrollPane jsp = null;
//只要一个参数
ReadSelectedLine(String txt) 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(txt);
add(jsp);
validate();
}
}
追问
能改字体大小吗
追答
................
ta = new JTextArea();
ta.setFont(new Font("黑体",Font.BOLD,30));//这就是设置字体
jsp = new JScrollPane(ta);
.........................
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询