aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes output problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "output problem" Watch "output problem" New topic
Author

output problem

ten doeschate
Greenhorn

Joined: Jun 22, 2013
Posts: 16
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class Login {

public static boolean authenticate(String username, String password) {
// hardcoded username and password
if (username.equals("tendoeschate") && password.equals("yomahesh")) {
return true;
}
return false;
}
}
class LoginDialog extends JDialog {

private JTextField tfUsername;
private JPasswordField pfPassword;
private JLabel lbUsername;
private JLabel lbPassword;
private JButton btnLogin;
private JButton btnCancel;
private boolean succeeded;

public LoginDialog(Frame parent) {
super(parent, "Login", true);
//
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();

cs.fill = GridBagConstraints.HORIZONTAL;

lbUsername = new JLabel("Username: ");
cs.gridx = 0;
cs.gridy = 0;
cs.gridwidth = 1;
panel.add(lbUsername, cs);

tfUsername = new JTextField(20);
cs.gridx = 1;
cs.gridy = 0;
cs.gridwidth = 2;
panel.add(tfUsername, cs);

lbPassword = new JLabel("Password: ");
cs.gridx = 0;
cs.gridy = 1;
cs.gridwidth = 1;
panel.add(lbPassword, cs);

pfPassword = new JPasswordField(20);
cs.gridx = 1;
cs.gridy = 1;
cs.gridwidth = 2;
panel.add(pfPassword, cs);
panel.setBorder(new LineBorder(Color.GRAY));

btnLogin = new JButton("Login");

btnLogin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (Login.authenticate(getUsername(), getPassword())) {

game p=new game();

p.setVisible(true);

} else {
JOptionPane.showMessageDialog(LoginDialog.this,
"Invalid username or password",
"Login",
JOptionPane.ERROR_MESSAGE);
// reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded = false;

}
}
});
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dispose();
}
});
JPanel bp = new JPanel();
bp.add(btnLogin);
bp.add(btnCancel);

getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(bp, BorderLayout.PAGE_END);

pack();
setResizable(false);
setLocationRelativeTo(parent);
}

public String getUsername() {
return tfUsername.getText().trim();
}

public String getPassword() {
return new String(pfPassword.getPassword());
}

public boolean isSucceeded() {
return succeeded;
}
}
public class re {
public static void main(String[] args) {
final JFrame frame = new JFrame("TicTacToe Login Page");
final JButton btnLogin = new JButton("Click to login");

btnLogin.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
LoginDialog loginDlg = new LoginDialog(frame);
loginDlg.setVisible(true);
// if logon successfully
if(loginDlg.isSucceeded()){
btnLogin.setText("Hi " + loginDlg.getUsername() + "!");
}
}
});

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(btnLogin);
frame.setVisible(true);
}
}
class game extends JFrame {
public game()
{
JPanel panel=new JPanel();
JLabel label=new JLabel("Select Game Mode");
JButton button1=new JButton("AI");
JButton button2=new JButton("TWO PLAYER");
setVisible(true);
setTitle("GameMode");

label.setLayout(new BorderLayout(5,5));
panel.add(label);

panel.add(button1);
panel.add(button2);
getContentPane().add(panel);
}



public static void main(String args[])
{

}
}

when i compile and run the program
when the username and password box opens
after writing the username(given value in program) and password(given value in program)
i am not getting redirected to my game program


please help!!

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1966
    
    7

So how are you redirecting from the login page/frame to the game page/frame? Please show just the relevant code.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
ten doeschate
Greenhorn

Joined: Jun 22, 2013
Posts: 16
by this:
if (Login.authenticate(getUsername(), getPassword())) {

game p=new game();

p.setVisible(true);

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1966
    
    7

ten doeschate wrote:by this:
if (Login.authenticate(getUsername(), getPassword())) {

game p=new game();

p.setVisible(true);



Tested your code. Your "game" window is actually displaying only the content is cramp together. Also once successful login the JDialog should close and the original window (button with login) should close and make the Game main window the control. Currently the Game window buttons (close, min/max) are disabled.

Also I suggest you separate your classes into different files.
ten doeschate
Greenhorn

Joined: Jun 22, 2013
Posts: 16
can yu suggest me or give me some hint on how to proceed
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1966
    
    7

Well first you should separate the Game class and set it up so that it is your main window with default close operation.

Make the frame variable into instance variable so that you can dispose it in the LoginDialog class. The authenticate method in the Login class can become private inside LoginDialog class.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: output problem
 
Similar Threads
Swing New User Interface - Opinions Please
Using move next, previous, first and last t shift through the database using in GUI
Problem with default Submit
Restart login if a wrong password is writed. (Java Swing)
Help with a simple login Gui