有人可以帮我解决程序中的问题吗?

时间:2014-04-06 23:44:54

标签: java user-interface

我必须使用Gui组件编写计算器程序。我已经编写了程序,但我的CE按钮出现了问题。它给了我错误。我在for循环中也得到了错误,我将操作数标签添加到表达式中。我只是希望得到一些帮助来解决这些错误。谢谢!请忽略评论。他们在那里让我跟踪我必须做的事情。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator
{
JFrame window;
// stuff for top panel
JPanel topPanel;
JTextField expr,result;
JButton equals;

// stuff for bottom panel

JPanel bottomPanel,digitsPanel,opsPanel;
JButton[] digits,ops;
JButton clear, clearEntry;
Container content;
Listener listener;
String[] oplabels = { "+", "-", "/", "*" };

public Calculator()
{
    listener = new Listener(); // our Listener class implements ActionListener
    window= new JFrame("GUI Calc");
    content=window.getContentPane();
    content.setLayout( new GridLayout(2,1) );
    topPanel=new JPanel();
    topPanel.setLayout( new GridLayout(1,3) );

    // TOP PANEL WORK

    expr = new JTextField( );
    equals = new JButton("=");
    equals.addActionListener( listener );
    result = new JTextField( );

    topPanel.add( expr );
    topPanel.add( equals );
    topPanel.add( result );

    // BOTTOM PANEL WORK

    bottomPanel = new JPanel();
    bottomPanel.setLayout( new GridLayout(1,2) );

    digitsPanel = new JPanel();
    digitsPanel.setLayout( new GridLayout(4,3) );

    opsPanel = new JPanel();
    opsPanel.setLayout( new GridLayout(4,1) );

    digits  = new JButton[12];
    ops = new JButton[4];

    for (int i=0 ; i<10 ; i++)
    {
        digits[i] = new JButton( i+"" );
        digits[i].addActionListener(listener);
        digitsPanel.add( digits[i] );
    }

    clear = new JButton( "C" );
    clearEntry = new JButton( "CE" );
    clear.addActionListener(listener);
    clearEntry.addActionListener(listener);
    digitsPanel.add( clear );
    digitsPanel.add( clearEntry);


    for (int i=0 ; i<4 ; i++)
    {
        ops[i] = new JButton( oplabels[i] ) ;
        ops[i].addActionListener(listener);
        opsPanel.add( ops[i] );
    }

    bottomPanel.add( digitsPanel );
    bottomPanel.add( opsPanel );


    content.add( topPanel);
    content.add( bottomPanel);

    window.setVisible(true);
}
class Listener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        Component clicked = (Component) e.getSource();
        if ( clicked == equals )
        {
            result.setText( evaluate( expr.getText() ) );
            return;
        }
        for ( int i=0 ; i<10 ; i++)
        {
            if ( clicked == digits[i] )
            {
                expr.setText( expr.getText() + i );
                return;
            }
        }
        if ( clicked == clear )
        {
            expr.setText("0");
            return;
            // do something

        }
        for (int i=0; i < 10; i++)
        {
            if (clicked == digits[i])
            {
            int lastValue = i;
            return;
            }
        }
            if ( clicked == clearEntry )
            {
                expr.setText(expr.getText() - lastValue);
                return;
                // do something
            }

        for ( int i=0 ; i<4 ; i++ )
        {
            if(clicked == oplabels[i])
            {
            expr.setText(expr.getText + oplabels[i]);
            return;
            }
            // tack on that operator to the expr string
        }

    }
    String evaluate( String exp )
    {
        return "NOT WRITTEN YET";
    }
}
public static void main(String [] args)
{
    new Calculator();
}
}

2 个答案:

答案 0 :(得分:0)

第128行:clickedComponentoplabels[i]string - 您无法直接比较它们。

第130行:()来电时遗失getText()

答案 1 :(得分:0)

问题#1

编译器抱怨expr.setText(expr.getText() - lastValue);因为lastValue未定义。

仔细查看代码,lastValue被定义的唯一位置是在for-loop语句if返回`语句之前的...but the next issues the之后。 ..

for (int i = 0; i < 10; i++) {
    if (clicked == digits[i]) {
        int lastValue = i;
        return;
    }
}
if (clicked == clearEntry) {
    expr.setText(expr.getText() - lastValue);
    return;
    // do something
}

这不仅使lastValue变得无用,而且不可能达到if陈述......

问题#2

for (int i = 0; i < 4; i++) {
    if (clicked == oplabels[i]) {
        expr.setText(expr.getText + oplabels[i]);
        return;
    }
    // tack on that operator to the expr string
}

clicked定义为Component clicked = (Component) e.getSource();oplables定义为String[] oplabels = {"+", "-", "/", "*"};StringComponent不具有可比性(另外,您希望expr.getText()不是expr.getText)。

我想你想做更像......的事情。

if (clicked instanceof JButton) { 
    JButton btn = (JButton)clicked)
    for (int i = 0; i < oplabels.length; i++) {
        if (oplabels[i].equals(btn.getText()) {
            //...
        }
    }
}