如何使用我的Set / Get方法到我的主类?

时间:2013-12-01 09:56:22

标签: 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 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方法获取EmployeeName JTextField的内容,然后通过GetEmpName返回,只需按一下按钮即可在文本字段下方显示该内容。它看起来像:

员工信息:詹姆斯

按钮点击

员工信息:詹姆斯

“你好詹姆斯”

修改

我已经知道我的Set方法是如何工作的,谢谢你。到目前为止,我迫切需要知道如何通过单击按钮来使我的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();
         }
}

1 个答案:

答案 0 :(得分:0)

我发现你正在尝试使用Swing。如果您使用actionListener s之类的内容,则可以从JTextField.getText()获取字符串并将其存储到setEmpName中:

类似的东西:

Methods yourObject = new Methods(); 你的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();
         }
}