Java实验一——习题册+登陆界面 源代码
1.小学习题册
源代码:
import java.util.*;
public class Xitice {
java.util.Random random=new java.util.Random(); //随机产生一个数字
public void set()
{
for(int a=0;a<=30;a++){
if(a==1||a-4*((a-1)/4)==1){
int n=random.nextInt(50)+1;
int m=random.nextInt(50)+30;
System.out.println(n+"+"+m+"=");
}
if(a==2||a-4*((a-1)/4)==2){
int n=random.nextInt(50)+30;
int m=random.nextInt(30)+1;
System.out.println(n+"-"+m+"=");
}
if(a==3||a-4*((a-1)/4)==3){
int n=random.nextInt(10)+0;
int m=random.nextInt(10)+0;
System.out.println(n+"*"+m+"=");
}
if(a==4||a-4*((a-1)/4)==4){
int n=random.nextInt(20)+10;
int m=random.nextInt(10)+1;
System.out.println(n+"➗"+m+"=");
}
}
}
public static void main(String[] args)
{
Xitice a=new Xitice();
a.set();
}
}
2.登陆界面
源代码:
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 javax.swing.JPasswordField;
public class Denglu extends JFrame implements ActionListener{
private JLabel usernameLabel;
private JTextField usernameTextField;
private JLabel passwordLabel;
private JTextField passwordField;
private JLabel DengluLabel;
private JTextField DengluField;
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.gray);
haha.setBounds(260, 70, 140, 30);
haha.setFont(new Font("汉真广标",Font.BOLD,10));
//验证码代码
DengluLabel =new JLabel("验证码");
DengluLabel.setBounds(60, 100, 70, 30);
DengluLabel.setFont(new Font("黑体",Font.BOLD,16));
DengluField =new JTextField();
DengluField.setBounds(140,105,60,20);
DengluField.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.BLUE);
bu1.setForeground(Color.white);
bu1.setBounds(130, 145, 100, 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(DengluLabel);
frame.add(DengluField);
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));
Denglu Swing=new Denglu();
Swing.add(frame);
frame.setVisible(true);
}
}