我正在为java class
编写一个计算器程序。我想说明像
用户决定除以0.我希望计算器能够确认此错误并显示错误。
用户输入6 + 7 +然后点击相等按钮。
我想再一次让计算器确认存在错误。我希望我能得到一些关于如何解决这个问题的提示。谢谢!
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
}
/* if (clicked == clearEntry)
{
expr.setText(expr.getDigits[i]);
}
for (int i=0; i < 10; i++)
{
if (clicked == digits[i])
{
int lastValue = clicked;
if(clicked == clearEntry)
{
expr.setText(expr.getText()-clicked);
return;
}
}
} */
for ( int i=0 ; i<4 ; i++ )
{
if(clicked == digits[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();
}
}
我还在努力。特别是在我试图找出clearEntry按钮的地方,所以我把那个部分作为注释,因为它仍然在进行中。
答案 0 :(得分:0)
你可以试试这个:
添加一个新类(计算器)成员:
Component last_clicked;
Inside Constructor:
last_clicked=null;
在每个return action语句执行之前,你可以添加:
last_clicked=clicked;
现在,在actionPerformed方法中,当检查clicked的值时,您可以这样做: