按Enter键以及鼠标点击(actionlistener)

时间:2017-07-02 10:32:32

标签: java actionlistener keylistener

import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.KeyStroke;
import java.awt.*;
import java.awt.event.*;

    public class Front implements ActionListener 
      {
    JFrame frame;
    JPanel mainpanel,headingpanel,displaypanel,intropanel;
    JLabel headingLabel,useridLabel,passwordLabel,introLabel;
    JTextField useridField;
    JPasswordField passwordField;
    JButton login;
    BorderLayout bl;
    GridBagLayout gbl;
    GridBagConstraints gbc;


    public Front() 
    {
        frame=new JFrame("CGRS(COMPLAINT GROSS RELEVANT SYSTEM)");

        mainpanel=new JPanel();
        headingpanel=new JPanel();
        displaypanel=new JPanel();
        intropanel=new JPanel();


        headingLabel=new JLabel("hi there" ,132001");
        headingLabel.setForeground(Color.WHITE);
        Font font=new Font("Arial Black",Font.BOLD,32);

        headingLabel.setFont(font);
        useridLabel=new JLabel("Username");
        useridLabel.setForeground(Color.BLUE);
        Font USERIDfont=new Font("Arial Rounded MT Bold",Font.BOLD,15);
        useridLabel.setFont(USERIDfont);
        passwordLabel=new JLabel("Password");
        passwordLabel.setForeground(Color.BLUE);
        useridLabel.setForeground(Color.BLUE);
        Font passfont=new Font("Arial Rounded MT Bold",Font.BOLD,15);
        passwordLabel.setFont(passfont);
        introLabel=new JLabel("DEVELOPED BY ");
        introLabel.setForeground(Color.WHITE);
        Font introfont=new Font("Arial Black",Font.BOLD,15);
        introLabel.setFont(introfont);
        gbl=new GridBagLayout();
        gbc=new GridBagConstraints();



        useridField=new JTextField(10);

        //useridField.setBorder(null);
        passwordField=new JPasswordField(10);

        //passwordField.setBorder(null);

        login=new JButton("Login");
        login.setBorder(null);
        login.setForeground(Color.WHITE);


        bl=new BorderLayout();

        mainpanel.setLayout(bl);
        displaypanel.setLayout(gbl);
        frame.add(mainpanel);

        mainpanel.add(headingpanel,BorderLayout.NORTH);
        mainpanel.add(displaypanel,BorderLayout.CENTER);
        mainpanel.add(intropanel,BorderLayout.SOUTH);

        headingpanel.setBackground(Color.BLUE);
        displaypanel.setBackground(Color.WHITE);
        intropanel.setBackground(Color.BLUE);
        headingpanel.add(headingLabel);

        gbc.insets=new Insets(8,8,8,8);
        gbc.gridx=0;
        gbc.gridy=0;
        gbl.setConstraints(useridLabel,gbc);
        displaypanel.add(useridLabel);

        gbc.gridx=1;
        gbc.gridy=0;
        gbl.setConstraints(useridField, gbc);
        displaypanel.add(useridField);

        gbc.gridx=0;
        gbc.gridy=1;
        gbl.setConstraints(passwordLabel, gbc);
        displaypanel.add(passwordLabel,gbc);

        gbc.gridx=1;
        gbc.gridy=1;
        gbl.setConstraints(passwordField, gbc);
        displaypanel.add(passwordField);

        gbc.gridx=1;
        gbc.gridy=3;

        gbl.setConstraints(login, gbc);

        login.setPreferredSize(new Dimension(60, 30));
        login.setBackground(Color.BLUE);
        displaypanel.add(login);
        login.addActionListener(this);


        Toolkit t=Toolkit.getDefaultToolkit();
        Dimension d=t.getScreenSize();
        frame.setLocation((d.width-900)/2,(d.height-500)/2);

        intropanel.add(introLabel);

        frame.add(mainpanel);
        //frame.setLocation(400,250);
        frame.setSize(900,500);
        frame.setResizable(false);
        frame.setVisible(true);
    }       

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

    }

    public void actionPerformed(ActionEvent evt)
    {
        String Uname=useridField.getText();
        String passf=passwordField.getText();
        if(evt.getSource()==login)
        {


            if(Uname.equals("kartik")&&passf.equals("12345678"))
            {
            JOptionPane.showMessageDialog(frame,"successfully Loggedin");
                //new Staff();

                //frame.setVisible(false);

            }
            else
            {
                JOptionPane.showMessageDialog(frame,"Invalid Id Password");
            }
        }
    }
}

0 个答案:

没有答案