设计一个Java应用程序,统计整数2~n以内所有素数的个数,程序运行界面如图2-1所示。具体要求: 1)用户在文本框中输入一个整数n(1000000=<n<=900000000); 2)设计两个线程,第一个线程用来计算2~n/2之间的素数的个数,第二个线程用来计算n/2~n之间的素数的个数,点击“启动线程”,两个文本框分别实时显示素数个数,如图2-2所示。当两个线程计算结束时在下面文本框显示素数的总个数,如图2-3所示。 3)线程运行过程中,可以随时点击“关闭线程”关闭正在运行的线程,同时清空3个文本框,按钮恢复到初始状态。
时间: 2024-03-23 17:37:49 浏览: 139
好的,根据您的要求,我为您提供Java代码实现如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PrimeNumberCounter extends JFrame implements ActionListener {
private JTextField inputField;
private JTextField firstHalfField;
private JTextField secondHalfField;
private JTextField totalField;
private JButton startButton;
private JButton stopButton;
private Thread firstThread;
private Thread secondThread;
public PrimeNumberCounter() {
setTitle("Prime Number Counter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
JPanel inputPanel = new JPanel(new FlowLayout());
JLabel inputLabel = new JLabel("Enter a number:");
inputField = new JTextField(10);
inputPanel.add(inputLabel);
inputPanel.add(inputField);
JPanel buttonPanel = new JPanel(new FlowLayout());
startButton = new JButton("Start Thread");
startButton.addActionListener(this);
stopButton = new JButton("Stop Thread");
stopButton.addActionListener(this);
stopButton.setEnabled(false);
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
JPanel resultPanel = new JPanel(new GridLayout(4, 1));
JLabel firstHalfLabel = new JLabel("Prime numbers in 2~n/2:");
firstHalfField = new JTextField(10);
firstHalfField.setEditable(false);
JLabel secondHalfLabel = new JLabel("Prime numbers in n/2~n:");
secondHalfField = new JTextField(10);
secondHalfField.setEditable(false);
JLabel totalLabel = new JLabel("Total prime numbers:");
totalField = new JTextField(10);
totalField.setEditable(false);
resultPanel.add(firstHalfLabel);
resultPanel.add(firstHalfField);
resultPanel.add(secondHalfLabel);
resultPanel.add(secondHalfField);
resultPanel.add(totalLabel);
resultPanel.add(totalField);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(resultPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
int n = Integer.parseInt(inputField.getText());
if (n < 2) {
JOptionPane.showMessageDialog(this, "Please enter an integer greater than or equal to 2.");
return;
}
firstThread = new PrimeNumberThread(2, n / 2);
secondThread = new PrimeNumberThread(n / 2 + 1, n);
firstThread.start();
secondThread.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
} else if (e.getSource() == stopButton) {
firstThread.interrupt();
secondThread.interrupt();
inputField.setText("");
firstHalfField.setText("");
secondHalfField.setText("");
totalField.setText("");
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
private class PrimeNumberThread extends Thread {
private int start;
private int end;
private int count;
public PrimeNumberThread(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public void run() {
count = 0;
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
count++;
}
if (Thread.interrupted()) {
return;
}
}
if (Thread.currentThread() == firstThread) {
firstHalfField.setText(String.valueOf(count));
} else if (Thread.currentThread() == secondThread) {
secondHalfField.setText(String.valueOf(count));
}
int total = Integer.parseInt(firstHalfField.getText())
+ Integer.parseInt(secondHalfField.getText());
totalField.setText(String.valueOf(total));
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
private boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
public static void main(String[] args) {
PrimeNumberCounter counter = new PrimeNumberCounter();
counter.setVisible(true);
}
}
```
该程序使用了Java Swing库构建了一个GUI界面,其中包含一个文本框和两个按钮,用于输入整数和启动/关闭线程。另外,还包含三个文本框,分别用于显示两个线程计算出的素数个数和总的素数个数。
当用户点击“启动线程”按钮时,程序会创建两个线程,分别计算2~n/2和n/2~n之间的素数个数,并实时更新两个文本框的显示。当两个线程计算结束后,程序会累加素数个数,并将结果显示在第三个文本框中。
当用户点击“关闭线程”按钮时,程序会中断正在运行的线程,并清空所有文本框的内容,同时将按钮状态恢复到初始状态。
阅读全文