2021Java期末加油


前言

前几天不是实现了去年的题目了吗?就那个登录窗口加随机数排序,但那也是我摸索着写的,所以有点乱,今天又重新整理了一下思路,感觉好很多,分享给大家。

下面是代码运行的演示视频:

2020年Java期末再现


J10

import javax.swing.*;

public class J10 extends JFrame {
	
	J10() {
		setTitle("登录窗口");
		setSize(600, 400);
		setVisible(true);
	}

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

}

J11

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class J11 extends J10 implements ActionListener {
	
	JButton b1, b2;
	
	J11() {
		setLayout(new GridLayout(3, 1));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		
		JLabel l1 = new JLabel("账号:");
		JLabel l2 = new JLabel("密码:");
		JTextField t = new JTextField(16);
		JPasswordField pass = new JPasswordField(16);
		
		b1 = new JButton("登录");
		b2 = new JButton("注册");
		b1.addActionListener(this);
		b2.addActionListener(this);
		
		p1.add(l1); p1.add(t);
		p2.add(l2); p2.add(pass);
		p3.add(b1); p3.add(b2);
		add(p1); add(p2); add(p3);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b2) {
			new J12().setTitle("注册窗口");
			this.setVisible(false);
		}
	}

	public static void main(String[] args) {new J11().setVisible(true);}

}

J12

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class J12 extends J10 implements ActionListener {
	
	JButton b;
	
	J12() {
		setLayout(new GridLayout(4, 1));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		
		JLabel l1 = new JLabel("申请账号:");
		JLabel l2 = new JLabel("设置密码:");
		JLabel l3 = new JLabel("确认密码:");
		
		JTextField t = new JTextField(16);
		JPasswordField pass1 = new JPasswordField(16);
		JPasswordField pass2 = new JPasswordField(16);
		
		b = new JButton("注册并登录");
		b.addActionListener(this);
		
		p1.add(l1); p1.add(t);
		p2.add(l2); p2.add(pass1);
		p3.add(l3); p3.add(pass2);
		p4.add(b);
		add(p1); add(p2); add(p3); add(p4); 
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == b) {
			new J13().setTitle("随机数排序窗口");
			this.setVisible(false);
		}
	}
	
	public static void main(String[] args) {new J12().setVisible(true);}

}

J13

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class J13 extends J10 implements ActionListener {
	
	JButton b;
	static String str = "",res  = "";
	static int[] a = new int[10];
	
	J13() {
		Random r = new Random();
		for (int i = 0; i < 10; i ++) {
			a[i] = r.nextInt(100);
			str = str + a[i] + " ";
		}
		
		//冒泡排序
		for (int i = 0; i < 9; i ++)
			for (int j = 0; j < 9-i; j ++)
				if (a[j] > a[j+1]) { int t = a[j]; a[j] = a[j+1]; a[j+1] = t;}
		
//		//选择排序
//		for (int i = 0; i < 9; i ++)
//			for (int j = i+1; j < 10; j ++)
//				if (a[i] > a[j]) {int t = a[i]; a[i] = a[j]; a[j] = t;}
		
		for (int i = 0; i < 10; i ++)
			res = res + a[i] + " ";
		
/* **************************************************************************************** */
		
		setLayout(new GridLayout(2, 1));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		
		JLabel l = new JLabel("十个随机数:");
		JTextArea t = new JTextArea(str, 1, 20);
		b = new JButton("点击进行排序");
		b.addActionListener(this);
	
		p1.add(l); p1.add(t);
		p2.add(b);
		add(p1); add(p2);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b) new J14();
	}

	public static void main(String[] args) {new J13().setVisible(true);}

}

J14

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class J14 extends J10 implements ActionListener {
	JButton b;
	
	J14() {
		setLayout(new GridLayout(2, 1));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		
		JLabel l = new JLabel("排序后的数:");
		JTextArea t = new JTextArea(J13.res, 1, 20);
		b = new JButton("点击结束");
		b.addActionListener(this);
		
		p1.add(l);p1.add(t);
		p2.add(b);
		add(p1); add(p2);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b) System.exit(0);
	}

	public static void main(String[] args) {new J14().setVisible(true);}

}

总结

J11、J12、J3、J14四个Jframe子类实现了四个窗口,一一对应,如果这次考试考其他算法,只需对J13中的随机数排序算法换一下就行。

最后盲猜一波:随机数+排序80%、二分查找50%、100以内素数10%、随机数猜数游戏10%、杨辉三角1%,最短路0%。

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值