十一Java作业三

二、编程实现用户登录界面需要输入验证码。

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JLabel;

import java.awt.Dimension;

import javax.swing.JPasswordField;

public class Yanzhengma extends JFrame implements ActionListener{

private JLabel usernameLabel;

private JTextField usernameTextField;

private JLabel passwordLabel;

private JTextField passwordField;

private JLabel YanzhengmaLabel;

private JTextField YanzhengmaField;

private JLabel ramstr;

private JLabel haha;

private JButton bu1;

private JButton bu2;

public void add(JFrame frame) {

frame.setTitle("请登录");

frame.setLayout(null);

usernameLabel =new JLabel("登录名");

usernameLabel.setBounds(60, 40, 70, 30);

usernameLabel.setFont(new Font("宋体",Font.BOLD,16));

usernameTextField =new JTextField();

usernameTextField.setBounds(140,45,120,20);

usernameTextField.setFont(new Font("宋体",Font.BOLD,16));

usernameTextField.setHorizontalAlignment(JTextField.CENTER);

passwordLabel =new JLabel("密码");

passwordLabel.setBounds(60, 70, 70, 30);

passwordLabel.setFont(new Font("宋体",Font.BOLD,16));

passwordField =new JPasswordField();

passwordField.setBounds(140,75,120,20);

passwordField.setFont(new Font("宋体",Font.BOLD,16));

haha =new JLabel("忘记用户名/密码?");

haha.setForeground(Color.blue);

haha.setBounds(260, 70, 140, 30);

haha.setFont(new Font("汉真广标",Font.BOLD,10));

YanzhengmaLabel =new JLabel("验证码");

YanzhengmaLabel.setBounds(60, 100, 70, 30);

YanzhengmaLabel.setFont(new Font("宋体",Font.BOLD,16));

YanzhengmaField =new JTextField();

YanzhengmaField.setBounds(140,105,60,20);

YanzhengmaField.setFont(new Font("宋体",Font.BOLD,16));

String result = "";

for(int i = 0 ; i < 6 ; i ++)

{

int intVal = (int)(Math.random() * 26 + 97);

result = result + (char)intVal;

}

ramstr =new JLabel(result);

ramstr.setBounds(220, 100, 70, 30);

ramstr.setFont(new Font("汉真广标",Font.BOLD,16));

bu1=new JButton();

bu1.setText("登陆");

bu1.setBackground(Color.ORANGE);

bu1.setForeground(Color.white);

bu1.setBounds(120, 145, 120, 30);

getContentPane().add(bu1);

bu2=new JButton();

bu2.setText("快速注册");

bu2.setBounds(120, 185, 120, 30);

getContentPane().add(bu2);

frame.add(usernameLabel);

frame.add(usernameTextField);

frame.add(passwordLabel);

frame.add(passwordField);

frame.add(haha);

frame.add(YanzhengmaLabel);

frame.add(YanzhengmaField);

frame.add(ramstr);

frame.add(bu1);

frame.add(bu2);

}

public static void main(String []args) {

JFrame frame =new JFrame("JFrame窗口");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400,300);

Dimension displaySize =Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize= frame.getSize();

if(frameSize.width>displaySize.width)

frameSize.width=displaySize.width;

if(frameSize.height>displaySize.height)

frameSize.height=displaySize.height;

frame.setLocation((displaySize.width-frameSize.width),(displaySize.height-frameSize.height));

Yanzhengma Swing=new Yanzhengma();

Swing.add(frame);

frame.setVisible(true);

}

}

猜你喜欢

转载自www.cnblogs.com/gkl20173667/p/9752557.html