2017 java期末上机练习

仅供参考!

 

一、最大值、最小值、平均数

 1 package examination;
 2 
 3 import java.util.Arrays;
 4 import java.util.Scanner;
 5 
 6 /**
 7  * 1. 从键盘输入10个成绩 
 8  * 2. 对这10个数字进行排序并从小到大显示 
 9  * 3. 输出最大值和最小值 
10  * 4. 输出平均值
11  *
12  */
13 public class gradeTest
14 {
15     public static void main(String[] args)
16     {
17         int[] grades = new int[10]; // 10个成绩
18         float sum = 0; // 10个成绩的和
19 
20         Scanner scan = new Scanner(System.in); // 从键盘接收数据
21 
22         System.out.println("请输入10个整数并以空格分隔:");
23         // 示例:33 25 13 67 76 85 90 83 99 91
24         for (int i = 0; i < 10; i++)
25         {
26             grades[i] = scan.nextInt(); // 成绩 赋值
27             sum += grades[i]; // 成绩累加
28         }
29 
30         scan.close(); // 关闭 Scanner 对象
31         Arrays.sort(grades); // 升序排序
32 
33         // 遍历排序结果
34         for (int i = 0; i < grades.length; i++)
35         {
36             int j = grades[i];
37             System.out.print(j + " ");
38         }
39         // 输出最大值、最小值、平均数
40         System.out.println();
41         System.out.println("最大值:" + grades[grades.length - 1]);
42         System.out.println("最小值:" + grades[0]);
43         System.out.println("平均数:" + sum / grades.length);
44     }
45 
46 }

2017 java期末上机练习

 

二、简单GUI测试

要求:
1.用户登录界面 可以通过按钮进入注册界面;
2.注册界面具有跳转到留言板和查看留言功能;
3.留言板内容可以初始化、保存到ly.txt文件

 

- 登录界面

 

 1 package examination;
 2 
 3 import javax.swing.*;
 4 import java.awt.event.ActionListener;
 5 import java.awt.GridLayout;
 6 import java.awt.event.ActionEvent;
 7 
 8 public class Login extends JFrame
 9 {
10     JLabel jlUser, jlPwd;   //标签
11     JTextField jtfUsername; //单行文本框
12     JPasswordField jpfPwd;  //密码框
13 
14     JPanel jp1, jp2, jp3; //面板容器
15     JButton jbtnRegist;   //注册按钮
16 
17     public Login()
18     {
19         jtfUsername = new JTextField(10); // 创建单行文本框
20         jpfPwd = new JPasswordField(10);  // 创建密码文本框
21 
22         jlUser = new JLabel("用户名"); // 创建标签
23         jlPwd = new JLabel("密   码");  // 创建标签
24 
25         jbtnRegist = new JButton("注  册"); // 创建“注册”按钮
26         
27         // 监听器:按钮事件
28         jbtnRegist.addActionListener(new ActionListener() 
29         {
30             public void actionPerformed(ActionEvent arg0) 
31             {
32                 new Regist();
33             }
34         });
35         // jb2 = new JButton("取消");
36         jp1 = new JPanel();
37         jp2 = new JPanel();
38         jp3 = new JPanel();
39 
40         // 设置 网格 布局
41         getContentPane().setLayout(new GridLayout(3, 1));
42 
43         // 面板1添加用户名和文本框
44         jp1.add(jlUser);
45         jp1.add(jtfUsername);
46 
47         // 面板2添加密码和密码输入框
48         jp2.add(jlPwd);
49         jp2.add(jpfPwd);
50 
51         jp3.add(jbtnRegist); // 面板3添加注册按钮
52 
53         // 将三块面板添加到登陆框
54         getContentPane().add(jp1);
55         getContentPane().add(jp2);
56         getContentPane().add(jp3);
57         
58         // 设置显示
59         this.setVisible(true);
60         this.setTitle("登录");
61         this.setSize(300, 200);
62         this.setLocation(200, 200);
63     
64         // 退出程序
65         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66 
67     }
68 
69     public static void main(String[] args)
70     {
71         new Login();
72     }
73 }

- 注册界面

 

 1 package examination;
 2 
 3 import javax.swing.*;
 4 import java.awt.event.ActionListener;
 5 import java.io.BufferedReader;
 6 import java.io.FileReader;
 7 import java.io.IOException;
 8 import java.awt.event.ActionEvent;
 9 
10 public class Regist extends JFrame
11 {
12     JButton jbtnCheck;   // “查看留言”按钮
13     JButton jbtnMessage; // “留言”按钮
14     
15     JPanel jPanel;       //面板容器
16 
17     public Regist()
18     {
19         jbtnMessage = new JButton("留    言");
20         jbtnCheck = new JButton("查看留言");
21 
22         // 按钮事件:编辑留言
23         jbtnMessage.addActionListener(new ActionListener()
24         {
25             public void actionPerformed(ActionEvent arg0)
26             {
27                 new MessageBoard();
28             }
29         });
30         // 按钮事件:查看留言
31         jbtnCheck.addActionListener(new ActionListener()
32         {
33             public void actionPerformed(ActionEvent arg0)
34             {
35                 String msg = readFile();
36                 // 查看文件中的留言
37                 if (!msg.equals(""))
38                 {
39                     JOptionPane.showMessageDialog(null, msg, "留言板", JOptionPane.INFORMATION_MESSAGE); // 消息提示框
40                 }
41                 else 
42                 {
43                     JOptionPane.showMessageDialog(null, "您还没有留言!", "提示", JOptionPane.ERROR_MESSAGE);
44                 }
45             }
46         });
47 
48         jPanel = new JPanel();
49         jPanel.add(jbtnMessage);
50         jPanel.add(jbtnCheck);
51 
52         getContentPane().add(jPanel);
53 
54         // 设置显示
55         this.setVisible(true);
56         this.setTitle("注册");
57         this.setSize(300, 200);
58         this.setLocation(220, 200);
59 
60         // 关闭当前窗口:释放本窗口资源,但不退出主程序
61         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
62 
63     }
64     
65     /**
66      * 读文件,并返回到字符串
67      * @return
68      */
69     protected static String readFile()
70     {
71         String fileName = "./ly.txt"; // 默认读取当前项目下
72         String msg = "";  //留言内容
73         try
74         {
75             FileReader fr = new FileReader(fileName);
76             BufferedReader bfr = new BufferedReader(fr);
77 
78             String str = null;
79             while ((str = bfr.readLine()) != null)
80             {
81                 msg += str + "
";
82             }
83             bfr.close();
84             fr.close();
85 
86         } catch (IOException ioException)
87         {
88             
89         }
90         
91         return msg;
92     }
93     
94     public static void main(String[] args)
95     {
96         new Regist();
97     }
98 }

- 留言板界面

 

 1 package examination;
 2 
 3 import javax.swing.*;
 4 import java.awt.event.ActionListener;
 5 import java.io.BufferedWriter;
 6 import java.io.FileWriter;
 7 import java.io.IOException;
 8 import java.awt.BorderLayout;
 9 import java.awt.event.ActionEvent;
10 
11 public class MessageBoard extends JFrame
12 {
13     JTextArea jTextArea; // 多行文本框
14     JButton jbtnSave;    // “保存”按钮
15     JPanel jPanel;       // 面板容器
16 
17     public MessageBoard()
18     {
19         jTextArea = new JTextArea(); // 创建文本编辑区
20         jTextArea.setLineWrap(true); // 自动换行
21         
22         // 初始化留言文本
23         String msg = Regist.readFile();
24         // 查看文件中的留言
25         if (!msg.equals(""))
26         {
27             jTextArea.setText(msg);
28         }
29 
30         jbtnSave = new JButton("保存留言");
31 
32         // 按钮事件:编辑留言,并保存到目录下为“ly.txt”
33         jbtnSave.addActionListener(new ActionListener()
34         {
35             public void actionPerformed(ActionEvent arg0)
36             {
37                 saveFile(); // 保存到文件
38                 dispose();  // 关闭当前窗口
39             }
40         });
41 
42         // 设置边界布局
43         getContentPane().setLayout(new BorderLayout());
44         getContentPane().add(jbtnSave, BorderLayout.SOUTH);
45         getContentPane().add(jTextArea, BorderLayout.CENTER);
46 
47         // 设置显示
48         this.setVisible(true);
49         this.setTitle("留言板");
50         this.setSize(300, 200);
51         this.setLocation(240, 200);
52 
53         // 关闭当前窗口:释放本窗口资源,但不退出主程序
54         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
55 
56     }
57 
58     private void saveFile()
59     {
60         String fileName = "./ly.txt"; // 默认保存当前项目下
61 
62         try
63         {
64             FileWriter fw = new FileWriter(fileName);
65             BufferedWriter bfw = new BufferedWriter(fw);
66             bfw.write(jTextArea.getText(), 0, jTextArea.getText().length());
67             bfw.flush();
68             fw.close();
69 
70             JOptionPane.showMessageDialog(null, "留言成功!"); // 消息提示框
71 
72         } catch (IOException ioException)
73         {
74             JOptionPane.showMessageDialog(null, "留言失败!", "错误", JOptionPane.ERROR_MESSAGE);
75         }
76     }
77 
78     public static void main(String[] args)
79     {
80         new MessageBoard();
81     }
82 }

2017 java期末上机练习

 

三、制作一个提醒器

提示:
1.能够设计一个时间和一个内容,当到了设定时间弹出对话框显示设定内容;
2.可以设置好友生日,到了生日的前一天能够提示“明天是那个好友的生日”

 

 1 package examination;
 2 
 3 import java.awt.event.ActionEvent;  
 4 import java.awt.event.ActionListener;  
 5 import java.text.DateFormat;  
 6 import java.text.SimpleDateFormat;  
 7 import java.util.Date;  
 8   
 9 import javax.swing.JFrame;  
10 import javax.swing.JLabel;  
11 import javax.swing.Timer;  
12   
13 /**  
14  * 测试swing中Timer的使用  
15  * 一个显示时间的GUI程序  
16  * @author wasw100  
17  *   
18  */  
19 public class TimerTest extends JFrame implements ActionListener {  
20     // 一个显示时间的JLabel  
21     private JLabel jlTime = new JLabel();  
22     private Timer timer;  
23   
24     public TimerTest() {  
25         setTitle("Timer测试");  
26         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
27         setSize(180, 80);  
28         add(jlTime);  
29           
30         //设置Timer定时器,并启动  
31         timer = new Timer(500, this);  
32         timer.start();
33         setVisible(true);  
34     }  
35   
36     /**  
37      * 执行Timer要执行的部分,  
38      */  
39     @Override  
40     public void actionPerformed(ActionEvent e) {  
41         DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
42         Date date = new Date();  
43         jlTime.setText(format.format(date));  
44   
45     }  
46   
47     public static void main(String[] args) {  
48         new TimerTest();  
49     }  
50 }  

2017 java期末上机练习