package thread;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PrimeFinder extends JFrame implements Runnable,ActionListener {
/**
* @param args
*/
Thread go;
JLabel howManyLabel;
JTextField howMany;
JButton display;
JTextArea primes;
final static long serialVersionUID = 333333333;
PrimeFinder() {
super("Find prime numbers");
setLookAndFeel();
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout bord = new BorderLayout();
setLayout(bord);
howManyLabel = new JLabel("Quantity: ");
howMany = new JTextField("400", 10);
display = new JButton("Display primes");
primes = new JTextArea(8, 40);
display.addActionListener(this);
JPanel topPanel = new JPanel();
topPanel.add(howManyLabel);
topPanel.add(howMany);
topPanel.add(display);
topPanel.add(primes);
add(topPanel, BorderLayout.NORTH);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
display.setEnabled(false);
if(go == null) {
go = new Thread(this);
go.start();
}
}
public void run() {
int quantity = Integer.parseInt(howMany.getText());
System.out.println(quantity);
int numPrimes = 0;
int candidate = 2;
primes.append("First " + quantity + " primes:");
while(numPrimes<quantity) {
if(isPrime(candidate)) {
primes.append(candidate + " ");
numPrimes++;
System.out.println(candidate+",");
}
candidate ++;
}
}
public static boolean isPrime(int checkNumber) {
double root = Math.sqrt(checkNumber);
for(int i= 2; i<=root;i++) {
if(checkNumber % i == 0) {
return false;
}
}
return true;
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Exception exc) {
}
}
public static void main(String[] args) {
PrimeFinder fp = new PrimeFinder();
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PrimeFinder extends JFrame implements Runnable,ActionListener {
/**
* @param args
*/
Thread go;
JLabel howManyLabel;
JTextField howMany;
JButton display;
JTextArea primes;
final static long serialVersionUID = 333333333;
PrimeFinder() {
super("Find prime numbers");
setLookAndFeel();
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout bord = new BorderLayout();
setLayout(bord);
howManyLabel = new JLabel("Quantity: ");
howMany = new JTextField("400", 10);
display = new JButton("Display primes");
primes = new JTextArea(8, 40);
display.addActionListener(this);
JPanel topPanel = new JPanel();
topPanel.add(howManyLabel);
topPanel.add(howMany);
topPanel.add(display);
topPanel.add(primes);
add(topPanel, BorderLayout.NORTH);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
display.setEnabled(false);
if(go == null) {
go = new Thread(this);
go.start();
}
}
public void run() {
int quantity = Integer.parseInt(howMany.getText());
System.out.println(quantity);
int numPrimes = 0;
int candidate = 2;
primes.append("First " + quantity + " primes:");
while(numPrimes<quantity) {
if(isPrime(candidate)) {
primes.append(candidate + " ");
numPrimes++;
System.out.println(candidate+",");
}
candidate ++;
}
}
public static boolean isPrime(int checkNumber) {
double root = Math.sqrt(checkNumber);
for(int i= 2; i<=root;i++) {
if(checkNumber % i == 0) {
return false;
}
}
return true;
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Exception exc) {
}
}
public static void main(String[] args) {
PrimeFinder fp = new PrimeFinder();
}
}
点击display primes button 运行效果