Java 制作一个注册窗口,点击注册按钮然后跳转到登入窗口。
代码如下:
注册窗口:
package shiyan3;import java.awt.event.;import javax.swing.;import java.awt.*;class Data{ static String uname; static String upass;}public class Zhuceck extends JFrame{ Box boxH1,boxH2,boxH3,boxV; JCheckBox checkBox1,checkBox2,checkBox3; JRadioButton radioM,radioF; JTextField jt1,jt2; JComboBox comBox; JButton button; JTextArea text; public Zhuceck() { setLayout(new java.awt.FlowLayout()); init(); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } void init() { setLayout(new FlowLayout()); boxH1=Box.createHorizontalBox(); boxH1.add(new JLabel(“用户名:”)); jt1=new JTextField(14); boxH1.add(jt1); boxH1.add(new JLabel(“密码:”)); jt2=new JTextField(14); boxH1.add(jt2); add(boxH1); boxH2=Box.createHorizontalBox(); boxH2.add(new JLabel(“性别:”)); radioM=new JRadioButton(“男”); radioF=new JRadioButton(“女”); boxH2.add(radioM);boxH2.add(radioF); boxH2.add(new JLabel(" 爱好:")); checkBox1=new JCheckBox(“音乐”); checkBox2=new JCheckBox(“国内旅游”);
checkBox3=new JCheckBox(“阅读”); boxH2.add(checkBox1);boxH2.add(checkBox2);boxH2.add(checkBox3);add(boxH2); comBox=new JComboBox(); boxH3=Box.createHorizontalBox(); boxV=Box.createVerticalBox(); boxH3.add(new JLabel(“职业:”)); comBox.addItem(“学生”); comBox.addItem(“老师”); boxV.add(Box.createVerticalStrut(75)); boxV.add(comBox); boxV.add(Box.createVerticalStrut(75)); boxH3.add(boxV); boxH3.add(new JLabel(“Note:”)); boxH3.add(new JTextArea(10,12)); button=new JButton(“register”); boxH3.add(button); add(boxH3); button.addActionListener(new ActionListener() { private Window oldFrame; @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Data.uname=jt1.getText(); Data.upass=jt2.getText(); if(Data.uname.equals("") || Data.upass.equals("")) JOptionPane.showMessageDialog(jt2, “请注册”, “注册”,JOptionPane.INFORMATION_MESSAGE ); else { setVisible(false); Dengruck dr=new Dengruck();} } }); } public static void main(String args[]) { Zhuceck zh = new Zhuceck(); zh.setBounds(100, 100, 400, 360); zh.setTitle(“注册窗口”); }}
登入窗口:
package shiyan3;import java.awt.event.;import javax.swing.;import java.awt.*;public class Dengruck extends JFrame{ Box boxH1,boxH2; Box boxVOne,boxVTwo; JButton button; JTextField jt3,jt4; String password; String name; public Dengruck() { setLayout(new java.awt.FlowLayout()); init(); setBounds(100,100,260,200); setTitle(“登入窗口”); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } void init() { boxH1=Box.createHorizontalBox(); boxH2=Box.createHorizontalBox(); boxVOne=Box.createVerticalBox(); boxVTwo=Box.createVerticalBox(); boxVOne.add(new JLabel(“用户名:”)); boxVOne.add(new JLabel(“密码:”)); jt3=new JTextField(15); boxVTwo.add(jt3); jt4=new JTextField(15); boxVTwo.add(jt4); boxH1.add(boxVOne); boxH1.add(Box.createHorizontalStrut(10)); boxH1.add(boxVTwo); add(boxH1); boxH2.add(Box.createHorizontalStrut(10)); button=new JButton(“login”); boxH2.add(button); add(boxH2); button.addActionListener(new ActionListener() { @Override public void actactionPerformed(ActionEvent e) { // TODO Auto-generated method stub String name=jt3.getText(); String pass=jt4.getText(); if(name.equals(Data.uname)&&pass.equals(Data.upass)) JOptionPane.showMessageDialog(jt3, “登入成功”, “登入”,JOptionPane.INFORMATION_MESSAGE ); else JOptionPane.showMessageDialog(jt3, “登入失败”,“重新登入”,JOptionPane.INFORMATION_MESSAGE ); } }); } }