---
# System prepended metadata

title: 2019-12-23 上課範例
tags: [Java計算機程式設計, 108-1]

---

# 2019-12-23 上課範例

[TOC]

## StudentDatabase.java (v3.0.0)

* 新增MenuBar
* 按下Menu的結束可以將視窗關閉

```java=
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
 * 學生資料庫視窗版
 * @author kChen
 * 
 * @version 3.0.0
 */

public class StudentDatabase {

	// 我們自己宣告的Feild
	private StudentList studentList;
	
	// WindowBuilder 宣告的
	private JFrame frame;
	private JTextField txtName;
	private JTextField txtStudentID;
	private JTextField txtChinese;
	private JTextField txtEnglish;
	private JTextField txtMath;
	private JTextArea txtrOutput;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StudentDatabase window = new StudentDatabase();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public StudentDatabase() {
		// WindowBuilder 初始化
		initialize();

		// 我們自己的初始化
		this.studentList = new StudentList();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("學生資料庫 v3.0.0");
		frame.setBounds(100, 100, 660, 469);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		txtName = new JTextField();
		txtName.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtName.setText("張三");
		txtName.setBounds(99, 71, 108, 30);
		frame.getContentPane().add(txtName);
		txtName.setColumns(10);

		JLabel lblName = new JLabel("姓名");
		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblName.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblName.setBounds(40, 74, 49, 25);
		frame.getContentPane().add(lblName);

		JLabel lblStudentID = new JLabel("學號");
		lblStudentID.setHorizontalAlignment(SwingConstants.RIGHT);
		lblStudentID.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblStudentID.setBounds(40, 29, 49, 25);
		frame.getContentPane().add(lblStudentID);

		txtStudentID = new JTextField();
		txtStudentID.setText("S01");
		txtStudentID.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtStudentID.setColumns(10);
		txtStudentID.setBounds(99, 26, 108, 30);
		frame.getContentPane().add(txtStudentID);

		txtChinese = new JTextField();
		txtChinese.setText("76");
		txtChinese.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtChinese.setColumns(10);
		txtChinese.setBounds(289, 26, 108, 30);
		frame.getContentPane().add(txtChinese);

		JLabel lblChinese = new JLabel("中文");
		lblChinese.setHorizontalAlignment(SwingConstants.RIGHT);
		lblChinese.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblChinese.setBounds(225, 29, 49, 25);
		frame.getContentPane().add(lblChinese);

		JLabel lblEnglish = new JLabel("英文");
		lblEnglish.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEnglish.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblEnglish.setBounds(225, 74, 49, 25);
		frame.getContentPane().add(lblEnglish);

		txtEnglish = new JTextField();
		txtEnglish.setText("80");
		txtEnglish.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtEnglish.setColumns(10);
		txtEnglish.setBounds(289, 71, 108, 30);
		frame.getContentPane().add(txtEnglish);

		JLabel lblMath = new JLabel("數學 ");
		lblMath.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMath.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblMath.setBounds(435, 29, 49, 25);
		frame.getContentPane().add(lblMath);

		txtMath = new JTextField();
		txtMath.setText("66");
		txtMath.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtMath.setColumns(10);
		txtMath.setBounds(493, 26, 108, 30);
		frame.getContentPane().add(txtMath);

		JButton btnAdd = new JButton("新增");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnAddClick();
			}
		});
		btnAdd.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		btnAdd.setBounds(514, 74, 87, 25);
		frame.getContentPane().add(btnAdd);

		txtrOutput = new JTextArea();
		txtrOutput.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtrOutput.setBounds(44, 141, 558, 256);
		frame.getContentPane().add(txtrOutput);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("檔案");
		mnFile.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		menuBar.add(mnFile);
		
		JMenuItem mntmOpen = new JMenuItem("開啟檔案");
		mntmOpen.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		mnFile.add(mntmOpen);
		
		JMenuItem mntmSave = new JMenuItem("儲存檔案");
		mntmSave.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		mnFile.add(mntmSave);
		
		JMenuItem mntmExit = new JMenuItem("結束");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mntmExitClick();
			}
		});
		mntmExit.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		mnFile.add(mntmExit);
		
		JMenu mnHelp = new JMenu("說明");
		mnHelp.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		menuBar.add(mnHelp);
		
		JMenuItem mntmAbout = new JMenuItem("關於");
		mntmAbout.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		mnHelp.add(mntmAbout);
	}

	/**
	 * 當Menu按結束時執行
	 */
	protected void mntmExitClick() {
		// 將視窗關閉
		this.frame.dispose();
	}

	/**
	 * 當按新增按鈕執行
	 */
	protected void btnAddClick() {
		// 取得視窗資料

		String id = this.txtStudentID.getText();
		String name = this.txtName.getText();
		String strChinese = this.txtChinese.getText();
		String strEnglish = this.txtEnglish.getText();
		String strMath = this.txtMath.getText();

		// 將字串轉換成數值
		int chinese = Integer.parseInt(strChinese);
		int english = Integer.parseInt(strEnglish);
		int math = Integer.parseInt(strMath);

		// 產生學生物件
		Student st = new Student(id, name, chinese, english, math);

		// 將學生物件加入至 StudentList
		this.studentList.add​(st);

		// 將 studentList 顯示至畫面
		this.txtrOutput.setText(studentList.toString());
	}
}

```

## Student.java (v0.1.1)

```java=
/**
 * 學生類別
 * 
 * @author kChen
 * @version 0.1.1
 *
 */
public class Student {

	/**
	 * 學生學號
	 */
	private String id;

	/**
	 * 學生姓名
	 */
	private String name;

	/**
	 * 學生的中文成績
	 */
	private int chinese;

	/**
	 * 學生的英文成績
	 */
	private int english;

	/**
	 * 學生的數學成績
	 */
	private int math;

	/**
	 * 建構子，預設學生資料
	 */
	public Student() {
		name = "老王";
		id = "S01";
		chinese = 95;
		english = 60;
		math = 78;
	}

	/**
	 * 自行輸入資料的建構子
	 * 
	 * @param id      學生學號
	 * @param name    學生姓名
	 * @param chinese 中文成績
	 * @param english 英文成績
	 * @param math    數學成績
	 */
	public Student(String id, String name, int chinese, int english, int math) {
		this.id = id;
		this.name = name;
		if (checkScore(chinese, english, math)) {
			this.chinese = chinese;
			this.english = english;
			this.math = math;
		} else 
			throw new java.lang.IllegalArgumentException("輸入的成績有誤，範圍不在0到100之間");
	}

	private boolean checkScore(int chinese, int english, int math) {
		boolean checkChinese = (0 <= chinese && chinese <= 100);
		boolean checkEnglish = (0 <= english && english <= 100);
		boolean checkMath = (0 <= math && math <= 100);

		if (checkChinese && checkEnglish && checkMath)
			return true;
		else
			return false;

	}

	/**
	 * 計算學生各科的總合
	 * 
	 * @return 學生各科的總合
	 */
	public int sum() {
		return this.chinese + this.english + this.math;
	}

	/**
	 * 計算學生各科的平均
	 * 
	 * @return 學生的各科的平均
	 */
	public double avg() {
		return this.sum() / 3.0;
	}

	@Override
	public String toString() {
		String str = this.id + ",";
		str += this.name + ",";
		str += this.chinese + ",";
		str += this.english + ",";
		str += this.math + ",";
		str += this.sum() + ",";
		str += this.avg();
		return str;
	}
}

```

## StudentDatabase.java (v3.0.1)

```java=
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 * 學生資料庫視窗版
 * @author kChen
 * 
 * @version 3.0.1
 */

public class StudentDatabase {

	// 我們自己宣告的Feild
	private StudentList studentList;
	
	// WindowBuilder 宣告的
	private JFrame frame;
	private JTextField txtName;
	private JTextField txtStudentID;
	private JTextField txtChinese;
	private JTextField txtEnglish;
	private JTextField txtMath;
	private JTextArea txtrOutput;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StudentDatabase window = new StudentDatabase();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public StudentDatabase() {
		// WindowBuilder 初始化
		initialize();

		// 我們自己的初始化
		this.studentList = new StudentList();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("學生資料庫 v3.0.0");
		frame.setBounds(100, 100, 660, 469);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		txtName = new JTextField();
		txtName.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtName.setText("張三");
		txtName.setBounds(99, 71, 108, 30);
		frame.getContentPane().add(txtName);
		txtName.setColumns(10);

		JLabel lblName = new JLabel("姓名");
		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblName.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblName.setBounds(40, 74, 49, 25);
		frame.getContentPane().add(lblName);

		JLabel lblStudentID = new JLabel("學號");
		lblStudentID.setHorizontalAlignment(SwingConstants.RIGHT);
		lblStudentID.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblStudentID.setBounds(40, 29, 49, 25);
		frame.getContentPane().add(lblStudentID);

		txtStudentID = new JTextField();
		txtStudentID.setText("S01");
		txtStudentID.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtStudentID.setColumns(10);
		txtStudentID.setBounds(99, 26, 108, 30);
		frame.getContentPane().add(txtStudentID);

		txtChinese = new JTextField();
		txtChinese.setText("76");
		txtChinese.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtChinese.setColumns(10);
		txtChinese.setBounds(289, 26, 108, 30);
		frame.getContentPane().add(txtChinese);

		JLabel lblChinese = new JLabel("中文");
		lblChinese.setHorizontalAlignment(SwingConstants.RIGHT);
		lblChinese.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblChinese.setBounds(225, 29, 49, 25);
		frame.getContentPane().add(lblChinese);

		JLabel lblEnglish = new JLabel("英文");
		lblEnglish.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEnglish.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblEnglish.setBounds(225, 74, 49, 25);
		frame.getContentPane().add(lblEnglish);

		txtEnglish = new JTextField();
		txtEnglish.setText("80");
		txtEnglish.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtEnglish.setColumns(10);
		txtEnglish.setBounds(289, 71, 108, 30);
		frame.getContentPane().add(txtEnglish);

		JLabel lblMath = new JLabel("數學 ");
		lblMath.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMath.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		lblMath.setBounds(435, 29, 49, 25);
		frame.getContentPane().add(lblMath);

		txtMath = new JTextField();
		txtMath.setText("66");
		txtMath.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtMath.setColumns(10);
		txtMath.setBounds(493, 26, 108, 30);
		frame.getContentPane().add(txtMath);

		JButton btnAdd = new JButton("新增");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnAddClick();
			}
		});
		btnAdd.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		btnAdd.setBounds(514, 74, 87, 25);
		frame.getContentPane().add(btnAdd);

		txtrOutput = new JTextArea();
		txtrOutput.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
		txtrOutput.setBounds(44, 141, 558, 256);
		frame.getContentPane().add(txtrOutput);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("檔案");
		mnFile.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		menuBar.add(mnFile);
		
		JMenuItem mntmOpen = new JMenuItem("開啟檔案");
		mntmOpen.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		mnFile.add(mntmOpen);
		
		JMenuItem mntmSave = new JMenuItem("儲存檔案");
		mntmSave.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		mnFile.add(mntmSave);
		
		JMenuItem mntmExit = new JMenuItem("結束");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mntmExitClick();
			}
		});
		mntmExit.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		mnFile.add(mntmExit);
		
		JMenu mnHelp = new JMenu("說明");
		mnHelp.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		menuBar.add(mnHelp);
		
		JMenuItem mntmAbout = new JMenuItem("關於");
		mntmAbout.setFont(new Font("微軟正黑體", Font.PLAIN, 14));
		mnHelp.add(mntmAbout);
	}

	/**
	 * 當Menu按結束時執行
	 */
	protected void mntmExitClick() {
		// 將視窗關閉
		this.frame.dispose();
	}

	/**
	 * 當按新增按鈕執行
	 */
	protected void btnAddClick() {
		// 取得視窗資料

		String id = this.txtStudentID.getText();
		String name = this.txtName.getText();
		String strChinese = this.txtChinese.getText();
		String strEnglish = this.txtEnglish.getText();
		String strMath = this.txtMath.getText();

		// 將字串轉換成數值
		int chinese = Integer.parseInt(strChinese);
		int english = Integer.parseInt(strEnglish);
		int math = Integer.parseInt(strMath);

		try {
		// 產生學生物件
		Student st = new Student(id, name, chinese, english, math);
		// 將學生物件加入至 StudentList
		this.studentList.add​(st);
		
		} catch (java.lang.IllegalArgumentException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "輸入有錯", JOptionPane.ERROR_MESSAGE);
		}


		// 將 studentList 顯示至畫面
		this.txtrOutput.setText(studentList.toString());
	}
}


```


###### tags: `Java計算機程式設計` `108-1`