2错误java flops converter

时间:2013-11-01 11:04:52

标签: java converter

我有两个错误:

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) {
        }
    }
}

2 个答案:

答案 0 :(得分:0)

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

目前,没有声明textField1textField2,而是textArea1textArea2,但即使这些都在构造函数的本地范围内。

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));

答案 1 :(得分: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) {

        }
    }
}