为什么我会收到错误:不兼容的类型; JButton无法转换为JTextfield

时间:2014-03-10 22:10:31

标签: java swing jbutton jtextfield

所以我收到了这个错误,我很困惑。我甚至都不想这样做。这就是我所拥有的:

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


    public class Answerfinder extends JFrame {
    private static final int WIDTH = 400;
    private static final int HEIGHTH = 300;

    private JLabel Ial, Ratelabel,earn1, earn2, earn3, earn4, earn5, earn10;
    private JLabel equalabel;
    private JTextField ratein,initialamin, earn1out, earn2out, earn3out, earn4out, earn5out,   earn10out;
    private JButton calculatebut;
    private CalculateButtonHandler cbhandler;

    public Answerfinder() {

        // adding the labels
        Ial = new JLabel("Enter your initial amount:",SwingConstants.RIGHT);
        Ratelabel = new JLabel("Enter growth percentage (just numbers):",SwingConstants.RIGHT);
        earn1 = new JLabel("Total by year 1:",SwingConstants.RIGHT);
        earn2 = new JLabel("Total by year 2:",SwingConstants.RIGHT);
        earn3 = new JLabel("Total by year 3:",SwingConstants.RIGHT);
        earn4 = new JLabel("Total by year 4:",SwingConstants.RIGHT);
        earn5 = new JLabel("Total by year 5:",SwingConstants.RIGHT);
        earn10 = new JLabel("Total by year 10:",SwingConstants.RIGHT);
        equalabel = new JLabel("Press this to calculate: ");

        //adding the textfields
        ratein = new JTextField();
        initialamin = new JTextField();
        earn1out = 

        //adding button
        calculatebut = new JButton("Calculate");
        cbhandler = new CalculateButtonHandler();
        calculatebut.addActionListener(cbhandler);

        // Sets title of program
        setTitle("Exponential Growth Calculator");
       // Sets size of frame, whether or not it's visible, and what do on close
        setSize(WIDTH, HEIGHTH);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // establishes container and sets the layout
        Container pane = getContentPane();
        pane.setLayout(new GridLayout (8,2));

        pane.add(Ial);
        pane.add(initialamin);
        pane.add(Ratelabel);
        pane.add(ratein);
        pane.add(equalabel);
        pane.add(calculatebut);
        pane.add(earn1);
    }

    private class CalculateButtonHandler implements ActionListener {


        @Override
        public void actionPerformed(ActionEvent e) {

            double rate, initamount, returns1, returns2, returns3, returns4, returns5, returns10;

            rate = Double.parseDouble(ratein.getText());
            if (rate > 1) {
                rate = (rate / 100) + 1;
            } else {
                rate = rate +1;
            }
            initamount = Double.parseDouble(initialamin.getText());
            returns1 = initamount * Math.pow(rate, 1);
            returns2 = initamount * Math.pow(rate, 2);
            returns3 = initamount * Math.pow(rate, 3);
            returns4 = initamount * Math.pow(rate, 4);
            returns5 = initamount * Math.pow(rate, 5);
            returns10 = initamount * Math.pow(rate, 10);
        }
    }

}

我正在制作一个程序,可以找到用户输入的数字的指数增长。这是代数课程的额外学分项目。请忽略丑陋丑陋的代码,我是刚开始的高中生。

3 个答案:

答案 0 :(得分:2)

你似乎有一些半写的代码:

initialamin = new JTextField();
earn1out =

//adding button
calculatebut = new JButton("Calculate");

您应完成earn1out =行,或将其删除。

答案 1 :(得分:2)

您错过了作业的右侧值(加上分号终止)

earn1out =

导致变量earn1out被分配给a JButton。它应该是

earn1out = new JTextField(); 

答案 2 :(得分:2)

我认为你的代码中有这条未完成的行:

earn1out = 

这会混入下一个语句,即JButton初始化语句。

//adding button
calculatebut = new JButton("Calculate");