有必要看一看 !

来源:互联网 发布:mac的app store连不上 编辑:程序博客网 时间:2024/04/28 10:17

定义变量的时候,虽然是同一个变量,但是局部变量会屏蔽域变量 !!!一定要注意,我为此吃了不少苦果!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
public class Login
{
 public static void main(String args[])
 {
  LoginFrame frame=new LoginFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//  frame.setResizable(false);
  frame.setVisible(true);
  
 }
}
class LoginFrame extends JFrame
{
 public LoginFrame()
 {
  setTitle("注册");
  Toolkit kit=Toolkit.getDefaultToolkit();
  Dimension screen=kit.getScreenSize();
 // setBounds(screen.width/4,screen.height/4,screen.width/2,screen.height/2);
     setSize(screen.width,screen.height);
  Image img=kit.getImage("1.jpg");
  setIconImage(img);
  GridBagLayout layout=new GridBagLayout();
  JPanel panel=new JPanel();
  
  add(panel);
  Icon icon = new ImageIcon ("2.jpg");
        panel.setBorder(new MatteBorder(200, 400, 200, 400, icon));
  panel.setLayout(layout);
  //添加用户名
  JLabel idlb=new JLabel("用户名");
     idtxt=new JTextField(); //如果写成 JTextField idtxt=new JTextField();  你就有好果子吃了...
//  txtListener txtlistener=new txtListener();
//     idtxt.getDocument().addDocumentListener(txtlistener);
     idlab=new JLabel();
  
    //    idtxt.Maxlength=10; 
        //添加用户密码
  JLabel pw1lb=new JLabel("用户密码");
  JPasswordField pw1txt=new JPasswordField();
  JLabel pw1lab=new JLabel();
  pw1txt.setEchoChar('★');
//  pw1txt.setPreferredSize(new Dimension(200, 21));  
  
  //添加确认密码
  JLabel pw2lb=new JLabel("确认密码");
  JPasswordField pw2txt=new JPasswordField();
  JLabel pw2lab=new JLabel();
  pw2txt.setEchoChar('★');
 // pw2txt.setMaximumSize(pw2txt.getPreferredSize());
 
  
  //添加姓名
  JLabel nmlb=new JLabel("姓名");
  
  JTextField nmtxt=new JTextField();
  JLabel nmlab=new JLabel();
  
  //添加性别
  JLabel sexlb=new JLabel("性别");
     JPanel sexPanel=new JPanel();
  ButtonGroup sex=new ButtonGroup();
  JRadioButton manbt=new JRadioButton("男");
  manbt.setSelected(true);
  manbt.setActionCommand("男");
  JRadioButton wmanbt=new JRadioButton("女");
  wmanbt.setActionCommand("女");
  sexPanel.add(manbt);
  sexPanel.add(wmanbt);
  sexPanel.setBorder(BorderFactory.createEtchedBorder());
  sex.add(manbt);sex.add(wmanbt);
  JLabel sexlab=new JLabel();
  
  //添加年龄
  JLabel agelb=new JLabel("年龄");
  int MINY=1900;
     int MINM=1;
     int MIND=1;
  int MAXY=2008;
  int MAXM=12;
  int MAXD=31;
 
  JPanel agePanel=new JPanel();
  JLabel ageY=new JLabel("年");
  JLabel ageM=new JLabel("月");
  JLabel ageD=new JLabel("日");
  String ageYE[]=new String[MAXY-MINY+1];
  String ageMO[]=new String[MAXM];
  
  yearListener ylistener=new yearListener();
  for(int j=0;MAXY>=MINY;j++)
   ageYE[j]=String.valueOf(MAXY--);
  for(int j=0;MINM<=MAXM;j++)
   ageMO[j]=String.valueOf(MINM++);
   ageYCombo=new JComboBox(ageYE);
//   ageYCombo.setSelectedItem("1987");
   ageMCombo=new JComboBox(ageMO);
//   ageMCombo.setSelectedItem("9");
  
          ageYCombo.addActionListener(ylistener);
       ageMCombo.addActionListener(ylistener);
     
     String ageDA[]=new String[MAXD];
        for(int j=0;MIND<=MAXD;j++)
           ageDA[j]=String.valueOf(MIND++);
      ageDCombo=new JComboBox(ageDA);
     
//      ageDCombo.setSelectedItem("3");
     JLabel agelab=new JLabel();
  agePanel.add(ageYCombo);agePanel.add(ageY);
  agePanel.add(ageMCombo);agePanel.add(ageM);
  agePanel.add(ageDCombo);agePanel.add(ageD);
  
  
   
    
     //添加班级
     JLabel cllb=new JLabel("班级");
     JComboBox clCombo=new JComboBox(new String []{"06大专网络","06物流1班","06大专财会","06大专软件"});
     JLabel cllab=new JLabel();
     //添加身份证
     JLabel Pidlb=new JLabel("身份证");
     JTextField Pidtxt=new JTextField();
     JLabel Pidlab=new JLabel();
    
     //添加家庭住址
     JLabel adrlb=new JLabel("家庭住址");
     JTextField adrtxt=new JTextField();
     JLabel adrlab=new JLabel();
    
     //添加命令按钮
     JButton okbt= new JButton("提交");
     okbt.addActionListener(new oklistener());
    
     JButton cancelbt=new JButton("取消");
     cancelbt.addActionListener(new ActionListener()
     {
      public void actionPerformed(ActionEvent e)
      {
       System.exit(0);
      }
     }
      );
    
     //添加评分
     JLabel grlb=new JLabel("<html><font color=green>请给该窗体打分</font></html>");
     JSlider grsid=new JSlider(0,100,90);
     JLabel grlab=new JLabel();
     grsid.setMajorTickSpacing(20);
     grsid.setMinorTickSpacing(5);
     grsid.setPaintTicks(true);
     grsid.setSnapToTicks(true);
     grsid.setPaintLabels(true);
     grsid.addChangeListener(new Grslistener());
     // 添加到窗体
     panel.add(idlb,new GBC(0,0,1,1).setAnchor(GBC.WEST).setInsets(1));
     panel.add(idtxt,new GBC(1,0,1,1).setFill(GBC.HORIZONTAL).setInsets(4));
     panel.add(idlab,new GBC(2,0,1,1).setAnchor(GBC.EAST).setInsets(1));
    
        panel.add(pw1lb,new GBC(0,1,1,1).setAnchor(GBC.WEST));
     panel.add(pw1txt,new GBC(1,1,1,1).setFill(GBC.HORIZONTAL).setInsets(4));
     panel.add(pw1lab,new GBC(2,1,1,1).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(pw2lb,new GBC(0,2,1,1).setAnchor(GBC.WEST));
     panel.add(pw2txt,new GBC(1,2,1,1).setFill(GBC.BOTH).setInsets(4));
     panel.add(pw2lab,new GBC(2,2,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(nmlb,new GBC(0,3,1,1).setAnchor(GBC.WEST));
     panel.add(nmtxt,new GBC(1,3,1,1).setFill(GBC.BOTH).setInsets(4));
     panel.add(nmlab,new GBC(2,3,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(sexlb,new GBC(0,4,1,1).setAnchor(GBC.WEST));
     panel.add(sexPanel,new GBC(1,4,1,1).setFill(GBC.BOTH).setAnchor(GBC.CENTER));
     panel.add(sexlab,new GBC(2,4,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(agelb,new GBC(0,5,1,1).setAnchor(GBC.WEST));
     panel.add(agePanel,new GBC(1,5,1,1).setFill(GBC.BOTH).setInsets(4));
     panel.add(agelab,new GBC(2,5,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(cllb,new GBC(0,6,1,1).setAnchor(GBC.WEST));
     panel.add(clCombo,new GBC(1,6,1,1).setFill(GBC.BOTH).setInsets(4));
     panel.add(cllab,new GBC(2,6,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(Pidlb,new GBC(0,7,1,1).setAnchor(GBC.WEST));
     panel.add(Pidtxt,new GBC(1,7,1,1).setFill(GBC.BOTH).setInsets(4));
     panel.add(Pidlab,new GBC(2,7,1,1).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(adrlb,new GBC(0,8,1,1).setAnchor(GBC.WEST));
     panel.add(adrtxt,new GBC(1,8,1,1).setFill(GBC.BOTH).setInsets(4));
     panel.add(adrlab,new GBC(2,8,1,1).setFill(GBC.BOTH).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(grlb,new GBC(0,9,1,1).setAnchor(GBC.WEST));
     panel.add(grsid,new GBC(1,9,1,1).setFill(GBC.BOTH).setInsets(4));
     panel.add(grlab,new GBC(2,9,1,1).setAnchor(GBC.EAST).setInsets(1));
    
     panel.add(okbt,new GBC(0,10,1,1).setAnchor(GBC.SOUTH).setInsets(4));
     panel.add(cancelbt,new GBC(1,10,2,1).setAnchor(GBC.SOUTH).setInsets(4));
     this.getRootPane().setDefaultButton(okbt);
 }

 private class yearListener implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
          stry=Integer.parseInt((String)ageYCombo.getSelectedItem());
    strm=Integer.parseInt((String)ageMCombo.getSelectedItem());
          if(tag1)
         {
          ageDCombo.addItem("31");
          tag1=false;
         }
         if(tag2)
         {
          ageDCombo.addItem("30");
          ageDCombo.addItem("31");
          tag2=false;
         }
         if(tag3)
         {
          ageDCombo.addItem("29");
          ageDCombo.addItem("30");
          ageDCombo.addItem("31");
          tag3=false;
         }
       if(strm==4||strm==6||strm==9||strm==11)
       {
              ageDCombo.removeItem("31");
           tag1=true;
       }
           if(((stry%4==0&&stry%100!=0)||stry%400==0)&&strm==2)
           {
                ageDCombo.removeItem("31");
            ageDCombo.removeItem("30");
            tag2=true;
           }
          if(!((stry%4==0&&stry%100!=0)||stry%400==0)&&strm==2)
          {
             ageDCombo.removeItem("31");
           ageDCombo.removeItem("30");
           ageDCombo.removeItem("29");
                tag3=true;
          }
   }
  boolean tag1=false;
  boolean tag2=false;
  boolean tag3=false;
  int stry=1990;
  int strm=1;
  
 }

 
 /* 
    private class txtListener implements DocumentListener
    {
     public void insertUpdate(DocumentEvent e)
     {
         str=idtxt.getText();
          int i=str.length();
         if(i<8)
                 idlab.setText("<html><font color=red>!字符过短!</color></html>");
                 if(i>16)
                  idlab.setText("<html><font color=red>!字符过短!</color></html>");
         }
     public void removeUpdate(DocumentEvent e)
     {
                  str=idtxt.getText();
          int i=str.length();
         if(i<8)
                 idlab.setText("<html><font color=red>!字符过短!</color></html>");
                 if(i>16)
                  idlab.setText("<html><font color=red>!字符过短!</color></html>");
     }
     public void changedUpdate(DocumentEvent e)
     {
      
     }
     
     String str;
    }*/
    private class oklistener implements ActionListener
    {
     
      public void actionPerformed(ActionEvent e)
      {
       if(dialog==null)
          dialog=new RemindDialog(LoginFrame.this);
          dialog.setId(LoginFrame.this.getId());
       dialog.setVisible(true);
       System.out.println(LoginFrame.this.getId());
         }
   }
    private class Grslistener implements ChangeListener
    {
     public void stateChanged(ChangeEvent e)
     {
      JSlider slider=(JSlider)e.getSource();
      value=slider.getValue();
      
     }
    }
    public String getId()
    {
     return idtxt.getText();
    }
    public String getPw()
    {
     return String.valueOf(pw2txt.getPassword());
    }
    public String getNm()
    {
     return nmtxt.getText();
    }
    public String getSex()
    {
     return sex.getSelection().getActionCommand();
    }
    public Date getAge()
    {
     GregorianCalendar calendar=new GregorianCalendar(Integer.parseInt((String)ageYCombo.getSelectedItem()),
     Integer.parseInt((String)ageMCombo.getSelectedItem()),
     Integer.parseInt((String)ageDCombo.getSelectedItem())
     );
     return calendar.getTime();
    }
    public String getCl()
    {
     return (String)clCombo.getSelectedItem();
    }
    public String getPid()
    {
     return Pidtxt.getText();
    }
    public String getAdr()
    {
     return adrtxt.getText();
    }
    public int getGrs()
    {
     return value;
    }
 private JComboBox ageYCombo;
 private JComboBox ageMCombo;
    private JLabel idlab;
 private JLabel pw1lab;
 private JLabel pw2lab;
 private JLabel Pidlab;
 private JLabel grlab;
 private JComboBox ageDCombo;
 private RemindDialog dialog=null;
 private JTextField idtxt;
 private JPasswordField pw2txt;
 private JTextField nmtxt;
    private JTextField Pidtxt;
    private ButtonGroup sex;
    private JRadioButton wmanbt;
    private JRadioButton manbt;
    private JComboBox clCombo;
    private JTextField adrtxt;
    private JSlider grsid;
    private int value;  //保存滑块所取得的值
}

原创粉丝点击