用户登录后,我试图在下一张卡上显示用户名,但没有运气。
我正在使用 CardLayout 并定义了两张卡 - 一张用于用户输入姓名和密码的卡,第二张用于显示带有登录名的欢迎消息。我正在学习自己的 Java 和 Swing,而不是专家。任何帮助,包括修复此代码或供我阅读的参考资料,将不胜感激。
这是我当前的代码(仍然需要添加代码来更新欢迎屏幕的文本字段):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardTest
{
private JFrame frame;
public static final String CARD_LOGIN = "Card Login";
public static final String CARD_DEPARTMENT = "Card Department";
public static final String CARD_TEAM = "Card Team";
public static JPanel cards;
public Employee employee = null;
public CardLogin cardLogin = null;
public CardDepartment cardDepartment = null;
public CardTeam cardTeam = null;
public CardTest()
{
Employee employee = new Employee();
frame = new JFrame("Card Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
cards = new JPanel();
cards.setLayout(new CardLayout(20, 20));
cardLogin = new CardLogin(this, employee);
cardDepartment = new CardDepartment(this, employee);
cardTeam = new CardTeam(this, employee);
cards.add(cardLogin, CARD_LOGIN);
cards.add(cardDepartment, CARD_DEPARTMENT);
cards.add(cardTeam, CARD_TEAM);
frame.getContentPane().add(cards);
frame.pack();
frame.setVisible(true);
}
public void swapView(String key)
{
CardLayout cardLayout = (CardLayout) cards.getLayout();
cardLayout.show(cards, key);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardTest();
}
});
}
}
class CardLogin extends JPanel
{
private ActionListener action;
private JTextField tfUsername= null;
Employee employee;
CardTest cardTest;
public CardLogin(CardTest cardTest, Employee employee)
{
this.cardTest = cardTest;
this.employee = employee;
init();
}
private void init()
{
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
JLabel lbCardName = new JLabel("Login Card ");
gc.gridx = 1;
gc.gridy = 0;
gc.gridwidth = 2;
panel.add(lbCardName, gc);
JLabel lbUsername = new JLabel("Username: ");
gc.gridx = 0;
gc.gridy = 2;
gc.gridwidth = 1;
panel.add(lbUsername, gc);
tfUsername = new JTextField(20);
gc.gridx = 1;
gc.gridy = 2;
gc.gridwidth = 2;
panel.add(tfUsername, gc);
JLabel lbPassword = new JLabel("Password: ");
gc.gridx = 0;
gc.gridy = 3;
gc.gridwidth = 1;
panel.add(lbPassword, gc);
JPasswordField pfPassword = new JPasswordField(20);
gc.gridx = 1;
gc.gridy = 3;
gc.gridwidth = 2;
panel.add(pfPassword, gc);
final JButton loginButton = new JButton("Login");
action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (tfUsername.getDocument().getLength() > 0)
{
employee.setUserName(tfUsername.getText());
cardTest.swapView(cardTest.CARD_DEPARTMENT);
}
}
};
loginButton.addActionListener(action);
JPanel bp = new JPanel();
bp.add(loginButton);
setSize( 640, 480);
add(panel, BorderLayout.CENTER);
add(bp, BorderLayout.PAGE_END);
}
}
class CardDepartment extends JPanel
{
private ActionListener actionNext;
private ActionListener actionLogout;
private JTextField tfDepartment= null;
private String department= null;
Employee employee;
CardTest cardTest;
CardLogin cardLogin;
public CardDepartment(CardTest cardTest, Employee employee)
{
this.employee = employee;
this.cardTest = cardTest;
init();
}
private void init()
{
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
JLabel lbCardName = new JLabel("Department Card ");
gc.gridx = 1;
gc.gridy = 0;
gc.gridwidth = 2;
panel.add(lbCardName, gc);
JLabel lbWelcome = new JLabel("Welcome ");
gc.gridx = 0;
gc.gridy = 2;
gc.gridwidth = 1;
panel.add(lbWelcome, gc);
gc.gridx = 1;
gc.gridy = 2;
gc.gridwidth = 2;
panel.add(new JLabel(employee.getUserName()), gc);
JLabel lbDepartment = new JLabel("Enter Department: ");
gc.gridx = 0;
gc.gridy = 3;
gc.gridwidth = 1;
panel.add(lbDepartment, gc);
tfDepartment = new JTextField(20);
gc.gridx = 1;
gc.gridy = 3;
gc.gridwidth = 2;
panel.add(tfDepartment, gc);
final JButton nextButton = new JButton("Next");
actionNext = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (tfDepartment.getDocument().getLength() > 0)
{
department = tfDepartment.getText();
cardTest.swapView(cardTest.CARD_TEAM);
}
}
};
final JButton logoutButton = new JButton("Logout");
actionLogout = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
cardTest.swapView(cardTest.CARD_LOGIN);
}
};
nextButton.addActionListener(actionNext);
logoutButton.addActionListener(actionLogout);
JPanel bp = new JPanel();
bp.add(panel);
bp.add(logoutButton);
bp.add(nextButton);
add(panel);
add(bp);
}
}
class CardTeam extends JPanel
{
private ActionListener actionPrev;
private ActionListener actionLogout;
private JTextField tfTeam= null;
Employee employee;
CardTest cardTest;
public CardTeam(CardTest cardTest, Employee employee)
{
this.cardTest = cardTest;
this.employee = employee;
init();
}
private void init()
{
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
JLabel lbCardName = new JLabel("Team Card ");
gc.gridx = 1;
gc.gridy = 0;
gc.gridwidth = 2;
panel.add(lbCardName, gc);
JLabel lbWelcome = new JLabel("Welcome ");
gc.gridx = 0;
gc.gridy = 2;
gc.gridwidth = 1;
panel.add(lbWelcome, gc);
gc.gridx = 1;
gc.gridy = 2;
gc.gridwidth = 2;
panel.add(new JLabel(employee.getUserName()), gc);
JLabel lbDepartment = new JLabel("Department: ");
gc.gridx = 0;
gc.gridy = 3;
gc.gridwidth = 1;
panel.add(lbDepartment, gc);
gc.gridx = 1;
gc.gridy = 3;
gc.gridwidth = 2;
panel.add(new JLabel(employee.getDepartment()), gc);
JLabel lbTeam = new JLabel("Enter Team: ");
gc.gridx = 0;
gc.gridy = 4;
gc.gridwidth = 1;
panel.add(lbTeam, gc);
tfTeam = new JTextField(20);
gc.gridx = 1;
gc.gridy = 4;
gc.gridwidth = 2;
panel.add(tfTeam, gc);
final JButton prevButton = new JButton("Prev");
actionPrev = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
cardTest.swapView(cardTest.CARD_DEPARTMENT);
}
};
final JButton logoutButton = new JButton("Logout");
actionLogout = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
cardTest.swapView(cardTest.CARD_LOGIN);
}
};
prevButton.addActionListener(actionPrev);
logoutButton.addActionListener(actionLogout);
JPanel bp = new JPanel();
bp.add(logoutButton);
bp.add(prevButton);
add(panel);
add(bp);
}
}
class Employee
{
private String userName = null;
private String department = null;
private String team = null;
public Employee() {
super();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
}