Я делаю калькулятор как апплет Java.Я успешно создал макет и зарегистрировал actionListener для всех JButtons.
Итак, в моем классе, который реализует ActionListener, у меня есть этот код для всех цифровых кнопок:
String buttonClicked = e.getActionCommand();
// for number buttons
for(int i = 0; i <= 9; i++)
{
if(i == Integer.parseInt(buttonClicked) )
answer.setText(answer.getText() + e.getActionCommand() );
}
... который работает правильно.Итак, почему этот код не работает:
if(buttonClicked.equals("/") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
... мы не можем сравнить строки с содержимым строки getActionCommand ()?Или я не импортирую то, что должен?РЕДАКТИРОВАТЬ - вот мой полный код:
`
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
public class Part_C extends Applet
{
// answer field here (no panel; JTextField is 100% width without panel)
private JTextField answer = new JTextField();
//answer.setHorizontalAlignment(JTextField.RIGHT); // align input to the right
// JButton array
private JButton[] buttons =
{
// (panel 1) backspace, clear entry, and clear buttons here
new JButton("backspace"),
new JButton("CE"),
new JButton("C"),
// (panel 2, row 1) numbers 7 - 9, divide, and sqare-root buttons here
new JButton("7"),
new JButton("8"),
new JButton("9"),
new JButton("/"),
new JButton("sqrt"),
// (panel 2, row 2) numbers 4 - 6, multiply, and percent buttons here
new JButton("4"),
new JButton("5"),
new JButton("6"),
new JButton("X"),
new JButton("%"),
// (panel 2, row 3) numbers 1 - 3, subtract, and inverse buttons here
new JButton("1"),
new JButton("2"),
new JButton("3"),
new JButton("-"),
new JButton("1/x"),
// (panel 2, row 4) number 0, positive/negative, decimal, addition, and equals buttons here
new JButton("0"),
new JButton("+/-"),
new JButton("."),
new JButton("+"),
new JButton("="),
}; // end of JButton array
// constructor for class Part_C
public Part_C()
{
// first panel for backspace, clear entry, and clear buttons
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(1, 3) );
for(int i = 0; i < 3; i++)
panel1.add(buttons[i] );
// second panel for operators and operands
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 5) );
for(int i = 3; i < buttons.length; i++)
panel2.add(buttons[i] );
// third panel for BorderLayout of answer JTextField, and first 2 panels
JPanel panel3 = new JPanel(new BorderLayout() );
panel3.add(answer, BorderLayout.NORTH);
panel3.add(panel1, BorderLayout.CENTER);
panel3.add(panel2, BorderLayout.SOUTH);
add(panel3); // add panel3 to the JFrame
// action listener created here
ButtonListener listener = new ButtonListener();
// action listener is added to all JButtons here
for (int i = 0; i < buttons.length; i++)
buttons[i].addActionListener(listener);
}
/* overriding the init() method of Applet;
* ...in an Applet, init() is used instead
* of the "main" method */
public void init()
{
// create JFrame
Part_C frame = new Part_C();
// the applet stops when the window / tab is closed
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// inner class ButtonListener
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String buttonClicked = e.getActionCommand();
// for number buttons
for(int i = 0; i <= 9; i++)
{
if(i == Integer.parseInt(buttonClicked) )
answer.setText(answer.getText() + e.getActionCommand() );
}
// for operators
if(buttonClicked.equals("/") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("sqrt") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("X") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("%") )
{
answer.setText(e.getActionCommand() );
}
else if(buttonClicked.equals("-") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("1/x") )
{
answer.setText(e.getActionCommand() );
}
else if(buttonClicked.equals("+/-") )
{
answer.setText(e.getActionCommand() );
}
else if(buttonClicked.equals(".") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("+") )
{
answer.setText(e.getActionCommand() );
}
// for data-clearing operations
if(buttonClicked.equals("backspace") )
{
answer.setText(answer.getText().substring(0, answer.getText().length() - 1) );
}
else if(buttonClicked.equals("CE") )
{
}
else if(buttonClicked.equals("C") )
{
answer.setText("");
}
// for the equals button
if(buttonClicked.equals("=") )
{
}
}
} // end of inner class buttonListener
} // end of Part_C