1. 程式人生 > >Java專案開發成績管理系統(五) 成績資訊模組—主介面設定

Java專案開發成績管理系統(五) 成績資訊模組—主介面設定

主介面和上一篇部落格大致相同,(編寫過程點選上一篇部落格),

效果如下:

程式碼如下:

package grade;

import course.CIndex;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import student.SIndex;
import dao.CourseManager;
import dao.GradeManager;
import dao.StudentManager;

public class GIndex extends Frame implements ActionListener{
	private Panel p1,p2,p3,p4;
	private Menu menu1,menu2;
	private MenuItem sitem1;     //選單項“學生資訊”
	private MenuItem citem1;     //選單項“課程資訊”
	private MenuBar bar;
	private Button btn1,addbtn,upbtn,delbtn;
	private Label lab;
	private TextField tf;
	private JScrollPane jsp;
	private GradeManager sm;
	private JTable jtab;
	private JOptionPane jop;
	
	public void menuCreate() {
		bar = new MenuBar();
		menu1 = new Menu("學生資訊");
		menu2 = new Menu("課程資訊");
		sitem1 = new MenuItem("學生資訊");
		sitem1.addActionListener(this);
		menu1.add(sitem1);
		citem1 = new MenuItem("課程資訊");
		citem1.addActionListener(this);
		menu2.add(citem1);
		bar.add(menu1);
		bar.add(menu2);
		this.setMenuBar(bar);
	}
	
	public GIndex() {
		this.setTitle("成績管理");
		p1 = new Panel();
		p1.setLayout(new BorderLayout());
		p2 = new Panel();
		p3 = new Panel();
		p4 = new Panel();
		menuCreate();
		p1.add(p2, BorderLayout.NORTH);
		lab = new Label("請輸入性別:");
		tf = new TextField(8);
		btn1 = new Button("開始查詢");
		btn1.addActionListener(this);
		p3.add(lab);
		p3.add(tf);
		p3.add(btn1);
		p1.add(p3, BorderLayout.SOUTH);
		this.add(p1, BorderLayout.NORTH);
		sm = new GradeManager();
		String sql = "select * from student where 0=?";
		String[] param = new String[] {"0"};
		sm.addGrade(sql, param);
		jtab = new JTable(sm);
		jtab.setRowHeight(30);
		jsp = new JScrollPane(jtab);
		this.add(jsp,BorderLayout.CENTER);
		addbtn = new Button("新增");
		upbtn = new Button("修改");
		delbtn = new Button("刪除");
		addbtn.addActionListener(this);
		upbtn.addActionListener(this);
		delbtn.addActionListener(this);
		p4.add(addbtn);
		p4.add(upbtn);
		p4.add(delbtn);
		this.add(p4, BorderLayout.SOUTH);
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension dim = tk.getScreenSize();
		this.setSize(1000, tk.getScreenSize().height-200);
		this.setLocationRelativeTo(null);
		this.addWindowListener(new WindowListener() {

			@Override
			public void windowClosing(WindowEvent w) {
				w.getWindow().setVisible(false);
				((Frame) w.getComponent()).dispose();
				System.exit(0);
				
			}
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==citem1) {
			CIndex cx = new CIndex();
			cx.setVisible(true);
			this.setVisible(false);
		}
		if(e.getSource()==sitem1) {
			SIndex sx = new SIndex();
			sx.setVisible(true);
			this.setVisible(false);
		}
		if(e.getSource()==btn1) {
			String jtf = tf.getText().trim();
			if(jtf.equals("")) {
				String sql = "select * from student where 1=?";
				String[] param = new String[] {"1"};
				sm = new GradeManager();
				sm.addGrade(sql, param);
				jtab.setModel(sm);
			}else {
				String sql = "select * from student where ssex=?";
				String[] param = new String[] {jtf};
				sm = new GradeManager();
				sm.addGrade(sql, param);
				jtab.setModel(sm);
			}
		}else if(e.getSource()==addbtn) {
			Gadd add = new Gadd(this,"新增成績資訊",true);
			sm = new GradeManager();
			String sql = "select * from student where 1=?";
			String[] param = new String[] {"1"};
			sm.addGrade(sql, param);
			jtab.setModel(sm);
		}else if(e.getSource()==upbtn){
			int rowNo = this.jtab.getSelectedRow();
			if(rowNo == -1) {
				jop.showMessageDialog(this,"請選擇修改項");
				return;
			}else{
			    Gupd add = new Gupd(this,"修改成績資訊",true,sm,rowNo);
			    sm = new GradeManager();
			    String sql = "select * from student where 1=?";
			    String[] param = new String[] {"1"};
			    sm.addGrade(sql, param);
			    jtab.setModel(sm);
			}
		}else if(e.getSource()==delbtn){
			int rowNo = this.jtab.getSelectedRow();
			if(rowNo == -1) {
				jop.showMessageDialog(this,"請選擇刪除項");
				return;
			}else{
				
			    String sql = "delete from student where sid=?";
			    String sid = (String)this.jtab.getValueAt(rowNo, 0);
			    String[] param = new String[] {sid};
			    StudentManager sm1=new StudentManager();
			    sm1.updateStudent(sql, param);
			    
			    String sql3 = "delete from student where gsid=?";
			    String[] param1 = new String[] {sid};
			    GradeManager gm=new GradeManager();
			    gm.updateGrade(sql3, param1);
			    sm = new GradeManager();
			    
			    String sql2 = "select * from student where 1=?";
			    String[] param2 = new String[] {"1"};
			    sm.addGrade(sql2, param2);
			    jtab.setModel(sm);
			}
		}	
	}

	public static void main(String[] args) {
		GIndex gIndex =new GIndex();
	}

}