文章目录
- 前言
- J10
- J11
- J12
- J13
- J14
- 总结
前言
前几天不是实现了去年的题目了吗?就那个登录窗口加随机数排序,但那也是我摸索着写的,所以有点乱,今天又重新整理了一下思路,感觉好很多,分享给大家。
下面是代码运行的演示视频:
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%。