我的程序是用Java编写的,我的程序所做的是首先你必须将你的用户名和密码登录到服务器,一旦验证,然后你去一个新页面(框架),我有一个JMenuBar ,JMenu和JMenuItem。我正在处理的JMenuItem在JMenu Exit下面被称为“Logout”,当你点击退出时,它会关闭屏幕。我想要发生的是将我重定向回登录页面。我尝试了很多功能,但都没有。我很感激你的帮助。
这是我的计划:
public class ScanTest2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static LoginDialog loginDialog;
public ScanTest2() {
loginDialog = new LoginDialog(this, true);
loginDialog.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// create new frame for new page
JFrame frame = new ScanTest2();
//create a Menu bar
JMenuBar menuBar = new JMenuBar();
frame.getContentPane().setBackground(Color.BLUE);
try {
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/home/a002384/logo2.bmp")))));
} catch (IOException e) {
e.printStackTrace();
}
frame.setTitle("Scan Gun Information");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// create Menus
JMenu databaseMenu = new JMenu("Database");
JMenu ticketMenu = new JMenu("Tickets");
JMenu inquiriesMenu = new JMenu("Inquiries");
JMenu reportsMenu = new JMenu("Reports");
JMenu exitMenu = new JMenu("Exit");
// create Menu Items
JMenuItem lotMenuItem = new JMenuItem("Lots");
lotMenuItem.setMnemonic(KeyEvent.VK_L);
lotMenuItem.setActionCommand("Lots");
JMenuItem gunMenuItem = new JMenuItem("Guns");
gunMenuItem.setMnemonic(KeyEvent.VK_G);
gunMenuItem.setActionCommand("Guns");
JMenuItem batteryMenuItem = new JMenuItem("Batteries");
batteryMenuItem.setMnemonic(KeyEvent.VK_B);
batteryMenuItem.setActionCommand("Batteries");
JMenuItem logoutMenuItem = new JMenuItem("Logout");
logoutMenuItem.setMnemonic(KeyEvent.VK_L);
logoutMenuItem.setActionCommand("Logout");
MenuItemListener menuItemListener = new MenuItemListener();
lotMenuItem.addActionListener(menuItemListener);
gunMenuItem.addActionListener(menuItemListener);
batteryMenuItem.addActionListener(menuItemListener);
logoutMenuItem.addActionListener(menuItemListener);
// add Menu Items to Menus
databaseMenu.add(lotMenuItem);
databaseMenu.add(gunMenuItem);
databaseMenu.add(batteryMenuItem);
exitMenu.add(logoutMenuItem);
// add menu to MenuBar
menuBar.add(databaseMenu);
menuBar.add(ticketMenu);
menuBar.add(inquiriesMenu);
menuBar.add(reportsMenu);
menuBar.add(exitMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
} // end of run method
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Logout"))
System.exit(0);
// go back to login screen
}
}
}); // end of Runnable
}// end of Main
}
class LoginDialog extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
private final JLabel loginlbl = new JLabel("Username");
private final JLabel Passwordlbl = new JLabel("Password");
private final JTextField loginName = new JTextField(15);
private final JPasswordField loginPassword = new JPasswordField();
private final JButton loginButton = new JButton("Login");
private final JButton cancelButton = new JButton("Cancel");
private final JLabel Status = new JLabel(" ");
private String username;
private String password;
private int m_errCounter = 0;
public static final int MAX_LOGIN_ATTEMPTS = 3;
public LoginDialog() {
this(null, true);
}
public LoginDialog(final JFrame parent, boolean modal) {
super(parent, modal);
JPanel p3 = new JPanel(new GridLayout(2, 1));
p3.add(loginlbl);
p3.add(Passwordlbl);
JPanel p4 = new JPanel(new GridLayout(2, 1));
p4.add(loginName);
p4.add(loginPassword);
JPanel p1 = new JPanel();
p1.add(p3);
p1.add(p4);
JPanel p2 = new JPanel();
p2.add(loginButton);
p2.add(cancelButton);
JPanel p5 = new JPanel(new BorderLayout());
p5.add(p2, BorderLayout.CENTER);
p5.add(Status, BorderLayout.NORTH);
Status.setForeground(Color.RED);
Status.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p5, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
loginPassword.enableInputMethods(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UniJava uJava = new UniJava();
try {
username = loginName.getText();
password = new String (loginPassword.getPassword());
if (username == " " && password == " ")
{
Status.setText("Invalid username or password!");
}
else
{
UniSession session = uJava.openSession();
session.setHostName("docdbtst.starcalif.com");
session.setUserName(username);
session.setPassword(password);
session.setAccountPath("/mnt/data1/DD");
session.connect();
parent.setVisible(true);
setVisible(false);
}
} catch (UniSessionException e1) {
if (++m_errCounter > MAX_LOGIN_ATTEMPTS)
{
JOptionPane.showMessageDialog(LoginDialog.this,
"All Login attempts failed!",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
else
{
Status.setText("Invalid Login! Cannot Connect!");
loginName.setText("");
loginPassword.setText("");
}
e1.printStackTrace();
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.dispose();
System.exit(0);
}
});
}
}
答案 0 :(得分:3)
System.exit()
一切都将被清理干净
JFrame.dispose() vs System.exit()
logoutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
f.dispose();
// northInformation.removeAll();
// init();
LoginWindow login = new LoginWindow();
}
});