Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在主类中使用Set/Get方法?_Java_Methods_Getter Setter - Fatal编程技术网

Java 如何在主类中使用Set/Get方法?

Java 如何在主类中使用Set/Get方法?,java,methods,getter-setter,Java,Methods,Getter Setter,我很难弄清楚该怎么处理它。例如,我在另一节课上有这样的内容: public class Methods { private String EmployeeName; private int SalaryDeduction; private int IDCard; public void setEmpName(String name) { this.EmployeeName = name; } public String get

我很难弄清楚该怎么处理它。例如,我在另一节课上有这样的内容:

public class Methods {

    private String EmployeeName;
    private int SalaryDeduction;
    private int IDCard;

    public void setEmpName(String name) {
        this.EmployeeName = name;
    }

    public String getEmpName() {
        return EmployeeName;
    }

    public void setSalaryDed(int sdvalue) {
        this.SalaryDeduction = sdvalue;
    }

    public int getSalaryDed() {
        return SalaryDeduction;
    }

    public void setIDCard(int idcard) {
        this.IDCard = idcard;
    }

    public int getIDCard() {
        return IDCard;
    }

}
我需要在下节课上使用上述方法。例如,
SetEmpName
方法获取my
EmployeeName
JTextField
的内容,然后通过
GetEmpName
返回,并单击按钮将其显示在textfield下方。它看起来像:

员工信息:詹姆斯

按钮点击

员工信息:詹姆斯

“你好,詹姆斯”

编辑

我已经知道我的固定方法是如何工作的了,谢谢你。到目前为止,我迫切需要了解如何通过单击按钮使get方法工作。这是我目前的节目

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

public class MainFrame extends JFrame implements ActionListener  
 {

   //UI Components Declaration


   JLabel EmpInfL = new JLabel("Employee Info:");
   JLabel SalDedL = new JLabel("Salary Deduction:");
   JLabel IDCL = new JLabel("ID Card");

   JTextField EmpInfTF = new JTextField(8);
   JTextField SalDedTF = new JTextField(9);
   JTextField IDCTF = new JTextField(8);

   JButton OkB = new JButton();
   JButton ClearB = new JButton();

   String EmployeeName
   int SalaryDeduction;
   int IDCard;

   //Constructor
   public MainFrame() {

   super("Programming Activity");
   setSize(300,100);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setVisible(true);
   setLayout(new FlowLayout());

   add(EmpInfL);
   add(EmpInfTF);


   add(SalDedL);
   add(SalDedTF);


   add(IDCL);
   add(IDCTF);


   OkB.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
   Methods SetGet = new Methods();

   SetGet.setEmpName(EmpInfTF.getText());


   }


      public static void main(String [] args)
         {

        new MainFrame();
         }
}

正如我看到的,您正在尝试使用
Swing
。如果您使用的是类似于
actionListener
s的内容,那么您可以从
JTextField.getText()
获取字符串并将其存储到
setEmpName

大概是这样的:

Methods yourObject=新方法()
actionListener方法中的yourObject.setEmpName(EmployeeName.getText())

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

public class MainFrame extends JFrame implements ActionListener  
 {

   //UI Components Declaration


   JLabel EmpInfL = new JLabel("Employee Info:");
   JLabel SalDedL = new JLabel("Salary Deduction:");
   JLabel IDCL = new JLabel("ID Card");
   JLabel sayHelloLabel = new JLabel("");
   JTextField EmpInfTF = new JTextField(8);
   JTextField SalDedTF = new JTextField(9);
   JTextField IDCTF = new JTextField(8);

   JButton OkB = new JButton();
   JButton ClearB = new JButton();

   String EmployeeName
   int SalaryDeduction;
   int IDCard;

   //Constructor
   public MainFrame() {

   super("Programming Activity");
   setSize(300,100);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setVisible(true);
   setLayout(new FlowLayout());

   add(EmpInfL);
   add(EmpInfTF);


   add(SalDedL);
   add(SalDedTF);


   add(IDCL);
   add(IDCTF);
   add(sayHelloLabel);

   OkB.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
   Methods SetGet = new Methods();

   SetGet.setEmpName(EmpInfTF.getText());
   sayHelloLabel.setText("Hello, "+SetGet.getEmpName());

   }


      public static void main(String [] args)
         {

        new MainFrame();
         }
}

请尊重命名惯例@邹祖这实际上似乎符合C#命名惯例。但是,是的,甚至Java的部分编译都是基于上面链接的JCC,所以从一开始就遵循JCC是一个非常好的主意。@Esko:不,不是。C#中的方法是PASCALCASE,变量通常是camelCased。谢谢你的留言。但另一个问题突然出现了。我似乎不能使用我的另一个类中的方法,因为它说“找不到符号”。我该怎么办?@JamesTorio它找不到哪个“符号”?.getText()部分:/@JamesTorio是
EmployeeName
类型为
JTextField
的对象?哦,糟糕。到目前为止,它已经奏效了。SetGet.setEmpName(EmpInfTF.getText());但是如何使用我的getEmpName?最后一个问题。