Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

actionListener

  Asked By: James    Date: Feb 20    Category: Java    Views: 722
  

i have a problem in my application.it has no syntax error but my
button can't work..my first two buttons work pretty well but my last
button dun seems to work.i have the same codes as the two
butons...can anyone help look at my codes..thanks.my button s cant
work.

class PasswordBox extends JFrame
{
private JTextField username;
private JPasswordField password;
private JButton ok, cancel, s;
private JFrame PasswordBox;

public PasswordBox()
{
super("Unlock Screensaver"); //set the frame size
setSize(310,120);

JPanel backPanel = new JPanel();
backPanel.setLayout(new BorderLayout());
backPanel.setBackground(Color.blue);

JPanel panel = new JPanel();
username = new JTextField(5);
panel.add(new JLabel("Username:", JLabel.RIGHT));
panel.add(username);
password = new JPasswordField(5);
panel.add(new JLabel("Password:", JLabel.RIGHT));
panel.add(password);
backPanel.add(panel, BorderLayout.CENTER);

JPanel OKPanel = new JPanel();
ok = new JButton("OK");
OKPanel.add(ok, BorderLayout.WEST);

//add listener to the 'ok' button
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ok)
{
if(username.getText().trim().equals
("fyp"))
{
if(password.getText().trim
().equals("fyp"))
{
System.exit(0);
}
}
else
{

clearInputFields();
}
}
}
});


JPanel CANCELPanel = new JPanel();
cancel = new JButton("CANCEL");
CANCELPanel.add(cancel, BorderLayout.CENTER);

//add listener to the 'cancel' button
cancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == cancel)
{
setVisible(false);
dispose();

}
}
});


JPanel SCREENPanel = new JPanel();
s = new JButton("SCREENSAVER");
SCREENPanel.add(s, BorderLayout.EAST);

//add listener to the 'screensaver' button
s.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == s)
{
//PasswordBox pSaver = new PasswordBox
();
setVisible(false);
dispose();
}
}
});

JPanel buttonPanelss = new JPanel();
buttonPanelss.add(OKPanel);
buttonPanelss.add(CANCELPanel);
buttonPanelss.add(SCREENPanel);
backPanel.add(buttonPanelss, BorderLayout.SOUTH);
setContentPane(backPanel);

// Center the box
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width)
/ 2,(screenDim.height - frameDim.height) / 2);

}


public void clearInputFields()
{
username.setText("");
password.setText("");

}

}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Vilhelm Fischer     Answered On: Feb 20

how about to remove all e.getSource in every actionPerformed() ??
It hink you don't need that ...

 
Answer #2    Answered By: Kyle Fox     Answered On: Feb 20

I tried your code out.
I had to add some import statements to get it to compile:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class PasswordBox extends JFrame


I invoked the class  from another java source as follows:
import javax.swing.*;

public class Test extends JFrame{

public Test(){

}

public static void main (String[] args){
Test test = new Test();
test.init();
}


public void init(){
PasswordBox box = new PasswordBox();
box.show();
}


}

 
Didn't find what you were looking for? Find more on actionListener Or get search suggestion and latest updates.




Tagged: