Sie sind auf Seite 1von 6

Kevin Eric E.

Dayo BSCS CS 334

Prof. J. C. Esperanza 09/03/13 Tues. 4:00 6:00 P.M.

package cs334; import import import import import import import import import import import import import java.awt.Container; java.awt.GridLayout; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.io.FileNotFoundException; java.io.FileReader; java.util.Scanner; javax.swing.JButton; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JOptionPane; javax.swing.JTextField; javax.swing.SwingConstants;

public class GradeForm extends JFrame { static Scanner inputF; static int j=0; static Student[] gs = new Student[4]; //instance variables JLabel studnoL,nameL, g1L, g2L, gpaL; JTextField studnoTF, nameTF, g1TF, g2TF, gpaTF; JButton nextB, prevB, exitB; NextButtonHandler nb; PrevButtonHandler pb; ExitButtonHandler eb; // define your constructor public GradeForm() { studnoL = new JLabel("Student No: ",SwingConstants.RIGHT); nameL = new JLabel("Name ",SwingConstants.RIGHT); g1L= new JLabel("CS 132",SwingConstants.RIGHT); g2L = new JLabel("CS 131 ",SwingConstants.RIGHT); gpaL = new JLabel("GPA ",SwingConstants.RIGHT);

//text fields studnoTF = new JTextField(3); nameTF = new JTextField(15); g1TF = new JTextField(4); g2TF = new JTextField(4); gpaTF = new JTextField(4);

//buttons nextB = new JButton("Next"); nb = new NextButtonHandler(); nextB.addActionListener((ActionListener) nb); prevB = new JButton("Previous"); pb = new PrevButtonHandler(); prevB.addActionListener((ActionListener) pb); exitB = new JButton("Exit"); eb = new ExitButtonHandler(); exitB.addActionListener((ActionListener) eb);

Container pane = getContentPane(); pane.setLayout(new GridLayout(7,2)); pane.add(studnoL); pane.add(studnoTF); pane.add(nameL); pane.add(nameTF); pane.add(g1L); pane.add(g1TF); pane.add(g2L); pane.add(g2TF); pane.add(gpaL); pane.add(gpaTF); pane.add(nextB); pane.add(prevB); pane.add(exitB); setTitle("Grade"); setVisible(true); setSize(400,300); } public { void loadDataFile() throws FileNotFoundException

inputF=new Scanner(new FileReader("C:\\Users/Kevin/Desktop/eclipse/CS112L/GUI/src/cs334/Student File")); int ctr = 0; while(inputF.hasNext()) { gs[ctr]=new Student(inputF.nextInt(),inputF.next(), inputF.nextDouble(), inputF.nextDouble()); ctr++;

} } public void displayRecord() { studnoTF.setText(gs[j].getStudNo()+""); nameTF.setText(gs[j].getName()); g1TF.setText(gs[j].getG1()+""); g2TF.setText(gs[j].getG2()+""); gpaTF.setText(gs[j].computeGpa()+""); } private class NextButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { j++; if(j>3) { JOptionPane.showMessageDialog(null, "BottomMost Data"); } else displayRecord(); } } private class PrevButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { j--; if(j<0) { JOptionPane.showMessageDialog(null, "TopMost Data"); } else displayRecord(); } }

private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String[] args) { GradeForm gf = new GradeForm();

try { gf.loadDataFile(); gf.displayRecord(); } catch(FileNotFoundException e ) { JOptionPane.showMessageDialog(null,"File Not Found"); } } }

package cs334;

public class Student { private Integer studno; private String name; private Double g1; private Double g2;

public Student(int studno, String n, Double g1, Double g2) { this.studno=studno; name = n; this.g1= g1; this.g2 = g2; } public int getStudNo() { return studno; } public String getName() { return name; } public Double getG1() { return g1; } public Double getG2() {

return g2; } public Double computeGpa() { return (g1+g2)/2.0; } }

Student File
123 345 678 901 JohnDC 2.5 1.5 JaneAyres 1.0 1.5 MarlonGamboa 3.5 4.0 OliverCruz 1.0 1.0

Sample Output

Das könnte Ihnen auch gefallen