我有2个Swing开发的应用程序。 1)付费屏幕,其中,用户ID和“付费”按钮将在那里。 2)另一个屏幕将有“匹配”按钮,用于检查用户的有效性并返回相应用户的密码。
我的主屏幕是“付费”屏幕。从这里我将输入用户ID并单击“付款”按钮,它必须导航到“另一个屏幕”。到目前为止,我的代码正在运行。
在“匹配”按钮中进行验证后,我应该将密码返回到“付款”屏幕。这部分代码不起作用。请帮忙
//Pay screen class
package trig_pack;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Image ;
import java.io.*;
import java.sql.*;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import capture_match_finger_image.*;
public class trigPayClass extends javax.swing.JFrame {
/**
* Creates new form trigPayClass
*/
public trigPayClass() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
t_userid = new javax.swing.JTextField();
btn_pay = new javax.swing.JButton();
lbl_result = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel2.setText("User Id:");
t_userid.setName("t_userid"); // NOI18N
t_userid.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t_useridActionPerformed(evt);
}
});
btn_pay.setText("PAY");
btn_pay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_payActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn_pay)
.addComponent(t_userid, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbl_result, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(85, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(t_userid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(btn_pay)
.addGap(26, 26, 26)
.addComponent(lbl_result, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(224, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btn_payActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("in btn_payActionPerformed");
int v_userid=Integer.parseInt(t_userid.getText());
System.out.println("v_userid-"+v_userid);
Connection con=null;
try
{
con=DriverManager.getConnection(
"<connection string>");
System.out.println("connection established..");
fetchPasswordClass c=new fetchPasswordClass(); //another class
String pwd=c.getpwd(v_userid,con); //call method in another class to get password fro incoming userid and connection objects
lbl_result.setText(pwd); //populate returned password in a label or text field.
} catch (Exception e)
{
e.printStackTrace();
}
}
private void t_useridActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (javax.swing.UIManager.getSystemLookAndFeelClassName().equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(trigPayClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(trigPayClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(trigPayClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(trigPayClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new trigPayClass().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_pay;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel lbl_result;
private javax.swing.JTextField t_userid;
// End of variables declaration
}
package match_pwd;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import java.sql.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
public class fetchPasswordClass extends javax.swing.JFrame {
int v_userid=0;
int v_nfiq=5;
String match_result=null;
Connection i_con=null;
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btn_con = new javax.swing.JButton();
pnl_srno = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
lbl_srno = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
lbl_nfiq = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
lbl_fing = new javax.swing.JLabel();
lbl_stat = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
btn_prv = new javax.swing.JButton();
btn_match = new javax.swing.JButton();
btn_disconn = new javax.swing.JButton();
lbl_userid = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(130, 140, 150));
setMinimumSize(new java.awt.Dimension(613, 389));
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
btn_match.setText("MATCH");
btn_match.setEnabled(false);
btn_match.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_matchActionPerformed(evt);
}
});
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void formWindowClosing(java.awt.event.WindowEvent evt) {
//
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
private String btn_matchActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("In SaveActionPerformed");
btn_match.setEnabled(false);
if (lbl_userid.getText()!=null && lbl_userid.getText().length()>0)
{
System.out.println("Got Userid-"+v_userid);
matchUserId m=new matchUserId();
String match_success_yn=m.matchUser(v_userid);
if (match_success_yn.equals("0"))
{
match_result="Not valid user!";
}
else
{
match_result=match_success_yn;
}
}
else
{
System.out.println("Userid null");
lbl_stat.setText("Please enter valid user id");
btn_match.setEnabled(false);
match_result="Please pass valid user id";
}
return match_result;
}
public String getpwd(int userid,Connection con){
v_userid=userid;
System.out.println("v_userid- "+v_userid);
i_con=con;
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (javax.swing.UIManager.getSystemLookAndFeelClassName().equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(fetchPasswordClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(fetchPasswordClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(fetchPasswordClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(fetchPasswordClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// new fetchPasswordClass().setVisible(true);
initComponents();
c.getContentPane().setBackground(new Color(176,176,176));//153, 255, 255
lbl_userid.setText(String.valueOf(v_userid));
System.out.println(" lbl_userid.getText()- "+ lbl_userid.getText());
c.setVisible(true);
}
});
System.out.println("before returning from matchclass");
// return match_result;
}
// Variables declaration - do not modify
private javax.swing.JButton btn_con;
private javax.swing.JButton btn_disconn;
private javax.swing.JButton btn_match;
private javax.swing.JButton btn_prv;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel lbl_fing;
private javax.swing.JLabel lbl_nfiq;
private javax.swing.JLabel lbl_srno;
private javax.swing.JLabel lbl_stat;
private javax.swing.JLabel lbl_userid;
private javax.swing.JPanel pnl_srno;
// End of variables declaration
}
答案 0 :(得分:0)
You need to pass data using constructors
First create a constructor inside the fetchPasswordClass
String fetchedPassword = null;
public fetchPasswordClass(String password){
//then pass the value to fetchedPassword variable
fetchedPassword = password;
//then initialize the components
initComponents();
}
--------------------------------
Then inside the trigPayClass inside the button action
fetchPasswordClass obj = new fetchPasswordClass(password);
obj.setVisible(true);
答案 1 :(得分:0)
让我们说这是你的主窗口
public class MainWindow extends JFrame {
private AnotherWindow another_window;
public MainWindow() {
init();
another_window = new AnotherWindow(this);
}
private void init() {
//some codes for JFrame
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
new MainWindow().setVisible(true);
});
}
public void your_event(/*some parameters*/) {
another_window.setVisible(true);
another_window.validateInfo(/*some values for the parameter*/);
}
}
这是AnotherWindow
public class AnotherWindow extends JFrame {
private MainWindow main_window;
public AnotherWindow(MainWindow main_window) {
this.main_window = main_window;
}
public boolean validateInfo(/**some parameters like string password, string username**/) {
return result;
}
}
这背后的想法是让&#34;对象&#34;知道是谁打电话给他们
在此示例中,MainWindow
创建了AnotherWindow
的实例。在AnotherWindow
类中,我们为将要调用它的对象创建了一个参数。我们在MainWindow
课程中声明了一个AnotherWindow
对象,但没有为其分配值,因为我们还不知道MainWindow
的哪个具体实例会调用AnotherWindow
。