按钮事件,判断从文本框中输入的数是否为素数
来源:互联网 发布:thinkphp网站源码 编辑:程序博客网 时间:2024/05/29 09:03
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package homework;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Administrator
*/
public class PrimeJudge extends JFrame implements ActionListener {
private JTextField inputPrime;
private JButton judgePrime;
/** Creates a new instance of PrimeJudge */
public PrimeJudge() {
this.setTitle("素数判断");
this.setSize(400, 400);
this.setIconImage(new ImageIcon("qsp.gif").getImage());
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(PrimeJudge.this, "Are you sure?", "Exit?", JOptionPane.YES_NO_OPTION)) {
System.exit(0);
}
}
});
this.buildContent();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private void buildContent() {
inputPrime = new JTextField(10);
judgePrime = new JButton("判断是否为素数");
judgePrime.addActionListener(this);
JPanel panel = new JPanel();
panel.add(new JLabel("请输入一个数(n>=2)"));
panel.add(inputPrime);
panel.add(judgePrime);
this.getContentPane().add(panel, "North");
}
public static boolean isPrime(int n) {
for (int i = 2; i <= (int) Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == judgePrime) {
try {
int n = Integer.parseInt(inputPrime.getText());
if (n < 2) {
JOptionPane.showMessageDialog(this, "这个数 < 2 !");
return;
}
boolean is = PrimeJudge.isPrime(n);
JOptionPane.showMessageDialog(this, n + (is ? " i 是" : " 不是") + " 一个素数.");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
}
public static void test() {
new PrimeJudge();
}
public static void main(String[] args) {
PrimeJudge.test();
}
}
- 按钮事件,判断从文本框中输入的数是否为素数
- 输入一个数,判断是否为素数
- 输入一个数判断是否为素数
- 判断输入的一个数是否为素数
- 判断一个输入的数是否为素数
- 判断输入一个数是否为素数的一般算法
- 判断输入的数是否是素数
- 循环输入一个数,判断是否为素数。
- .循环输入一个数,判断是否为素数.Cpp
- .循环输入一个数,判断是否为素数
- 输入一个数,判断是否为素数(C语言)
- 判断从2到输入数字间的数字是否为素数
- MFC 判断文本框输入的值是否为数字
- MFC 判断文本框输入的值是否为数字
- MFC 判断文本框输入的值是否为数字
- 判断文本框中输入的是否为数字,不是则清除
- 判断文本框输入字符是否为数字
- 判断文本框输入是否为数字
- 资源(论文、文学等)
- Cygwin完全下载指南[图]
- 成员变量和局部变量
- 做一个合格软件工程师基本的素质
- iscis客户端问题
- 按钮事件,判断从文本框中输入的数是否为素数
- MsChart 的使用 (VC)
- 代理模式
- IC设计入门注意
- ORACLE 操作索引语句
- apache2+mysql+php+phpmyadmin配置
- 我的25年嵌入式生涯[转]
- 又做起了老本行—在讲台上。
- 如何让CPU使用率固定在50%---我做微软面试题的心得