使用在一个Class中声明的全局变量到另一个类的混淆

时间:2014-02-22 13:18:19

标签: java applet

我有两个不同的.java文件。

  1. form.java是一个获取用户详细信息的小程序
  2. EmbeddedNeo4j用于在Neo4j中创建节点
  3. 我想使用包含我从EmbeddedNeo4j中的form.java中检索的值的变量。

    我已经检索了这些值。我想知道如何在不同的java文件中使用它。

    我的form.java文件是这个

    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;
    public class form extends JApplet implements ActionListener
    
    {
      String Firstname,Lastname,Birthdate,Gender,State,City,HobbyS,EducationS,OccupationS;
      Object Hobby,Education,Occupation;
    // instance variables - replace the example below with your own
    String HobbyList[]={"Select ANY ONE","Cricket","Hockry","Golf","BasketBall","Football"};
    String EducationList[]={"Select ANY ONE","BSc","BCom","BE","B.Tech","MBA","MBBS","PhD"};
    String OccupationList[]={"Select ANY ONE","Business","Service","others"};
    JPanel panel=new JPanel(new SpringLayout());
    JLabel l1=new JLabel("First Name",JLabel.LEFT);
    JTextField t1=new JTextField(50);
    JLabel l2=new JLabel("Last Name",JLabel.LEFT);
    JTextField t2=new JTextField(50);
    JLabel l3=new JLabel("Birthday",JLabel.LEFT);
    JTextField t3=new JTextField(20);
    JRadioButton r1=new JRadioButton("Male");
    JRadioButton r2=new JRadioButton("Female");
    JLabel l4=new JLabel("State",JLabel.LEFT);
    JTextField t4=new JTextField(50);
    JLabel l5=new JLabel("City",JLabel.LEFT);
    JTextField t5=new JTextField(50);
    JLabel l6=new JLabel("Hobby",JLabel.LEFT);
    JComboBox jl1=new JComboBox(HobbyList);
    JLabel l7=new JLabel("Education",JLabel.LEFT);
    JComboBox jl2=new JComboBox(EducationList);
    JLabel l8=new JLabel("Occupation",JLabel.LEFT);
    JComboBox jl3=new JComboBox(OccupationList);
     JLabel l9=new JLabel("THANK YOU" ,JLabel.LEFT);
    JButton submit=new JButton("SUBMIT");
    
    
    public void init()
    {
        add(panel);
        panel.setLayout(new SpringLayout());
        panel.add(l1);
        panel.add(t1);
        panel.add(l2);
        panel.add(t2);
        panel.add(l3);
        panel.add(t3);
       // add(cpanel);
        panel.add(r1);
        panel.add(r2);       
        panel.add(l4);
        panel.add(t4);
        panel.add(l5);
        panel.add(t5);
        panel.add(l6);
        panel.add(jl1);
        panel.add(l7);
         panel.add(jl2);
        panel.add(l8);
    
        panel.add(jl3);
        panel.add(l9);
        panel.add(submit);
    
    
        /*panel.add(c1);
        panel.add(c2);
        panel.add(c3);
        panel.add(c4);*/
        /*panel.add(c2);*/
        Dimension d =new Dimension(20,20);
    
        l1.setLabelFor(t1);
        l2.setLabelFor(t2);
        t1.setPreferredSize(d);
        t1.setSize(d);
        t1.setMaximumSize(d);
       // t1.setMinimumSize(d);
        t2.setPreferredSize(d);
        t2.setSize(d);
        t2.setMaximumSize(d);
    
        t3.setPreferredSize(d);
        t3.setSize(d);
        t3.setMaximumSize(d);
    
        t4.setPreferredSize(d);
        t4.setSize(d);
        t4.setMaximumSize(d);
    
        t5.setPreferredSize(d);
        t5.setSize(d);
        t5.setMaximumSize(d);
    
        SpringUtilities.makeCompactGrid(panel,10,2,20,20,10,10);
        submit.addActionListener(this);
        // provide any initialisation necessary for your JApplet
    }
    
     public void actionPerformed(ActionEvent ae)
        {
            Firstname=t1.getText();
           Lastname=t2.getText();
           Birthdate=t3.getText();
           if(r1.isSelected())
           {Gender=r1.getText();
            }
            else
            Gender=r2.getText();
            State=t4.getText();
            City=t5.getText();
            Hobby=jl1.getSelectedItem();
            Education=jl2.getSelectedItem();
            Occupation=jl3.getSelectedItem();
            HobbyS=Hobby.toString();
           EducationS=Education.toString();
            OccupationS=Occupation.toString();
            repaint();
            //System.out.println(Firstname)
        }
    public void start()
    {
    
    }
    public void stop()
    {
    
    }
    public void paint(Graphics g)
    {
       g.setColor(Color.red);
    
        super.paint(g);
      g.drawString(Firstname,10,560);
         g.drawString(Lastname,10,570);
         g.drawString(Birthdate,10,580);
          g.drawString(Gender,10,590);
           g.drawString(State,10,600);
            g.drawString(City,10,610);
            g.drawString(HobbyS,10,620);
             g.drawString(EducationS,10,630);
              g.drawString(OccupationS,10,640);   
    }
    public void destroy()
    {
        // provide code to be run when JApplet is about to be destroyed.
    }
    
    
    }
    

    我想在EmbeddedNeo4j中使用String Firstname,Lastname,Birthdate,Gender,State,City,HobbyS,EducationS,OccupationS 有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您可能希望为要访问的每个字段创建一个公共getter方法:

public String getFirstname() {
    return Firstname;
}

public String getLastname() {
    return Lastname;
}

我还建议您将字段命名为小写,即:firstname, lastname ...

如同@chrylis所说,将所有这些相关字段封装在一个类中也很棒。

答案 1 :(得分:0)

在Java中,如果要复制所有数据,只需创建一个Person bean类,就没有全局变量的概念。例如:

public class Person{

private String fname;
private String mname;
private String lname;
public String getfname() {
    return fname;
}

public String setfname() {
    return fname;
}
//Other getters()&setters();
}

然后您可以使用getter和setter在表单类中定义 Pesron 类。

如果你想使用类似全局变量的java类,可以在另一个类中将 Person类定义为静态变量,如下所示

Public class Globals{
Public static Person FakeGlobalvariablePerson=null;
}

通过调用Globals.FakeGlobalvariablePerson可以像全局变量一样使用它,但要记住它不是编码的好方法