Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

the frame should close when the button 'OK' is clic

  Asked By: Koila    Date: Aug 06    Category: Java    Views: 2612
  

.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);
}

}

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Qadriyyah Malik     Answered On: Aug 06

you have this line:

if (button instanceof Button)

which will never evaluate to true as your 'OK' button  is a JButton
and this is not descended from Button. You could change it to:

if (button instanceof JButton)

though this is not really necessary as the line you have later is
sufficient to check that the OK button has been pressed:

if(button == ok)

You will have more problems when you get that part working, like when
you call

PasswordBox.setVisible(false)

You have variables with the same name as the class, PasswordBox,
which is VERY confusing.

 
Answer #2    Answered By: Terry Williams     Answered On: Aug 06

i have change this line to the following line
if (button instanceof JButton)..
and true enough,i faced these problems...
below is the error when i click  the button..

java.lang.NullPointerException
at PasswordBox.actionPerformed(PasswordBox.java:80)
at javax.swing.AbstractButton.fireActionPerformed
(AbstractButton.java:1764)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed
(AbstractButton.java:1817)
at javax.swing.DefaultButtonModel.fireActionPerformed
(DefaultButtonModel.java:419)
at javax.swing.DefaultButtonModel.setPressed
(DefaultButtonModel.java:257)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased
(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5093)
at java.awt.Component.processEvent(Component.java:4890)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3598)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3439)
at java.awt.LightweightDispatcher.retargetMouseEvent
(Container.java:3450)
at java.awt.LightweightDispatcher.processMouseEvent
(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent
(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1585)
at java.awt.Component.dispatchEvent(Component.java:3439)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy
(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents
(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents
(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run
(EventDispatchThread.java:99)

 
Answer #3    Answered By: Casey Montgomery     Answered On: Aug 06

as I said last time, you have a member variable with the same name as the class:

private JFrame PasswordBox;

this is a bad idea anyway. You don't actually use this variable, so just delete
the above line. In actionPerformed() you could replace this:

PasswordBox.setVisible(false);
PasswordBox.dispose();

with this:
this.setVisible(false);
this.dispose();

Which at least makes the window disappear when you enter the correct username &
password though it leaves the program running with no way of quiting it,
depending on the OS you are using.

 
Answer #4    Answered By: Jonathan Brown     Answered On: Aug 06

my JSP page cannot lookup home interface. plz help

 
Didn't find what you were looking for? Find more on the frame should close when the button 'OK' is clic Or get search suggestion and latest updates.




Tagged: