我有两个窗户。一个登录窗口(称为main;我还在测试)和一个MainWindow。我正在从MS Access数据库中读取登录数据(用户名,密码)。该数据库有四个表,Customer,Accounts,Mortgage,CreditCard。到目前为止,一切正常,但是当我们成功登录时,我正试图将用户的名字和姓氏传递给主窗口中的JLabel。我该怎么做呢?用户登录后是否需要customerindex?在我可以在帧之间传递之前,是否需要将其存储在数组中?如果是这样,我该怎么做?我还在学习,所以一个例子会很棒。
我假设我的数组看起来像这样?
Account accounts[] = new Account[2];
Customer people[] = new Customer[2];
CreditCard credit[] = new CreditCard[2];
Mortgage mortgage[] = new Mortgage[2];
int index = 0;
这是我的登录类
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
public class Login extends JFrame
{
Connection con;
Statement st;
ResultSet rs;
JFrame f = new JFrame("User Login");
JLabel l = new JLabel("Username");
JLabel l1 = new JLabel("Password");
JTextField t = new JTextField(10);
JTextField t1 = new JTextField(10);
JButton b = new JButton("Login");
public Login()
{
connect();
frame();
}
public void connect()
{
try
{
//Connect to the database
con = DriverManager.getConnection("jdbc:ucanaccess://G:/JSProject/prjTIMS_JS/Bank.mdb");
st = con.createStatement();
System.out.println("Connected database successfully ...");
}
catch(Exception ex)
{
}
}
public void frame()
{
f.setSize(600,100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JPanel p = new JPanel();
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
p.add(b);
f.add(p);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
String username = t.getText().trim();
String password = t1.getText().trim();
String sql = "select username, password from tblCustomer where username = '" +username+"'and password = '"+password+"'" ;
rs = st.executeQuery(sql);
int count = 0;
while(rs.next())
{
count ++;
}
if(count == 1)
{
setVisible(false);
new MainWindow();
}
else if(count > 1)
{
JOptionPane.showMessageDialog(null, "Duplicate User, Access Denied!");
}
else
{
JOptionPane.showMessageDialog(null, "User Not Found!");
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "Catch Error");
}
}
});
}
public static void main(String[] args) {
new Login();
}}
主窗口
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class MainWindow extends JFrame {
MainWindow() {
//Create a new frame container
setTitle("Jo's Dealership: Main Menu");
//Give the frame an initial size and center the window
setSize(390, 270);
setLocationRelativeTo(null);
//Terminate the program when the user closes the application
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("pc.png");
JLabel lblImage = new JLabel(image);
lblImage.setPreferredSize(new Dimension(48, 48));
JLabel lblOptionMsg = new JLabel(" choose an option below.");
lblOptionMsg.setFont(new Font("Helvetica", Font.PLAIN, 11));
lblOptionMsg.setForeground(new Color(37, 75, 124));
//Make two buttons
JButton okButton = new JButton ("OK");
okButton.setFont(new Font("Helvetica", Font.BOLD, 12));
okButton.setOpaque(false);
okButton.setPreferredSize(new Dimension(85, 22));
okButton.setForeground(new Color(37, 75, 124));
JButton cancelButton = new JButton ("Logout");
cancelButton.setFont(new Font("Helvetica", Font.BOLD, 12));
cancelButton.setOpaque(false);
cancelButton.setPreferredSize(new Dimension(85, 22));
cancelButton.setForeground(new Color(37, 75, 124));
JRadioButton buyButton = new JRadioButton("Buy Car", true);
buyButton.setMnemonic(KeyEvent.VK_B);
buyButton.setActionCommand("Buy");
buyButton.setFont(new Font("Helvetica", Font.BOLD, 11));
buyButton.setOpaque(true);
buyButton.setPreferredSize(new Dimension(120, 22));
buyButton.setForeground(new Color(37, 75, 124));
JRadioButton calcButton = new JRadioButton("Calculate Commission", true);
calcButton.setMnemonic(KeyEvent.VK_C);
calcButton.setActionCommand("Calculate");
calcButton.setFont(new Font("Helvetica", Font.BOLD, 11));
calcButton.setOpaque(true);
calcButton.setPreferredSize(new Dimension(200, 22));
calcButton.setForeground(new Color(37, 75, 124));
ButtonGroup bG = new ButtonGroup();
bG.add(buyButton);
bG.add(calcButton);
buyButton.setSize(100,200);
calcButton.setSize(100,200);
//Create text based label
JLabel lblMessage = new JLabel("[CustomerNameHere] is currently logged on.");
lblMessage.setFont(new Font("Helvetica", Font.PLAIN, 11));
lblMessage.setForeground(new Color(37, 75, 124));
//Add the panel to the content page
JPanel panel = new JPanel();
add(panel);
panel.setBackground(new Color(239, 239, 239));
//Display the frame
setVisible(true);
//Set the layout
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
//Add the label to the content pane
panel.add(lblImage);
panel.add(lblMessage);
panel.add(lblOptionMsg);
panel.add(okButton);
panel.add(cancelButton);
panel.add(buyButton);
panel.add(calcButton);
//Adjust constraints for the components
layout.putConstraint(SpringLayout.WEST, lblImage, 150, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, lblImage, 50, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, lblMessage, 75, SpringLayout.WEST, panel); //right
layout.putConstraint(SpringLayout.NORTH, lblMessage, 10, SpringLayout.NORTH, panel); //top
layout.putConstraint(SpringLayout.WEST, okButton, 90, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, okButton, 185, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, cancelButton, 195, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, cancelButton, 185, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, buyButton, 95, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, buyButton, 105, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, calcButton, 95, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, calcButton, 135, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, lblOptionMsg, 105, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, lblOptionMsg, 25, SpringLayout.NORTH, panel);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//new MainWindow(0).setVisible(true);
}
});
}
}
答案 0 :(得分:0)
如果您想将登录用户的名字和姓氏传递给MainWindow,这是一种简单的方法。
首先,在验证数据库中是否存在用户时,修改您的查询以获取Last和First名称:
String sql = "select FName, LName from tblCustomer where username = '" +username+"'and password = '"+password+"'" ;
rs = st.executeQuery(sql);
int count = 0;
String lastName ='';
String firstName = '';
while(rs.next())
{
count ++;
//get last name and first name when connected
lastName = rs.getString("LName");
firstName = rs.getString("FName");
}
然后:
if(count == 1)
{
setVisible(false);
new MainWindow( firstName+" "+lastName);//<--pass it here to the mainWindow
}
最后,在您的MainWindow中:
public class MainWindow extends JFrame {
private String whosLoggedOn;
MainWindow(String whosLoggedOn) {
this.whosLoggedOn = whosLoggedOn;
//------ other stuff--------
//Create text based label
JLabel lblMessage = new JLabel(this.whosLoggedOnis+ " is currently logged on.");
//------ other stuff--------
}
}
编辑1:
如果要传递其他表中的其他信息,例如tblCustomer
和tblAccount
,可以像这样修改MainWindow的构造函数:
public class MainWindow extends JFrame {
MainWindow(ResultSet tblCustomer, ResultSet tblAccount ) {
//use them as you want here
//for example to get last and first name :
String lastName = tblCustomer.getString("LName");
String firstName = tblCustomer.getString("FName");
//Create text based label
JLabel lblMessage = new JLabel(lastName +" "+ firstName" is currently logged on.");
//------ other stuff--------
}
}
现在,在您的登录信息JFrame
中,您只需查询所需的表格,并在连接后将ResultSet
传递给mainWindow
。