访问方法类中的变量以在 GUI 类中使用

huangapple go评论76阅读模式
英文:

Accessing variable from method class to use in GUI class

问题

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

public class A implements ActionListener {
    JTextField tf1, tf2, tf3;
    JButton b1;

    A() {
        JFrame f = new JFrame();
        JLabel myLabel = new JLabel("输入第一个值");
        myLabel.setBounds(50, 50, 150, 20);
        tf1 = new JTextField();
        tf1.setBounds(50, 75, 250, 20);
        JLabel mysecondLabel = new JLabel("输入第二个值");
        mysecondLabel.setBounds(50, 125, 150, 20);
        tf2 = new JTextField();
        tf2.setBounds(50, 150, 250, 20);
        tf3 = new JTextField();
        tf3.setBounds(160, 225, 140, 20);
        tf3.setEditable(false);
        b1 = new JButton("求和");
        b1.setBounds(50, 225, 95, 20);
        b1.addActionListener(this);

        f.add(tf1);
        f.add(myLabel);
        f.add(tf2);
        f.add(mysecondLabel);
        f.add(tf3);
        f.add(b1);
        f.setSize(300, 300);
        f.setLayout(null);
        f.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        String s1 = tf1.getText();
        String s2 = tf2.getText();
        if (e.getSource() != b1) {
            return;
        }

        int x = Integer.parseInt(s1);
        int y = Integer.parseInt(s2);

        int result = B.myMethod(x, y);

        tf3.setText(Integer.toString(result));
    }

    public static void main(String[] args) {
        new A();
    }
}
import javax.swing.*;
import java.awt.event.*;

public class B {
    public static int myMethod(int x, int y) {
        int Z;
        Z = x + y;
        return Z;
    }
}
英文:

I am writing a code to add 2 numbers from two textfield and having the sum show up in a third textfield. Howerver, when I set the third textfield to display value Z, which is the sum, I get the error "cannot find symbol". What am I doing wrong?

import javax.swing.*;  
import java.awt.event.*;  
public class A implements ActionListener{     
    JTextField tf1,tf2,tf3;  
    JButton b1;   
    A(){  
        JFrame f= new JFrame(); 
        JLabel myLabel= new JLabel("Enter First Value");
        myLabel.setBounds(50,50,150,20); 
        tf1=new JTextField();  
        tf1.setBounds(50,75,250,20);
        JLabel mysecondLabel= new JLabel("Enter Second Value");
        mysecondLabel.setBounds(50,125,150,20);  
        tf2=new JTextField();  
        tf2.setBounds(50,150,250,20);  
        tf3=new JTextField();  
        tf3.setBounds(160,225,140,20);  
        tf3.setEditable(false);   
        b1=new JButton("Sum");  
        b1.setBounds(50,225,95,20);
        b1.addActionListener(this); 

        f.add(tf1);f.add(myLabel);f.add(tf2);f.add(mysecondLabel);f.add(tf3);f.add(b1);  
        f.setSize(300,300);  
        f.setLayout(null);  
        f.setVisible(true);  
    }         

        public void actionPerformed(ActionEvent e) {  
        String s1=tf1.getText();  
        String s2=tf2.getText();
    if(e.getSource()!=b1){  
        return;  
    }
    
    int x = Integer.parseInt(s1);
    int y = Integer.parseInt(s2);
            
    String result = B.Z;

    tf3.setText(result); 
}  public static void main(String[] args) {  
    new A(); 
}
}
import javax.swing.*;  
import java.awt.event.*;
public class B {
    public static int myMethod(int x, int y) {
      int Z;
      Z = x + y;
      return Z;
      }
}

答案1

得分: 0

你的 B 类没有一个名为 Z 的静态字段,无法通过调用 B.Z 来访问它。你没有调用 myMethod(x, y);。尝试使用 String result = String.valueOf(B.myMethod(x, y)); 看看是否起作用。

英文:

Your B class doesn't have a static field called Z that you can access by calling B.Z. You are not calling myMethod(x, y);. Try String result = String.valueOf(B.myMethod(x, y)); and see if that works.

huangapple
  • 本文由 发表于 2020年9月2日 22:24:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63707647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定