关闭

我的JAVA练习代码

标签: javastringyahooexceptionimportnull
1269人阅读 评论(0) 收藏 举报
分类:

我的JAVA练习代码

作者:MATOKU

 

import javax.swing.*;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.*;

public class YahooFrame extends JFrame {

    Thread tr;
    boolean []pass=new boolean[4];

    JPanel contentPane;
    Font font=new Font("Arial", Font.BOLD, 12);
    JLabel jlblTop = new JLabel();
    JLabel jlblimages = new JLabel();
    JLabel jlblid = new JLabel();
    JLabel jlblpswd = new JLabel();
    JLabel jlblRepswd = new JLabel();
    JLabel jlblfn = new JLabel();
    JLabel jlblln = new JLabel();
    JLabel jlblgd = new JLabel();
    JLabel jlblbd = new JLabel();
    JLabel jlblcr = new JLabel();
    JLabel jlbls = new JLabel();
    JTextField jtxtid = new JTextField();
    JPasswordField jtxtpswd = new JPasswordField();
    JPasswordField jtxtRepswd = new JPasswordField();
    JTextField jtxtfn = new JTextField();
    JTextField jtxtln = new JTextField();
    JRadioButton jrbm = new JRadioButton();
    JRadioButton jrbf = new JRadioButton();
    JLabel jlblmail = new JLabel();
    ButtonGroup bgsex = new ButtonGroup();
    JComboBox jcbm = new JComboBox();
    JTextField jtxtd = new JTextField();
    JTextField jtxty = new JTextField();
    JLabel jlblmdy = new JLabel();
    JComboBox jcbcr = new JComboBox();
    JComboBox jcbs = new JComboBox();
    JButton jbtok = new JButton();
    JButton jbtexit = new JButton();
    public YahooFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            implmentsFrame();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void implmentsFrame() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(500, 460));
        setTitle("Yahoo");
        jlblTop.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
        jlblTop.setText("Registration Form");
        jlblTop.setBounds(new Rectangle(162, 13, 178, 36));
        jlblimages.setVerticalAlignment(SwingConstants.CENTER);
        jlblimages.setHorizontalAlignment(SwingConstants.CENTER);
        tr=new Thread(new Runnable(){   //广告轮翻线程
            public void run(){
                String []photo=new String[7];
                photo[0]=new String("yahoo_news");
                photo[1]=new String("yahoo_ent");
                photo[2]=new String("yahoo_fin");
                photo[3]=new String("yahoo_jobs");
                photo[4]=new String("yahoo_main");
                photo[5]=new String("yahoo_mess");
                photo[6]=new String("yahoo_spts");

                while(true){
                    jlblimages.setIcon(new ImageIcon("images//" + photo[(int)(Math.random()*7)]  + ".gif"));
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {}
                }

            }
        });
        tr.start();

        jlblimages.setBounds(new Rectangle(3, 40, 492, 50));
        jlblid.setFont(font);
        jlblid.setText("Yahoo ID:");
        jlblid.setBounds(new Rectangle(22, 91, 57, 29));
        jlblpswd.setFont(font);
        jlblpswd.setText("Password:");
        jlblpswd.setBounds(new Rectangle(21, 123, 67, 28));
        jlblRepswd.setFont(font);
        jlblRepswd.setText("Retype Password:");
        jlblRepswd.setBounds(new Rectangle(21, 156, 108, 27));
        jlblfn.setFont(font);
        jlblfn.setText("First Name:");
        jlblfn.setBounds(new Rectangle(21, 194, 66, 18));
        jlblln.setFont(font);
        jlblln.setText("Last Name:");
        jlblln.setBounds(new Rectangle(21, 224, 69, 22));
        jlblgd.setFont(font);
        jlblgd.setText("Gender:");
        jlblgd.setBounds(new Rectangle(21, 255, 47, 24));
        jlblbd.setFont(font);
        jlblbd.setText("Birthday:");
        jlblbd.setBounds(new Rectangle(21, 292, 54, 21));
        jlblcr.setFont(font);
        jlblcr.setText("Country/Region:");
        jlblcr.setBounds(new Rectangle(21, 323, 92, 22));
        jlbls.setFont(font);
        jlbls.setText("State:");
        jlbls.setBounds(new Rectangle(21, 355, 40, 20));
        jtxtid.setBounds(new Rectangle(162, 96, 115, 20));
        jtxtid.addKeyListener(new KeyAdapter(){   //检查ID首位是否为字母
            public void keyReleased(KeyEvent e){
                String s=jtxtid.getText();
                if (s !="" && s.length()>0) {
                    pass[0]=true;
                    if (!Character.isLetter(s.charAt(0))) {
                        JOptionPane.showConfirmDialog(null, "ID首位应为字母","警告", JOptionPane.YES_OPTION);
                        jtxtid.setText("");
                        pass[0]=false;
                    }
                }
            }
        });

        jtxtpswd.setBounds(new Rectangle(162, 126, 115, 20));
        jtxtRepswd.setBounds(new Rectangle(162, 156, 115, 20));

        class PswdKeyAdapter extends KeyAdapter{   //检查密码是否为8位

            JPasswordField jpswd;
            boolean b;

            public PswdKeyAdapter(JPasswordField jpswd){
                this(jpswd,false);
            }

            public PswdKeyAdapter(JPasswordField jpswd,boolean b) {
                this.jpswd = jpswd;
                this.b=b;
            }

            public void keyReleased(KeyEvent e) {
                String temp = new String("");
                char[] s = jpswd.getPassword();
                int j=0;

                if(s.length>8){
                    j=8;
                }else{
                    j=s.length;
                }
                for (int i = 0; i <j; i++) {
                        temp = temp + s[i];
                }

                if (s.length > 8) {
                    jpswd.setText(temp);
                }

                if (b == true) {
                    pass[1]=true;
                    if(jtxtpswd.getPassword().length==temp.length()){
                        char[] txt;
                        String txt2;

                        txt = jtxtpswd.getPassword();
                        txt2 = String.copyValueOf(txt);

                        if (!txt2.equals(temp)) {
                            JOptionPane.showConfirmDialog(null, "密码不一致", "警告",
                                                          JOptionPane.YES_OPTION);
                            pass[1]=false;
                        }
                    }else if(jtxtpswd.getPassword().length<temp.length()){
                        JOptionPane.showConfirmDialog(null, "密码不一致", "警告",
                                                          JOptionPane.YES_OPTION);
                        pass[1]=false;
                    }
                }
            }
        }

        jtxtpswd.addKeyListener(new PswdKeyAdapter(jtxtpswd));
        jtxtRepswd.addKeyListener(new PswdKeyAdapter(jtxtRepswd,true));

        jtxtfn.setBounds(new Rectangle(162, 190, 115, 20));
        jtxtln.setBounds(new Rectangle(162, 226, 115, 20));
        jrbm.setFont(font);
        jrbm.setText("Male");
        jrbm.setBounds(new Rectangle(162, 254, 54, 25));
        jrbm.setSelected(true);
        jcbm.setFont(font);
        jcbm.addItem("Jan");
        jcbm.addItem("Feb");
        jcbm.addItem("Mar");
        jcbm.addItem("Apr");
        jcbm.addItem("May");
        jcbm.addItem("Jun");
        jcbm.addItem("Jul");
        jcbm.addItem("Aug");
        jcbm.addItem("Sep");
        jcbm.addItem("Oct");
        jcbm.addItem("Nov");
        jcbm.addItem("Dec");
        jcbm.setBounds(new Rectangle(162, 288, 69, 20));
        jtxtd.setBounds(new Rectangle(230, 288, 72, 20));

        jtxtd.addKeyListener(new KeyAdapter(){ //检查日期号是否合法
            public void keyReleased(KeyEvent e){
                pass[2]=true;
                if (!Character.isDigit(e.getKeyChar()) || jtxtd.getText().length()>2 || Integer.parseInt(jtxtd.getText().toString())>31) {
                    jtxtd.setText("");
                    pass[2]=false;
                }
            }
        });

        jtxty.setBounds(new Rectangle(301, 288, 72, 20));

        jtxty.addKeyListener(new KeyAdapter(){ //检查日期月是否合法
            public void keyReleased(KeyEvent e){
                pass[3]=true;
                if (!Character.isDigit(e.getKeyChar()) || jtxty.getText().length()>4 || Integer.parseInt(jtxty.getText().toString())>2006) {
                    jtxty.setText("");
                    pass[3]=false;
                }
            }
        });

        jlblmdy.setFont(font);
        jlblmdy.setText("(Month,DD,yyyy)");
        jlblmdy.setBounds(new Rectangle(380, 289, 90, 19));
        jcbcr.setFont(font);
        jcbcr.setBounds(new Rectangle(162, 321, 146, 20));
        jcbcr.addItem("<Select Country>");
        jcbcr.addItem("Australia");
        jcbcr.addItem("India");
        jcbcr.addItem("United Kingdom");
        jcbcr.addItem("United States");

        jcbcr.addItemListener(new ItemListener(){  //国家下拉框事件
            public void itemStateChanged(ItemEvent e){
                String s=e.getItem().toString();
                jcbs.removeAllItems();

                if(s.equals("Australia")){
                    jcbs.addItem("<Select Country>");
                    jcbs.addItem("New South Wales");
                    jcbs.addItem("Northern Territory");
                    jcbs.addItem("Queensland");
                    jcbs.addItem("South Australia");
                    jcbs.addItem("Tasmania");
                    jcbs.addItem("Wastern Australia");
                }
            }
        });

        jcbs.setFont(font);
        jcbs.setBounds(new Rectangle(162, 355, 146, 20));
        jbtok.setBounds(new Rectangle(149, 403, 74, 20));
        jbtok.setFont(font);
        jbtok.setText("OK");

        jbtok.addActionListener(new ActionListener(){  //确定检查数据是否合法
            public void actionPerformed(ActionEvent e) {
                int i;
                boolean exit=true;

                for(i=0;i<3;i++){
                    if(pass[i]==false){
                        exit=false;
                        break;
                    }
                }

                if(exit==true){
                    JOptionPane.showConfirmDialog(null, "注册成功/n谢谢 " + jtxtid.getText() + "/n密码为 " + String.copyValueOf(jtxtRepswd.getPassword()), "恭喜",
                                                          JOptionPane.YES_OPTION);
                    System.exit(0);
                }else{
                    JOptionPane.showConfirmDialog(null, "注册失败", "失败",
                                                          JOptionPane.YES_OPTION);
                }
            }
        });

        jbtexit.setBounds(new Rectangle(251, 403, 74, 20));
        jbtexit.setFont(font);
        jbtexit.setText("Exit");

        jbtexit.addActionListener(new ActionListener(){  //退出注册
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        bgsex.add(jrbm);
        jrbf.setFont(font);
        jrbf.setText("Female");
        jrbf.setBounds(new Rectangle(248, 255, 67, 24));
        bgsex.add(jrbf);
        jlblmail.setFont(font);
        jlblmail.setText("@Yahoo.com");
        jlblmail.setBounds(new Rectangle(294, 96, 75, 18));
        contentPane.add(jlblmail);
        contentPane.add(jlbls);
        contentPane.add(jlblid);
        contentPane.add(jtxtid);
        contentPane.add(jtxtpswd);
        contentPane.add(jlblRepswd);
        contentPane.add(jlblfn);
        contentPane.add(jlblpswd);
        contentPane.add(jlblln);
        contentPane.add(jtxtRepswd);
        contentPane.add(jtxtfn);
        contentPane.add(jtxtln);
        contentPane.add(jlblgd);
        contentPane.add(jrbm);
        contentPane.add(jrbf);
        contentPane.add(jlblcr);
        contentPane.add(jlblbd);
        contentPane.add(jlblTop);
        contentPane.add(jcbm);
        contentPane.add(jtxtd);
        contentPane.add(jtxty);
        contentPane.add(jlblmdy);
        contentPane.add(jcbcr);
        contentPane.add(jcbs);
        contentPane.add(jbtexit);
        contentPane.add(jbtok);
        contentPane.add(jlblimages);
    }
    public static void main(String[] args) {
        YahooFrame frame = new YahooFrame();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2,
                          (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:36131次
    • 积分:400
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:1篇
    • 译文:0篇
    • 评论:18条
    文章存档
    最新评论