编写一个应用程序,实现下列要求:
主窗口尺寸为400×400,设置标题和个性化图标
使用文本域输入一个整数 n(n>=2)
使用一个按钮,当点击时判断 n 是否为素数

解决方案 »

  1.   

    你是刚学GUI吗?
    这个问题自己解决吧~~
      

  2.   

    我是初学都 ,自己设计的不好看。献丑了。
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JLabel;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    public class primeNumber{
    public static void main(String[] args){ 
    primeNoFrame win=new primeNoFrame();
    win.setVisible(true);
    }
    }class primeNoFrame extends JFrame{
    private JButton prime;
    private JTextField No;
    private JLabel lbl,msg;

    primeNoFrame(){
    super("判断是否为素数"); setSize(400, 400);
    Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(null); prime = new JButton("检 验 (OK)");
    prime.setMnemonic('O');
    prime.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    try {
    int i=Integer.parseInt(No.getText().trim());
    if(i<2) {
    msg.setText("<html><font color= red>您输入的数小于2  !");
    return;
    }
    boolean notPrime=true;
    for(int j=2;j<=(int)Math.sqrt(i);j++){
    if(i%j==0){
    msg.setText("<html><font color= red>不是素数!");
    notPrime=false;
    }
    }
    if(notPrime)
    msg.setText("<html><font color=red>恭喜您,是素数!");
    }
    catch(Exception err){
    msg.setText("<html><font color= red>数据不合法!");
    }
    }
    });

    prime.setBounds(80,200,230,20);
    prime.setFocusable(false);

    lbl=new JLabel("请输入>=2的数值:");
    lbl.setBounds(80,150,150,20);

    msg=new JLabel("",JLabel.CENTER);
    msg.setBounds(80,175,230,20);

    No=new JTextField();
    No.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    prime.doClick();
    }
    });
    No.setBounds(210,150,100,20);

    getContentPane().add(lbl);
    getContentPane().add(No);
    getContentPane().add(msg);
    getContentPane().add(prime);
    setResizable(false);
    setLocation((screenSize.width-400)/2,(screenSize.height-400)/2);
    }
    }