【发布时间】:2017-09-20 16:05:24
【问题描述】:
我有 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
}
【问题讨论】:
-
你需要使用一个对话框,当它出现时将停止代码执行直到它关闭,此时你可以向对象询问密码。或者,您可以使用某种观察者模式,但对话框的好处是它可以阻止用户导航到 UI 的其他部分,直到它被处理
-
我首先建议不要制作不同的框架,而是使用卡片布局。在 jframe 上设置 cardlayout 并在其上添加 2 个面板。在第一个面板上添加您的支付屏幕,在第二个面板上添加“匹配”按钮屏幕。然后从一个屏幕切换到另一个屏幕。这将减少您的代码和复杂性。
-
在您重新提出问题之前,您也得到了相同的建议,即在示例中使用模态 JDialog,这些建议您当时忽略了。请相信并向他学习,因为他在这方面知识渊博。
标签: java swing java-threads