import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;public class LoginGUI extends JFrame implements ActionListener{
JTextField userID=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton ok=new JButton("登录");
JButton regit=new JButton("注册");
JButton cancel=new JButton("取消");
JButton exit=new JButton("退出");
Connection conn;
JFrame frame=new JFrame();

public LoginGUI()
{
setTitle("用户登录");
Container c=getContentPane();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.add(new JLabel("用户名: "));
p1.add(userID);
p1.add(new JLabel("密码:"));
p1.add(password);


p2.add(ok);
p2.add(regit);
p2.add(cancel);
p2.add(exit);
c.setLayout(new GridLayout(2,1));
c.add(p1);
c.add(p2);

ok.addActionListener(this);
regit.addActionListener(this);
cancel.addActionListener(this);
exit.addActionListener(this); setSize(400,300); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok){
try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );

Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Login where userID='"+userID.getText()+"' and password='"+new String(password.getPassword())+"'");
if(rs.next()) JOptionPane.showMessageDialog(null,"登录成功!");
else JOptionPane.showMessageDialog(null,"密码或用户名不对!");
conn.close();
}catch(Exception ex){System.out.println(ex);}
}
if(e.getActionCommand().equals("注册"))
{

}
if(e.getActionCommand().equals("取消")){
userID.setText("");
password.setText("");

}
if(e.getActionCommand().equals("退出")){
System.exit(0);
}
}
public static void main(String[] s){
new LoginGUI();
}
}我做了一个登录界面连数据库的。
我又做了一个简单的注册见面
但是我不知道怎么样把两者链接起来。就是说在登录界面上一按注册按钮 就会弹出注册界面;~~~~~~~~~~~~~~谢谢!
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.sql.*;
public class RegitGUI extends  JFrame implements ActionListener
{  public static void main(String[] s){  
    RegitGUI frame=new RegitGUI();
frame.setTitle("用户注册界面");
frame.pack();
//frame.setSize(200,300);
frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
}
JTextField userID=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JPasswordField repassword=new JPasswordField(10);
//添加按钮;

JButton regit=new JButton("确定");
JButton cancel=new JButton("取消");
JButton exit=new JButton("退出");

public RegitGUI()
{
Container c=getContentPane();

JPanel p1=new JPanel(); 
JPanel p2=new JPanel();

p1.add(new JLabel("用户名"));
//new JLabel("用户名").setBackground(Color.red);
p1.add(userID);
p1.add(new JLabel("用户密码"));
p1.add(password);
p1.add(new JLabel("重新确定密码"));
p1.add(repassword);
p1.setLayout(new GridLayout(3,1,2,15));
//使输入的密码用*代替;
password.setEchoChar('*');
    repassword.setEchoChar('*');
    //设置背景色跟前景色
    //userID.setBackground(Color.yellow);
    //password.setBackground(Color.blue);
    //repassword.setBackground(Color.green);
    //new JLabel("用户密码").setForeground(Color.yellow);
    //new JLabel("重新确定密码").setForeground(Color.yellow);
   
    regit.setBackground(Color.red);
    regit.setForeground(Color.CYAN);
    cancel.setBackground(Color.red);
    cancel.setForeground(Color.CYAN);
    exit.setBackground(Color.red);
    exit.setForeground(Color.CYAN);
    //版面p2添加按钮;
 
p2.add(regit);
p2.add(cancel);
p2.add(exit);
  //设置提示文本;
    regit.setToolTipText("按此键进行注册");
    cancel.setToolTipText("可以取消你添的东西");
    exit.setToolTipText("退出界面");
//c.setLayout(p2,BorderLayout.SOUTH);
c.setLayout(new GridLayout(2,1));

c.add(p1);
c.add(p2);
//注册;

regit.addActionListener(this);
cancel.addActionListener(this);
    exit.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{

        if(e.getSource()==regit)
      {
   try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );
                   Statement stmt=conn.createStatement();
                   //ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
       ResultSet rs=stmt.executeQuery("select * from Login where userID='"+userID.getText()+"' and password='"+new String(password.getPassword())+"'");
       if(rs.next()) 
        {
        JOptionPane.showMessageDialog(null,"此用户已经注册过了\n请重新输入用户名");
        userID.setText("");
   password.setText("");
   repassword.setText("");
        }
       
       else 
       {    
    
            if(!new String(repassword.getPassword()).equals(new String(password.getPassword())))
            { 
             JOptionPane.showMessageDialog(null,"重新确认的密码不正确\n请重新输入");
                repassword.setText("");
            }  
          else
          {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:roemin", "sa", "880405" );
            stmt=conn.createStatement();
            int rst=stmt.executeUpdate("insert into Login values('"+userID.getText()+"','"+new String(password.getPassword())+"')");   
            JOptionPane.showMessageDialog(null,"恭喜你注册成功!");
          }
       }
       conn.close();
           }catch(Exception ex){System.out.println(ex);}
      }
else if(e.getSource()==cancel)
      {
userID.setText("");
    password.setText("");
    repassword.setText("");
  }
else if(e.getSource()==exit)
       {
System.exit(0);

}
}

}

解决方案 »

  1.   

            if (e.getActionCommand().equals("注册"))
            {
                RegitGUI frame = new RegitGUI();
                frame.setTitle("用户注册界面");
                frame.pack();
                // frame.setSize(200,300);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
      

  2.   

    **.setTitle("用户注册界面");
      

  3.   

    if (e.getActionCommand().equals("注册"))
    {
        new RegitGUI();       
    }
      

  4.   

    if(jtextfield.getText() != null)..明白?