0

我有两个错误:

C:\Users\anderson\Documents\FlopstoGFlopsConverter1.java:80: error: cannot find symbol
       long flops = Long.parseLong(this.textField1.getText());
                                       ^
  symbol: variable textField1
C:\Users\anderson\Documents\FlopstoGFlopsConverter1.java:85: error: cannot find symbol
       this.textField2.setText(String.valueOf(gFlops));
           ^
  symbol: variable textField2
2 errors

学习太难了。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;



public class FlopstoGFlopsConverter1 extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;

    FlopstoGFlopsConverter1() {
        setSize(500, 350);
        setVisible(true);

        JButton button1 = new JButton("Convert!");
        button1.addActionListener(this);
        JPanel panel = new JPanel();
        panel.add(button1);
        panel.setLayout(null);
        this.add(panel);
        button1.setBounds(190, 230, 100, 30);


        JLabel label1 = new JLabel("Enter Flops");
        panel.add(label1);
        this.add(panel);
        label1.setBounds(89, 52, 150, 50);

        JTextField textArea1 = new JTextField(20);
        JPanel p = new JPanel();
        panel.add(textArea1);

        this.add(panel);
        textArea1.setBounds(160, 69, 160, 20);


        JTextField textArea2 = new JTextField(20);
        panel.add(textArea2);
        this.add(panel);
        textArea2.setBounds(159, 155, 160, 20);


        JLabel label2 = new JLabel("Gigaflops ");
        panel.add(label2);
        this.add(panel);
        label2.setBounds(91, 150, 200, 30);



    }


    public static  void main(String[]args) {
        new  FlopstoGFlopsConverter1();
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("");

        try {
            long flops = Long.parseLong(this.textField1.getText());

            double gFlops = flops/1000000000;


            this.textField2.setText(String.valueOf(gFlops));
        } catch(Exception exception) {
        }
    }
}
4

2 回答 2

0

您需要将textArea1and声明textArea2为实例变量并在actionPerformed()方法中使用它们。

目前,没有声明textField1and ,而是声明and ,但即使这些也在构造函数的本地范围内。textField2textArea1textArea2

JTextField textArea1 = null; // Outside the constructor, inside the class
JTextField textArea2 = null; // Outside the constructor, inside the class
...
...
// Inside actionPerformed method
long flops = Long.parseLong(this.textArea1.getText()); // use textArea1
...
this.textArea2.setText(String.valueOf(gFlops));
于 2013-11-01T11:08:07.270 回答
0

正如它所说,您没有任何 textField1 和 textField2 声明。先声明它们。如果您打算使用 textArea1 和 textArea2,请在类声明之后将它们声明为实例变量,以便您可以使用它们。您的课程将如下所示:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class FlopstoGFlopsConverter1 extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JTextField textArea1;
    private JTextField textArea2;

    FlopstoGFlopsConverter1() {

        setSize(500, 350);
        setVisible(true);

        JButton button1 = new JButton("Convert!");
        button1.addActionListener(this);
        JPanel panel = new JPanel();
        panel.add(button1);
        panel.setLayout(null);
        this.add(panel);
        button1.setBounds(190, 230, 100, 30);


        JLabel label1 = new JLabel("Enter Flops");
        panel.add(label1);
        this.add(panel);
        label1.setBounds(89, 52, 150, 50);

        textArea1 = new JTextField(20);
        JPanel p = new JPanel();
        panel.add(textArea1);

        this.add(panel);
        textArea1.setBounds(160, 69, 160, 20);



        textArea2 = new JTextField(20);
        panel.add(textArea2);
        this.add(panel);
        textArea2.setBounds(159, 155, 160, 20);


        JLabel label2 = new JLabel("Gigaflops ");
        panel.add(label2);
        this.add(panel);
        label2.setBounds(91, 150, 200, 30);
    }


    public static  void main(String[]args) {
        new  FlopstoGFlopsConverter1();
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("");
        try {
            long flops = Long.parseLong(this.textArea1.getText());

            double gFlops = flops/1000000000;


            this.textArea2.setText(String.valueOf(gFlops));
        } catch(Exception exception) {

        }
    }
}
于 2013-11-01T11:08:16.840 回答