.i faced with this problem in thses codes..the frame should
close when the button 'OK' is click,but in these codes,the frame did
not close after the button is click.there is no syntax errors..can
anyone pls help me look at it...thnx
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PasswordBox extends JFrame implements ActionListener
{
private JTextField username, password;
private JButton ok, cancel;
private JFrame PasswordBox;
public PasswordBox()
{
//super("Unlock Screensaver");
setSize(280,120);
JPanel backPanel = new JPanel();
backPanel.setLayout(new BorderLayout());
backPanel.setBackground(Color.blue);
//add(backPanel, BorderLayout.CENTER);
//panel.setBackground(Color.grey);
JPanel panel = new JPanel();
username = new JTextField(5);
panel.add(new JLabel("Username:", JLabel.RIGHT));
panel.add(username);
password = new JTextField(5);
panel.add(new JLabel("Password:", JLabel.RIGHT));
panel.add(password);
backPanel.add(panel, BorderLayout.CENTER);
JPanel OKPanel = new JPanel();
ok = new JButton("OK");
ok.addActionListener(this);
OKPanel.add(ok, BorderLayout.WEST);
JPanel CANCELPanel = new JPanel();
cancel = new JButton("CANCEL");
cancel.addActionListener(this);
CANCELPanel.add(cancel, BorderLayout.EAST);
JPanel buttonPanelss = new JPanel();
buttonPanelss.add(OKPanel);
buttonPanelss.add(CANCELPanel);
backPanel.add(buttonPanelss, BorderLayout.SOUTH);
setContentPane(backPanel);
}
public void clearInputFields()
{
username.setText("");
password.setText("");
}
public void actionPerformed(ActionEvent e)
{
Object button = e.getSource();
if(username.getText().trim().equals("fyp"))
{
if(password.getText().trim().equals("fyp"))
{
//Event originated from a Button
if ( button instanceof Button )
{
//Button in main applet
clicked
if ( button == ok )
{
//If frame is open,
close it
//if
(passwordBox.isVisible())
//{
PasswordBox.setVisible(false);
PasswordBox.dispose();
}
}
}
}
else
{
clearInputFields();
}
}
public static void main(String[] args)
{
PasswordBox pb = new PasswordBox();
pb.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
pb.pack();
pb.setVisible(true);
}
}