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

public class ClientProgram implements ActionListener{
    //static FilmCollection collection = new FilmCollection();
   	static boolean condition = true;
	static int memberPassword = 123;
	static JButton button1, button2, button3;

    public static void main(String[] args){
    	int mPassword;
        JFrame myFrame = new JFrame("Member Login"); //window's title
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//perform exit when click on 'X'
        Container myPane = myFrame.getContentPane();
        myPane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        setMyConstraints(c,0,0,GridBagConstraints.CENTER);
        myPane.add(getFieldPanel(),c);
        setMyConstraints(c,0,1,GridBagConstraints.CENTER);
        myPane.add(getButtonPanel(),c);
        myFrame.pack();
        myFrame.setVisible(true);
    }

   private static JPanel getFieldPanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.setBorder(BorderFactory.createTitledBorder("LOGIN")); //Name of Box
      GridBagConstraints c = new GridBagConstraints();

      setMyConstraints(c,0,0,GridBagConstraints.EAST);
      p.add(new JLabel("Member ID:"),c);
      setMyConstraints(c,1,0,GridBagConstraints.WEST);
      p.add(new JTextField(20),c);

      setMyConstraints(c,0,1,GridBagConstraints.EAST);
      p.add(new JLabel("Member Password:"),c);
      setMyConstraints(c,1,1,GridBagConstraints.WEST);
      p.add(new JTextField(20), c);

      return p;
   }

   private static JPanel getButtonPanel() {
      JPanel p = new JPanel(new GridBagLayout());

      p.add(button1 = new JButton("Login"));
      p.add(button2 = new JButton("Register"));
      p.add(button3 = new JButton("Exit"));

      button1.addActionListener(this);
      button2.addActionListener(this);
      button3.addActionListener(this);

	  return p;
   }

   private static void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) {
      c.gridx = gridx;
      c.gridy = gridy;
      c.anchor = anchor;
   }

  public void actionPerformed(ActionEvent e) {
	JButton buttonSelection = (JButton) e.getSource();
		if(buttonSelection==button1)
			checkLogin();
		/*else if(buttonSelection=button2){

		}
		else if(buttonSelection=button3){

		}
		else if(buttonSelection=button4){

		}*/
  }

	public static void checkLogin(){
		System.out.println("Checked");
	}

I faced a problem that i couldn't detect the actions made by the JButton eventhough i inserted AddListener method.

Recommended Answers

All 7 Replies

http://www.java2s.com/Code/JavaAPI/javax.swing/JButtonaddActionListenerActionListeneract.htm

You should put the button listener in a new module (maybe called ButtonListener)
that will hold and respond to your actions:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonListener implements ActionListener
{
  ButtonListener()
  {
  }

  public void actionPerformed(ActionEvent e)
  {
    if (e.getActionCommand().equals("Login"))
    {
		System.out.println("User Clicked Login");
    }

    if (e.getActionCommand().equals("Register"))
	{
		System.out.println("User Clicked Register");
    }

    if (e.getActionCommand().equals("Exit"))
	{
		System.out.println("User Clicked Exit");
    }
  }
}

Then you can change your class to extend JPanel

public class ClientProgram extends JPanel

Then you can change your getBuggonPanel() method like this:

private static JPanel getButtonPanel() {
      JPanel p = new JPanel(new GridBagLayout());

      p.add(button1 = new JButton("Login"));
      p.add(button2 = new JButton("Register"));
      p.add(button3 = new JButton("Exit"));

      button1.addActionListener(new ButtonListener());
      button2.addActionListener(new ButtonListener());
      button3.addActionListener(new ButtonListener());


	  return p;
   }

...and remove the other button listener code from your main ClientProgram module.

Your code does not compile. You need to get a clean compile before you can execute it.

Please post the full text of the compiler error messages here.

now the problem i've solved by using thines01 idea, but i am wondering why i cannot declare inside the ClientProgram?

i am wondering why i cannot declare inside the ClientProgram?

Please explain your problem.

alright, if i declare my button1, button2, button3 as JButton static
then inside the getButtonPanel() got errors

private static JPanel getButtonPanel() {      JPanel p = new JPanel(new GridBagLayout());       p.add(button1 = new JButton("Login"));      p.add(button2 = new JButton("Register"));      p.add(button3 = new JButton("Exit"));       button1.addActionListener(this);      button2.addActionListener(this);      button3.addActionListener(this); 	  return p;   }

the error message is non-static.........


by the way, i would like to ask how to read the value from the text that i want to read user ID and user Password.?

error message is non-static....

I recommend you get rid of ALL static usages (except main) and put your code into the constructor for the class. Have main() call the constructor: new <classname>();

In the future please post the full text of the error message.

You can declare the class inside the other class.
I just wouldn't do that.
Keeping them separate makes for cleaner code.
Also, if you're working with someone else at any time, you can work on one module and they can work on the other.
It also helps in debugging when you only have to deal with one module at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.