Student Record

By AtomTheMan on Feb 28, 2013

Simple Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class StudentRecord extends JFrame implements ActionListener{
    private JLabel lbl1 = new JLabel("Full Name: ");
    private JTextField txt1 = new JTextField();
    private JLabel lbl2 = new JLabel("Address: ");
    private JTextField txt2 = new JTextField();
    private JLabel lbl3 = new JLabel("Contact Number: ");
    private JTextField txt3 = new JTextField();
    private JButton btn1 = new JButton("Save Student Record");
    private JButton btn2 = new JButton("Clear All Fields");

public StudentRecord(){
    JFrame f = new JFrame("Student Record by Atom");
    f.setBounds(500,300,300,320);
    f.setLayout(new GridLayout(4,1));
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();
    p.setLayout(new GridLayout(1,1));
    p.add(lbl1);
    p.add(txt1);

    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(1,1));
    p2.add(lbl2);
    p2.add(txt2);

    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(1,1));
    p3.add(lbl3);
    p3.add(txt3);

    JPanel p4 = new JPanel();
    p4.setLayout(new GridLayout(1,1));
    p4.add(btn1);
    p4.add(btn2);
    btn1.addActionListener(this);
    btn2.addActionListener(this);

        f.add(p);
        f.add(p2);
        f.add(p3);
        f.add(p4);
}
public void AddRecord(){
    try{
        FileWriter fw = new FileWriter("StudentRecords.txt");
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write("Full Name: "+ txt1.getText());
        bw.newLine();
        bw.write("Address: "+ txt2.getText());
        bw.newLine();
        bw.write("Contact Number: "+ txt3.getText());
        bw.newLine();
        bw.close();

        JOptionPane.showMessageDialog(null,"New Student Record Inserted:"+ txt1.getText());

    }catch(Exception e){

    }
}
public void actionPerformed(ActionEvent e){
    Object src = e.getSource();
    String t1 = txt1.getText();
    String t2 = txt2.getText();
    String t3 = txt3.getText();

    if(src == btn1){
        if(t1.trim().isEmpty() || t2.trim().isEmpty() || t3.trim().isEmpty()){
            JOptionPane.showMessageDialog(null,"Cannot display with incomplete data. Please Try again!");
        }else{
            AddRecord();
        }
    }else if (src == btn2){
        txt1.setText("");
        txt2.setText("");
        txt3.setText("");
    }
}
public static void main(String[] args){
    StudentRecord sr = new StudentRecord();
}
}

Comments

Sign in to comment.
Sorasyn   -  Feb 28, 2013

What is it supposed to do? I see some GUI components, but nothing to clue me in. Also, I find it to be better to instantiate JLabels (since they provide no real functionality) within the add statement. Unless you plan on adding events to it, it's quicker this way, and saves you a bit of memory. Nice seeing some Java.

AtomTheMan  -  Mar 01, 2013

Just a Simple Code... it is our activity last day..

Hawkee  -  Mar 01, 2013

Would be nice to add some descriptive text.

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.