如何从一个班级捕获数据并将其传递给所有班级

时间:2018-08-30 18:00:34

标签: java

我有一个多类程序,每个类都有自己的GUI,我的任务是从名为dataInput的类中捕获详细信息,并且必须将数据捕获到名为StudentRecords的类的变量中,我必须捕获包含的约10条记录的名称,姓氏,学生编号和1-4的测试,我有一个称为RecordsMenu的类,该类具有可调用不同类的Button,例如,当我单击dataInput按钮时,它将调用DataInput类,在datainput类中,我必须捕获数据进入记录类,然后我必须从那里返回recordsMenu屏幕!例如,当我单击显示按钮Button时,我必须能够看到捕获到的数据,但是我遇到的问题是,当我回到主屏幕时,捕获到的所有数据都会丢失,并且在打印时显示为空我测试它是否被捕获,我将不胜感激,我的代码在下面。这必须在不使用文件的情况下完成,谢谢     [这是我的应用程序主屏幕的图片] [1]

    enter code here

  [1]: https://i.stack.imgur.com/I6JyI.jpg
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


import javax.swing.*;

public class InputDetails extends JPanel
{
    JLabel title,name,surname,studentno,tests,t1,t2,t3,t4;
    JTextField nametxf,surnametxf,studentnotxf,t1txf,t2txf,t3txf,t4txf;
    JButton submit,home;
    JPanel buttons, mainPanel, labels;

    public InputDetails()
    {
           // title
           title = new JLabel("Records Menu",SwingConstants.CENTER); 
           title.setForeground(Color.BLUE);
           Font newLabelFont =new Font("Serif",Font.BOLD,70);
           title.setFont(newLabelFont); 
           // buttons and actions
           submit = new JButton("Submit");
           submit.addActionListener(new Handler());
           home = new JButton("Home");
           home.addActionListener(new Handler());
           //labels
           name = new JLabel("Name");
           surname = new JLabel("Surname");
           studentno= new JLabel("Student Number");
           tests = new JLabel("Tests");
           // tests
           t1 = new JLabel("Test 1");
           t2 = new JLabel("Test 2");
           t3 = new JLabel("Test 3");
           t4 = new JLabel("Test 4");

           //textfields
           nametxf = new JTextField(10);
           surnametxf = new JTextField(10);
           studentnotxf = new JTextField(10);
           t1txf= new JTextField(10);
           t2txf= new JTextField(10);
           t3txf= new JTextField(10);
           t4txf= new JTextField(10);

            buttons = new JPanel(new GridLayout(1,2,5,5));
            buttons.add(submit);
            buttons.add(home);
        // fields
            labels = new JPanel(new GridLayout(7,1,5,5));  
            labels.add(name);
            labels.add(nametxf);
            labels.add(surname);
            labels.add(surnametxf);
            labels.add(studentno);
            labels.add(studentnotxf);
            labels.add(t1);
            labels.add(t1txf);
            labels.add(t2);
            labels.add(t2txf);
            labels.add(t3);
            labels.add(t3txf);
            labels.add(t4);
            labels.add(t4txf);
            // 
            mainPanel = new JPanel(new BorderLayout(5,5));
            mainPanel.add(title,BorderLayout.NORTH);
            mainPanel.add(labels,BorderLayout.WEST);
            mainPanel.add( buttons,BorderLayout.SOUTH);
            add(mainPanel);


    }
    class Handler implements ActionListener
    {

        public void actionPerformed(ActionEvent click)
        {

            if(click.getSource()==home)
            {

                // back to main view    
                mainPanel.setVisible(false);
                add(new RecordsMenu());
            }
            else if(click.getSource()==submit)
            {

                String name,surname,studentno;
                int test1,test2,test3,test4;

                name = nametxf.getText(); 
                surname = surnametxf.getText();
                studentno=studentnotxf.getText();
                test1 =Integer.parseInt(t1txf.getText());
                test2 = Integer.parseInt(t2txf.getText());
                test3 = Integer.parseInt(t3txf.getText());
                test4 = Integer.parseInt(t4txf.getText());
                StudentRecords records = new StudentRecords(name,surname,studentno,test1,test2,test3,test4);






            }


        }





    }




}
import java.io.*;

import javax.swing.JOptionPane;
public class StudentRecords {

     String name , Surname, studentno;
     int test1,test2,test3,test4;
     FileWriter records;
     PrintWriter out;


      public StudentRecords(String Name, String Surname,String StudentNo,int t1, int t2, int t3, int t4)
      {

          setName(Name);  
          setSurname(Surname);
          setStudentno(StudentNo);
          setTest1(t1);
          setTest2(t2);
          setTest3(t3);
          setTest4(t4);

        //  saveRecords();  


      }




     public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return Surname;
    }
    public void setSurname(String surname) {
        Surname = surname;
    }
    public String getStudentno() {
        return studentno;
    }
    public void setStudentno(String studentno) {
        this.studentno = studentno;
    }
    public int getTest1() {
        return test1;
    }
    public void setTest1(int test1) {
        this.test1 = test1;
    }
    public int getTest2() {
        return test2;
    }
    public void setTest2(int test2) {
        this.test2 = test2;
    }
    public int getTest3() {
        return test3;
    }
    public void setTest3(int test3) {
        this.test3 = test3;
    }
    public int getTest4() {
        return test4;
    }
    public void setTest4(int test4) {
        this.test4 = test4;
    }










}
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.*;

public class ReadFile extends JPanel{

private JButton read,back;  
private JTextArea desplay;
private JLabel title;
private JPanel mainpanel, buttons;
String record,student1,student2,student3,student4,student5,student6,student7,student8,student9,student10;
public  ReadFile()
{

    // title
 title = new JLabel("Read File Contents",SwingConstants.CENTER);
 Font newLabelFont =new Font("Serif",Font.BOLD,30);
 title.setFont(newLabelFont); 

 // textarea


  desplay = new JTextArea();


 // buttons
  read = new JButton("Open");
  read.addActionListener(new Handler());
  back = new JButton("back to main");
  back.addActionListener(new Handler());
 // panel
  buttons = new JPanel(new GridLayout(1,2,5,5));
  buttons.add(read);
  buttons.add(back);

   mainpanel = new JPanel(new BorderLayout(5,5));
   mainpanel.add(title,BorderLayout.NORTH);
   mainpanel.add(desplay,BorderLayout.CENTER);
   mainpanel.add(buttons,BorderLayout.SOUTH);



    add(mainpanel);


}
class Handler implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent click) {


        if(click.getSource()==read)
        {

            JFileChooser chooser = new JFileChooser();
            Scanner input = null;
            if(chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
            {


            File selectedFile = chooser.getSelectedFile();
            try {   
            input = new Scanner(selectedFile);  
            while(input.hasNextLine())
            {
                record = input.nextLine();
                desplay.append(record);
                desplay.append("\n");


            }



            }catch(IOException fileerror) {

                JOptionPane.showMessageDialog(null, "you have an error");


            }   



            }





        }
        else if(click.getSource()==back)
        {

      mainpanel.setVisible(false);
        add(new RecordsMenu()); 
        }




    }




}




}
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Search extends JPanel
{
    DisplayDetails d = new DisplayDetails();
    JLabel title,stdn;
    JTextField studentno;
    JPanel mainp,details,buttons;
    JButton search,home;
    Scanner input;
    File records;
    String key;
    String line;
    boolean wordfound = false;
    public Search()
    {

        // title
         title = new JLabel("Search a Student",SwingConstants.CENTER);
         Font newLabelFont =new Font("Serif",Font.BOLD,30);
         title.setFont(newLabelFont); 
         stdn = new JLabel("Student Number");
         studentno = new JTextField(10);
    //   
         search = new JButton("Search");
         search.addActionListener(new Handler());
         home = new JButton("Home");
         mainp = new JPanel(new BorderLayout());
         details = new JPanel(new GridLayout(1,2));
         details.add(stdn);
         details.add(studentno);

         buttons = new JPanel(new GridLayout(1,2));
         home.addActionListener(new Handler());
         buttons.add(search);
         buttons.add(home);

         mainp.add(title,BorderLayout.NORTH);
         mainp.add(details,BorderLayout.WEST);
         mainp.add(buttons,BorderLayout.SOUTH);
         add(mainp);
    }
    class Handler implements ActionListener
    {


        public void actionPerformed(ActionEvent button)
        {
            if(button.getSource()==search)
            {   
                // define the key
               key = studentno.getText();   
                if(key.matches("^2\\d{8}"))
                {
                     File file =new File("StudentRecords.txt");
                     Scanner in;
                     try {

                         in = new Scanner(file);

                         while(in.hasNextLine())
                         {
                            line=in.nextLine();


                            if((line.contains(key))) {
                                wordfound=true;
                                  break;



                                  }





                         }
                           if((wordfound)) {
                              System.out.println(line); 
                              mainp.setVisible(false);
                              add(d);
                              d.display.setText(line);
                              JOptionPane.showMessageDialog(null,"found");  

                              }
                           else 
                           {

                               JOptionPane.showMessageDialog(null,"not found"); 



                           }


                         in.close();
                     } catch(IOException e) {

                     }







                }   
                else if(!(key.matches("^2\\d{8}")))
                {

                JOptionPane.showMessageDialog(null,"incorrect student number"); 


                }










        }
            else if(button.getSource()==home)
            {


                mainp.setVisible(false);
                add(new RecordsMenu());

            }

        }
    }
    // methods

       public void RecordFOund()
        {



        }
       public void notFound()
       {




       }



            }




import javax.swing.*;
import java.awt.*;

public class Frame extends JFrame
{
    public  Frame()
    {

       setSize(500,400);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setLocationRelativeTo(null);
        add(new RecordsMenu());
      // add(new Search());
       setVisible(true);





    }

}

1 个答案:

答案 0 :(得分:0)

尝试一下 在StudentRecords类的getter方法中,使它们{返回this.variable;}。 例如在public int getTest1(){return this.test1;}