Home > Mobile >  I tried making my 2 buttons first one to save and second one to show all , but I couldn't make
I tried making my 2 buttons first one to save and second one to show all , but I couldn't make

Time:01-28

I am trying to make a simple java program that saves data for 20 students which is the first name, last name, which term, and email and make 2 buttons to save data after filling it and then another button to print all data.

My main problem to how to figure out using my action listener.

First I made student class:

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class student {
    static Scanner scr = new Scanner(System.in);
    String [] [] student = new String[20][5];
    private String fornamn;
    private String efternamn;
    private String termin;
    private String username;
    private String epost;

    Calendar calendarObj = GregorianCalendar.getInstance();
    int month = calendarObj.get(Calendar.MONTH);
    int year = calendarObj.get(Calendar.YEAR);

    public student(String fornamn, String efternamn){ //constructor takes f,e
        this.fornamn = fornamn;
        this.efternamn = efternamn;

    }

    public student(){ // default constructor

    }


    public void setFornamn(String fornamn ) {
        this.fornamn = fornamn;
    }
    public void setEfternamn (String efternamn ) {
        this.efternamn= efternamn;
    }

    public String getFornamn(){
        return fornamn;
    }
    public String getEfternamn(){
        return efternamn;
    }

    public String getTermin(){
        return termin;
    }

    public void setTermin(String termin) {
        this.termin = termin;
    }

    public String getUsername(){
        return username;
    }
    public void setUsername(String username) {
        this.username = username ;
    }

    public void setEpost(String epost) {
        this.epost = epost;
    }
    public String getEpost() { return epost;}

    int i = 0;
    public String skapaTermin() {
        if (month < 6){
            termin = "ht";
        } else {
            termin = "vt";
        }
        return termin;
    }

    public String skapaUser() {
        username = termin   fornamn.substring(0,3)   efternamn.substring(0,3);
        return skapaUser();
    }


    public String skapaEpost(){
        epost = username   "@du.se";
        return skapaEpost();
    }

}

second I made this GUI:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class mysubclass implements ActionListener{
    static student obj = new student ();    
    JTextField tfFornamn; 
    JLabel lblFornamn;   
    JTextField tfEfternamn; 
    JLabel lblEfternamn; 
    JTextField tfTermin; 
    JLabel lblTermin; 
    JTextField tfUsername; 
    JLabel lblUsername; 
    JTextField tfEpost; 
    JLabel lblEpost; 
    JButton spara ;
    JTextField btnSpara ;
    JButton visaalia ;
    mysubclass() {
        JFrame frame = new JFrame("student form");
        JPanel panel = new JPanel();
        frame.setSize(400,350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        panel.setLayout(null);

        JLabel lblFornamn = new JLabel("Förnamn :"); //First name
        lblFornamn.setBounds(10,10,80,35);
        panel.add(lblFornamn);

        JTextField tfFornamn = new JTextField(25);  //textfield for first name
        tfFornamn.setBounds(120,10,600,30);
        panel.add(tfFornamn);

        JLabel efternamn = new JLabel("Efternamn :"); //last name 
        efternamn.setBounds(10,50,90,35);
        panel.add(efternamn);

        JTextField tfEfternamn = new JTextField(25); // textfield for last name
        tfEfternamn.setBounds(120,50,600,30);
        panel.add(tfEfternamn);

        JLabel lblTermin = new JLabel("Termin  :"); //termin
        lblTermin.setBounds(10,90,90,35);
        panel.add(lblTermin);

        JTextField tfTermin = new JTextField(25);   //textfield termin
        tfTermin.setBounds(120,90,600,30);
        panel.add(tfTermin); 

        JLabel lblUsername = new JLabel("Username :"); // username
        lblUsername.setBounds(10,130,90,35);
        panel.add(lblUsername);

        JTextField tfUsername = new JTextField(25);   //textfield username
        tfUsername.setBounds(120,130,600,30);
        panel.add(tfUsername); 

        JLabel lblEpost = new JLabel("Epost :"); //email
        lblEpost.setBounds(10,170,90,35);
        panel.add(lblEpost);

        JTextField tfEpost = new JTextField(25);   //textfield email
        tfEpost.setBounds(120,170,600,30);
        panel.add(tfEpost); 

        JButton spara = new JButton("Save");
        spara.setBounds(10,210,120,30);
        panel.add(spara);
        spara.addActionListener(this);

        JTextField btnSpara = new JTextField(25);   //textfield save button
        btnSpara.setBounds(150,210,600,130);
        panel.add(btnSpara); 

        JButton visaalia  = new JButton("show all");
        visaalia.setBounds(10,280,120,30);
        panel.add(visaalia);
        visaalia.addActionListener(this);

        frame.setResizable(true); 
        frame.setVisible(true);



    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == spara) {
            obj.setFornamn(tfFornamn.getText()); //encapsulation
            obj.setEfternamn(tfEfternamn.getText());
            obj.setTermin(tfTermin.getText());
            obj.setUsername(tfUsername.getText());
            obj.setEpost(tfEpost.getText());
            for(int i = 0 ; i <= obj.student.length;i  ) {
                obj.student [i][0] = obj.getFornamn();
                obj.student [i][1] = obj.getEfternamn();
                obj.student [i][2] = obj.getTermin();
                obj.student [i][3] = obj.getUsername();
                obj.student [i][4] = obj.getEpost();
            }
        }
        if(e.getSource() == visaalia) {
            for(int i = 0 ; i <= obj.student.length;i  ) {
                System.out.println("hi");
            }
        }


    }
}

Then I just called it:

public class Main {
    public static void main(String[] args) {
        new mysubclass ();
    }

}

CodePudding user response:

The first problem is, you're shadowing your variables.

That is, you've declared spara and visaalia as instance fields of your class, but then re-declared them as local variables in your constructor...

class mysubclass implements ActionListener {

    //...
    JButton spara;
    JButton visaalia;
    //...

    mysubclass() {
        //...
        // Re-declared here as local variable
        JButton spara = new JButton("Save");
        //...
        // Re-declared here as local variable
        JButton visaalia = new JButton("show all");
        //...

This means when actionPerformed is called, spara and visaalia are both null

And now, the elephant(s) in the room

You don't need static Scanner scr = new Scanner(System.in);, get rid of it, when using a GUI, this kind of statement just scares people

You shouldn't be using Calendar. It's effectively deprecated and has been replaced with the java.time.* APIs, time to move forward and learn how it should be done.

You really need to take the time to learn how to use layout managers. null layouts are just going to explode in your face and when I run your code, the components don't fit within in the window, rendering your interface useless.

You really need to take the time to look at:

CodePudding user response:

The problem is that you are not checking if there is already a username with the same term. For example, if the first line in your array is "nt01, Tobi, Gröndahl, nt01, [email protected]", when you press "Save" the program will try to save another student with username "nt01". But as you may have realized, there is already a student with username "nt01" in the array. The reason for this is that you are using the same variable for storing username and termin. The simplest solution I can think of would be to change one of the variable names. You could use "user_name" instead of "username". Or, you could make another variable that stores all usernames and check if there is already an entry with this username. If there is, don't save the new entry. In order to print all students, you have to create a separate function that loops through the whole array and prints each student. The problem is that you already use "student_array" as a variable for storing the student array, and you want to use it for printing out all students. So you have to rename the variable.

  •  Tags:  
  • Related