首先建一个文本文件,比如在D盘根目录下,名字为:int.txt,然后写一个Java类CalcNumber.java。
****************************************************************
int.txt里面的文本如下:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
****************************************************************
CalcNumber.java类代码如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
public class CalcNumber extends JFrame {
JTextArea in;
JTextArea suText;
JTextArea heText;
public CalcNumber() {
this.setTitle("计算素数和合数");
// 输入框
in = new JTextArea(4, 1);
in.setAutoscrolls(true);
JScrollPane inScroll = new JScrollPane(in);
in.setBorder(BorderFactory.createBevelBorder(1));
JPanel upper = new JPanel(new BorderLayout());
upper.add(inScroll);
// 按钮
JButton imp = new JButton("导入文本");
imp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedReader br = new BufferedReader(new FileReader(
"d:\\int.txt"));
String s;
while ((s = br.readLine()) != null) {
in.setText(in.getText() + "\n" + s);
}
br.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
});
JButton start = new JButton("启动两个线程并计算");
start.addActionListener(new CalcActionListener());
JPanel buttonP = new JPanel();
buttonP.add(imp);
buttonP.add(start);
// 结果
JPanel result = new JPanel(new GridLayout(1, 2));
JPanel suPanel = new JPanel();
suPanel.setBorder(BorderFactory.createTitledBorder("以下为素数"));
JPanel hePanel = new JPanel();
hePanel.setBorder(BorderFactory.createTitledBorder("以下为合数"));
result.add(suPanel);
result.add(hePanel);
suText = new JTextArea(4, 25);
JScrollPane suScroll = new JScrollPane(suText);
suText.setBorder(BorderFactory.createBevelBorder(1));
heText = new JTextArea(4, 25);
JScrollPane heScroll = new JScrollPane(heText);
heText.setBorder(BorderFactory.createBevelBorder(1));
suPanel.add(suScroll);
hePanel.add(heScroll);
// 总
JPanel all = new JPanel(new GridLayout(2, 1, 2, 2));
all.setBorder(BorderFactory.createTitledBorder("计算素数和合数"));
all.add(upper);
all.add(result);
this.add(buttonP, BorderLayout.SOUTH);
this.add(all, BorderLayout.CENTER);
// this.setSize(400, 300);
this.pack();
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension screen = tool.getScreenSize();
this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
/ 2 - this.getHeight() / 2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class CalcActionListener implements ActionListener, Runnable {
public void actionPerformed(ActionEvent e) {
Thread suThread = new Thread(new CalcActionListener(), "su");
Thread heThread = new Thread(new CalcActionListener(), "he");
suThread.start();
heThread.start();
}
public void run() {
String s = in.getText();
String[] arr = s.split("\\s");
StringBuffer su = new StringBuffer();
StringBuffer he = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
boolean flag = false;
if (!arr[i].equals("") && arr[i] != null) {
int n = Integer.valueOf(arr[i]);
for (int j = 2; j < n; j++) {
if (n % j == 0) {
flag = true;
}
}
if (Thread.currentThread().getName().equals("su")
&& flag == false) {
su = su.append(" " + n);
}
if (Thread.currentThread().getName().equals("he")
&& flag == true) {
he = he.append(" " + n);
}
}
}
if (Thread.currentThread().getName().equals("su")) {
suText.setText(new String(su));
} else {
heText.setText(new String(he));
}
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
CalcNumber c = new CalcNumber();
}
}