0
public class Login extends JFrame{
    JFrame frame; //frame
    JTextField field; //to get username
    JPasswordField p; //password field
    JLabel l; //used for printing on frame
    JButton b;

    Login() {

        frame = new JFrame("Login");
        frame.setSize(350,200);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        l = new JLabel("Enter Username");
        l.setLocation(10,10);
        l.setSize(l.getPreferredSize());
        frame.add(l);

        field = new JTextField();
        field.setColumns(15);
        field.setSize(field.getPreferredSize());

        field.setLocation(120,10);
        frame.add(field);

        l = new JLabel("Enter Password");
        l.setLocation(10,40);
        l.setSize(l.getPreferredSize());
        frame.add(l);

        p = new JPasswordField();
        p.setColumns(15);
        p.setSize(p.getPreferredSize());
        p.setLocation(120,40);
        frame.add(p);

        b = new JButton("OK");
        b.setSize(b.getPreferredSize());
        b.setLocation(120, 80);
        frame.add(b);

        frame.setVisible(true);
    }


    private class b implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
           String str;
           str = field.getText();
           if(str.equals("")) {
               JOptionPane.showMessageDialog(null,"Please enter username");
               field.requestFocusInWindow();
           } else {

           }
        }

    }

    public static void main (String[] args) {
        new Login();
    }
}

the button won't functioning when I'm hit it

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Zaem Shakkir
  • 51
  • 10

2 Answers2

4

the button won't functioning when I'm hit it

You need to add the ActionListener to the button:

b = new JButton("OK");
b.addActionListener( new b() );

Make your class names more descriptive. "b" is not descriptive. Also, class names should start with an upper case character.

Don't use null layouts and setBounds(...). Swing was designed to be used with Layout Managers. Keep a link to the tutorial handy for Swing basics.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • thank god, its work . actually the code I'm using is the code that been copied from the internet and just mix it . @camickr – Zaem Shakkir Nov 13 '15 at 14:24
  • 1
    @ZaemShakkir, glad it helped. Don't forget to "accept" answers when you get help, by clicking on the check mark, so people know the problem has been solved. – camickr Nov 13 '15 at 15:24
2

Take a look at How to Write an Action Listeners and How to Use Buttons, Check Boxes, and Radio Buttons.

Basically, you never register the ActionListener with your JButton

b.addActionListener(new b());

You code would be easier to read if you used meaningful variable names and followed the establishing coding conventions of the language. Have a look at Code Conventions for the Java TM Programming Language, for more details, it will make it easier for people to read your code and for you to read others

You may also want to have a read of You might want to have a read of Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?, but since you're discarded the layout manager, the use of setPreferredSize is completely pointless.

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366