编写图形界面下的Java Swing程序,接受用户输入的两个数据为上下限,然后输出上、下限之间的所有素数。(结果部分每行显示10个数据)

这个代码我整体写的比较简单,读者可以简单参考一下,对人家题目要求略微修改了一下,多加了一个“置空”按钮。下图为我设计的界面

运行程序后的截图请看我后面的截图:

package com.wangshilin.study.app_127;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;

/**
* 通过简单的Swing组件实现求两个数之间的素数!
* @author wangshilin
*
*/

public class Demo_app {

private JFrame frame;
private JTextField xiaxian;
private JTextField shangxian;
private JTextArea textArea;
private JLabel label;
private JLabel label_1;
private JScrollPane scrollPane_1;
private JButton bt;
private JButton bt_chongzhi;

public String sushu(int a, int b) {
String string ="";
int num = 0;
for (int i = a; i <= b; i++) {
boolean flag = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0)
flag = false;
}
if (flag == true) {
string += Integer.toString(i) + " ";
num++;
}
if (num == 10) {
string += " ";
num = 0;
}
}

return string;
}

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Demo_app window = new Demo_app();
window.frame.setVisible(true);

/**
* 给执行按钮添加事件
*/
window.bt.addActionListener(new ActionListener() {// 给按钮添加事件接收器
@Override
public void actionPerformed(ActionEvent e) {// 接受到事件后,进行下面的处理
String show=""; //显示的信息
int a = Integer.parseInt(window.xiaxian.getText());
int b = Integer.parseInt(window.shangxian.getText());
if (a<=1 || b<=1) {
window.textArea.setText("您输入的数据不合法!请检查重新输入!您输入的数必须大于或等于2");
}else if (a >=2 && a>=2 && a > b) {
show = window.sushu(b,a);
window.textArea.setText(show);
}else{
show = window.sushu(a,b);
window.textArea.setText(show);
}
}
});

/**
* 给重置按钮添加事件
*/
window.bt_chongzhi.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
window.textArea.setText(null);
window.xiaxian.setText(null);
window.shangxian.setText(null);
}
});

} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
private Demo_app() {
initialize();
}

/**
* Initialize the contents of the frame.
*/

private void initialize() {
frame = new JFrame();
frame.setTitle("u6211u7684u5C0Fu7A0Bu5E8F");
frame.setBounds(100, 100, 973, 553);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

xiaxian = new JTextField();
xiaxian.setToolTipText("text");
xiaxian.setBounds(80, 57, 166, 27);
frame.getContentPane().add(xiaxian);
xiaxian.setColumns(10);

shangxian = new JTextField();
shangxian.setBounds(289, 57, 166, 27);
frame.getContentPane().add(shangxian);
shangxian.setColumns(10);

label = new JLabel("u4E0Bu9650");
label.setFont(new Font("隶书", Font.BOLD, 24));
label.setBounds(80, 27, 81, 21);
frame.getContentPane().add(label);

label_1 = new JLabel("u4E0Au9650");
label_1.setFont(new Font("隶书", Font.BOLD, 22));
label_1.setBounds(290, 27, 81, 21);
frame.getContentPane().add(label_1);

scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(80, 146, 787, 313);
frame.getContentPane().add(scrollPane_1);

textArea = new JTextArea();
textArea.setFont(new Font("楷体", Font.BOLD, 14));
textArea.setTabSize(14);
textArea.setBackground(Color.LIGHT_GRAY);
scrollPane_1.setViewportView(textArea);


bt = new JButton("u6267u884C");
bt.setFont(new Font("楷体", Font.BOLD, 22));
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
bt.setBounds(748, 56, 123, 58);
frame.getContentPane().add(bt);

JLabel label_2 = new JLabel("u663Eu793Au57DF");
label_2.setFont(new Font("楷体", Font.BOLD, 23));
label_2.setBounds(80, 119, 81, 21);
frame.getContentPane().add(label_2);

bt_chongzhi = new JButton("u91CDu7F6E");
bt_chongzhi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
bt_chongzhi.setFont(new Font("楷体", Font.BOLD, 22));
bt_chongzhi.setBounds(596, 56, 123, 58);
frame.getContentPane(http://www.my516.com).add(bt_chongzhi);
}
}


--------------------- 

原文地址:https://www.cnblogs.com/hyhy904/p/11007510.html