You are on page 1of 8

JAVA

Java GUI
JTextField JPasswordField

1 passwords.txt

1
public class Login {
private JFrame frame;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JPasswordField jf;
private JTextField jt;
private JButton yes;
private JButton no;
private int i=0;
public Login(){
frame=new JFrame("");

label1=new JLabel("");
label2=new JLabel(" ");
label3=new JLabel(" ");
jf=new JPasswordField(15);
jt=new JTextField(15);
yes=new JButton("");
no=new JButton("");
init();
addEventHandler();
}
2
public void init(){
JPanel north=new JPanel();
JPanel center=new JPanel();
JPanel south=new JPanel();
north.setLayout(new FlowLayout());
center.setLayout(new FlowLayout(2));
south.setLayout(new FlowLayout());
north.add(label1);
center.add(label2);
center.add(jt);
center.add(label3);
center.add(jf);
south.add(yes);
south.add(no);
frame.setLayout(new BorderLayout());
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.add(south,BorderLayout.SOUTH);
}
public void showMe(){
label1.setFont(new Font("",Font.BOLD,16));

frame.setLocation(600, 400);
frame.setSize(250,200);
frame.setResizable(false);//
frame.setVisible(true);
//frame.pack();//
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
3

public void addEventHandler(){


yes.addActionListener(
new ActionListener(){
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
if(jt.getText().equals("abc")&jf.getText().equals("123"))
{
JOptionPane.showMessageDialog(null,

"");

}
else{
if(i==2){
System.exit(0);
}
else{
i++;
JOptionPane.showMessageDialog(null,
","+(3-i)+"");
}
}
});

no.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args){
new Login().showMe();
}
}

import
import
import
import
import
import
import
import
import
import
import
import
import
import

java.awt.BorderLayout;
java.awt.Color;
java.awt.FlowLayout;
java.awt.Font;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JOptionPane;
javax.swing.JPanel;
javax.swing.JPasswordField;
javax.swing.JTextField;
java.io.*;

public class Login {


private JFrame frame;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JPasswordField jf;
private JTextField jt;
private JButton yes;
private JButton no;
private int i=0;

public Login(){
frame=new JFrame("");
label1=new JLabel("");
label2=new JLabel(" ");
label3=new JLabel(" ");
jf=new JPasswordField(15);
jt=new JTextField(15);
yes=new JButton("");
no=new JButton("");
init();
addEventHandler();
}
public void init(){
JPanel north=new JPanel();
JPanel center=new JPanel();
JPanel south=new JPanel();
north.setLayout(new FlowLayout());
center.setLayout(new FlowLayout(2));
south.setLayout(new FlowLayout());
north.add(label1);
center.add(label2);
center.add(jt);
center.add(label3);
center.add(jf);
south.add(yes);
south.add(no);
frame.setLayout(new BorderLayout());
frame.add(north,BorderLayout.NORTH);

frame.add(center,BorderLayout.CENTER);
frame.add(south,BorderLayout.SOUTH);
}
public void showMe(){
label1.setFont(new Font("",Font.BOLD,16));
frame.setLocation(600, 400);
frame.setSize(250,200);
frame.setResizable(false);//
frame.setVisible(true);
//frame.pack();//
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addEventHandler(){
yes.addActionListener(
new ActionListener(){
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{

if(jt.getText().equals("abc")&jf.getText().equals("123"))
{
JOptionPane.showMessageDialog(null,
");
}
else{
if(i==2){
System.exit(0);
}
else{
i++;

"

JOptionPane.showMessageDialog(null, "
,"+(3-i)+"");
}
}
}
});
no.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args){
new Login().showMe();
}
}

You might also like