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.