Can anyone help me to correct this Java?
任何人都可以帮我纠正这个Java吗?
I want to pass the data from login obj JFrame
to my mainform
class but I do not know how to do it!
我想将登录obj JFrame中的数据传递给我的mainform类,但我不知道该怎么做!
Here is my code:
这是我的代码:
Login.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
class Login extends JFrame implements ActionListener{
private JTextField tfUser;
private JPasswordField tfPass;
private JButton btnLogin,btnCancel;
private JComboBox cbotype;
private String user,pass;
private Connection con;
private Statement stmt;
private ResultSet rs;
public Login(){
super("Login");
initGUI();
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnLogin) pLogin();
else if(e.getSource()==btnCancel) cancel();
}
public void cancel(){ dispose();
}
public void pLogin(){
if(isLogin())
setVisible(false);
System.out.print(user);
}
public boolean isLogin(){
String sms="Welcome!!!",table=cbotype.getSelectedItem().toString();
pass=tfPass.getText();
user=tfUser.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=dbLibrary;user=sa;password=123;";
Connection con = DriverManager.getConnection(connectionUrl);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("SELECT username,password FROM " + table +" Where username='"+ user +"'");
if(rs.next ()){
rs.first();
if(rs.getString(2).compareTo(pass)==0)
{ JOptionPane.showMessageDialog(null,sms);
return true;
}else{
sms="Incorrect Password! Please, try it again!";
}
}else
{sms="Incorrect User or Password! Please, try again!";}
JOptionPane.showMessageDialog(null,sms);
}catch(SQLException e){ e.printStackTrace();
}catch(ClassNotFoundException e1){e1.printStackTrace();
}return false;
}
public void initGUI(){
JPanel p1=new JPanel(new FlowLayout());
p1.add(Interface.title(new JLabel("Login"),"images/login_0.png"));
//con.add(p1);
JPanel p2=new JPanel(new GridLayout(3,2));
p2.add(Interface.label(new JLabel("Type:")));
p2.add(cbotype=new JComboBox());
cbotype.addItem("Employee");cbotype.addItem("Patron");
p2.add(Interface.label(new JLabel("User:")));
p2.add(tfUser=new JTextField(20));
p2.add(Interface.label(new JLabel("Password:")));
p2.add(tfPass=new JPasswordField(20));
//con.add(p2);
JPanel p3=new JPanel(new FlowLayout());
p3.add(Interface.button(btnLogin=new JButton("Login"),"images/login_1.png"));
p3.add(Interface.button(btnCancel=new JButton("Cancel"),"images/login_2.png"));
JPanel p=new JPanel(new BorderLayout());
p.add(p1,"North");
p.add(p2,"Center");
p.add(p3,"South");
add(p);
pack();
btnLogin.addActionListener(this);
btnCancel.addActionListener(this);
}
public String getUser(){
return user;
}
public String getPass(){
return pass;
}
public static void main(String[] args) {
new Login();
}
}
MainForm.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MainForm extends JFrame implements ActionListener{
private Container con;
private String user;
private JButton btnshow;
public MainForm() {
initGUI();
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void initGUI(){
Login login=new Login();
user=login.getUser();
//login.dispose();
System.out.print(user);
con=getContentPane();
con.add(btnshow=new JButton("Show"));
btnshow.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnshow) {System.out.print(user);
}
}
public static void main(String[] args) {
new MainForm();
}
}
2 个解决方案
#1
1
One way: When you call the Login constructor, is to parse the MainForm instance to it. Your Login class will keep a reference to the MainForm and when you need to let the MainForm know call a method on the MainForm from within the Login class. This is pretty much the same way listeners work...
一种方法:当你调用Login构造函数时,就是将MainForm实例解析为它。您的Login类将保留对MainForm的引用,并且当您需要让MainForm知道在Login类中调用MainForm上的方法时。这与听众的工作方式非常相似......
In your MainForm class
在您的MainForm类中
Login login=new Login(this);
In your Login class when you want to let the MainForm know:
在您想要让MainForm知道的Login类中:
this.mainForm.someListenerMethod();
#2
1
See The Use of Multiple JFrames, Good/Bad Practice? The log-in should be a modal JDialog
or a JOptionPane
. Pop the dialog from the frame, then examine the relevant fields in the next line of code.
请参阅使用多个JFrame,好/坏练习?登录应该是模态JDialog或JOptionPane。从框架弹出对话框,然后检查下一行代码中的相关字段。
Try looking over this code for tips.
尝试查看此代码以获取提示。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class MainForm extends JFrame implements ActionListener{
private Container con;
private String user;
private JButton btnshow;
public MainForm() {
initGUI();
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void initGUI(){
Login login=new Login(this);
user=login.getUser();
//login.dispose();
System.out.print(user);
con=getContentPane();
con.add(btnshow=new JButton("Show"));
btnshow.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnshow) {System.out.print(user);
}
}
public static void main(String[] args) {
new MainForm();
}
}
class Login extends JDialog implements ActionListener{
private JTextField tfUser;
private JPasswordField tfPass;
private JButton btnLogin,btnCancel;
private JComboBox cbotype;
private String user,pass;
private Connection con;
private Statement stmt;
private ResultSet rs;
public Login(Frame f){
// make it modal
super(f, true);
initGUI();
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnLogin) pLogin();
else if(e.getSource()==btnCancel) cancel();
}
public void cancel(){ dispose();
}
public void pLogin(){
//if(isLogin())
setVisible(false);
System.out.print(user);
}
public boolean isLogin(){
String sms="Welcome!!!",table=cbotype.getSelectedItem().toString();
// never store passwords as a String, left as an exercise for the user
pass = new String(tfPass.getPassword());
user= tfUser.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=dbLibrary;user=sa;password=123;";
Connection con = DriverManager.getConnection(connectionUrl);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("SELECT username,password FROM " + table +" Where username='"+ user +"'");
if(rs.next ()){
rs.first();
if(rs.getString(2).compareTo(pass)==0)
{ JOptionPane.showMessageDialog(null,sms);
return true;
}else{
sms="Incorrect Password! Please, try it again!";
}
}else
{sms="Incorrect User or Password! Please, try again!";}
JOptionPane.showMessageDialog(null,sms);
}catch(SQLException e){ e.printStackTrace();
}catch(ClassNotFoundException e1){e1.printStackTrace();
}return false;
}
public void initGUI(){
JPanel p1=new JPanel(new FlowLayout());
p1.add(new JLabel("Login"));
//con.add(p1);
JPanel p2=new JPanel(new GridLayout(3,2));
p2.add(new JLabel("Type:"));
p2.add(cbotype=new JComboBox());
cbotype.addItem("Employee");cbotype.addItem("Patron");
p2.add(new JLabel("User:"));
p2.add(tfUser=new JTextField(20));
p2.add(new JLabel("Password:"));
p2.add(tfPass=new JPasswordField(20));
//con.add(p2);
JPanel p3=new JPanel(new FlowLayout());
p3.add(btnLogin = new JButton("Login"));
p3.add(btnCancel = new JButton("Cancel"));
JPanel p=new JPanel(new BorderLayout());
// Don't use magic numbers! There are defined constants
// for these constraints.
p.add(p1,BorderLayout.NORTH);
p.add(p2,BorderLayout.CENTER);
p.add(p3,BorderLayout.SOUTH);
add(p);
pack();
btnLogin.addActionListener(this);
btnCancel.addActionListener(this);
}
public String getUser(){
return user;
}
public String getPass(){
return pass;
}
}
Other tips
- Don't extend frame or dialog, just use instances of them.
- Swing & AWT GUIs should be created and updated on the Event Dispatch Thread.
- Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow.
- See further tips in code comments.
不要扩展框架或对话框,只使用它们的实例。
应在Event Dispatch Thread上创建和更新Swing和AWT GUI。
对代码块使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程。
请参阅代码注释中的更多提示。
#1
1
One way: When you call the Login constructor, is to parse the MainForm instance to it. Your Login class will keep a reference to the MainForm and when you need to let the MainForm know call a method on the MainForm from within the Login class. This is pretty much the same way listeners work...
一种方法:当你调用Login构造函数时,就是将MainForm实例解析为它。您的Login类将保留对MainForm的引用,并且当您需要让MainForm知道在Login类中调用MainForm上的方法时。这与听众的工作方式非常相似......
In your MainForm class
在您的MainForm类中
Login login=new Login(this);
In your Login class when you want to let the MainForm know:
在您想要让MainForm知道的Login类中:
this.mainForm.someListenerMethod();
#2
1
See The Use of Multiple JFrames, Good/Bad Practice? The log-in should be a modal JDialog
or a JOptionPane
. Pop the dialog from the frame, then examine the relevant fields in the next line of code.
请参阅使用多个JFrame,好/坏练习?登录应该是模态JDialog或JOptionPane。从框架弹出对话框,然后检查下一行代码中的相关字段。
Try looking over this code for tips.
尝试查看此代码以获取提示。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class MainForm extends JFrame implements ActionListener{
private Container con;
private String user;
private JButton btnshow;
public MainForm() {
initGUI();
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void initGUI(){
Login login=new Login(this);
user=login.getUser();
//login.dispose();
System.out.print(user);
con=getContentPane();
con.add(btnshow=new JButton("Show"));
btnshow.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnshow) {System.out.print(user);
}
}
public static void main(String[] args) {
new MainForm();
}
}
class Login extends JDialog implements ActionListener{
private JTextField tfUser;
private JPasswordField tfPass;
private JButton btnLogin,btnCancel;
private JComboBox cbotype;
private String user,pass;
private Connection con;
private Statement stmt;
private ResultSet rs;
public Login(Frame f){
// make it modal
super(f, true);
initGUI();
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnLogin) pLogin();
else if(e.getSource()==btnCancel) cancel();
}
public void cancel(){ dispose();
}
public void pLogin(){
//if(isLogin())
setVisible(false);
System.out.print(user);
}
public boolean isLogin(){
String sms="Welcome!!!",table=cbotype.getSelectedItem().toString();
// never store passwords as a String, left as an exercise for the user
pass = new String(tfPass.getPassword());
user= tfUser.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=dbLibrary;user=sa;password=123;";
Connection con = DriverManager.getConnection(connectionUrl);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("SELECT username,password FROM " + table +" Where username='"+ user +"'");
if(rs.next ()){
rs.first();
if(rs.getString(2).compareTo(pass)==0)
{ JOptionPane.showMessageDialog(null,sms);
return true;
}else{
sms="Incorrect Password! Please, try it again!";
}
}else
{sms="Incorrect User or Password! Please, try again!";}
JOptionPane.showMessageDialog(null,sms);
}catch(SQLException e){ e.printStackTrace();
}catch(ClassNotFoundException e1){e1.printStackTrace();
}return false;
}
public void initGUI(){
JPanel p1=new JPanel(new FlowLayout());
p1.add(new JLabel("Login"));
//con.add(p1);
JPanel p2=new JPanel(new GridLayout(3,2));
p2.add(new JLabel("Type:"));
p2.add(cbotype=new JComboBox());
cbotype.addItem("Employee");cbotype.addItem("Patron");
p2.add(new JLabel("User:"));
p2.add(tfUser=new JTextField(20));
p2.add(new JLabel("Password:"));
p2.add(tfPass=new JPasswordField(20));
//con.add(p2);
JPanel p3=new JPanel(new FlowLayout());
p3.add(btnLogin = new JButton("Login"));
p3.add(btnCancel = new JButton("Cancel"));
JPanel p=new JPanel(new BorderLayout());
// Don't use magic numbers! There are defined constants
// for these constraints.
p.add(p1,BorderLayout.NORTH);
p.add(p2,BorderLayout.CENTER);
p.add(p3,BorderLayout.SOUTH);
add(p);
pack();
btnLogin.addActionListener(this);
btnCancel.addActionListener(this);
}
public String getUser(){
return user;
}
public String getPass(){
return pass;
}
}
Other tips
- Don't extend frame or dialog, just use instances of them.
- Swing & AWT GUIs should be created and updated on the Event Dispatch Thread.
- Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow.
- See further tips in code comments.
不要扩展框架或对话框,只使用它们的实例。
应在Event Dispatch Thread上创建和更新Swing和AWT GUI。
对代码块使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程。
请参阅代码注释中的更多提示。