java 编写的计算器如何支持键盘事件
源代码:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassCalculator...
源代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame
{
public Calculator()
{
setTitle("计算器");
this.getContentPane().add(new CustomPanel().add(new CalculatorPanel()));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
class CalculatorPanel extends JPanel
{
private boolean start;
private String lastCommand;
private double result;
private JPanel panel;
private JLabel display;
public CalculatorPanel()
{
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
display = new JLabel("0");
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
// KeyListener keyListener = new MyKeyListener();
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);
add(panel, BorderLayout.CENTER);
}
void addButton(String desp, ActionListener listener)
{
JButton btn = new JButton(desp);
btn.addActionListener(listener);
btn.addKeyListener(new MyKeyListener());
panel.add(btn);
}
class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input = e.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
public class MyKeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent evt)
{
String input = "";
String command = "";
}
}
public void calculate(double x)
{
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}
}
请高手在clas MyKeyListener中的代码补充完整。谢谢!!!(限于字数,部分省略)。
public static void main(String[] agrs)
{
new Calculator().setVisible(true);
}
上次因为字数限制我没写main函数 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame
{
public Calculator()
{
setTitle("计算器");
this.getContentPane().add(new CustomPanel().add(new CalculatorPanel()));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
class CalculatorPanel extends JPanel
{
private boolean start;
private String lastCommand;
private double result;
private JPanel panel;
private JLabel display;
public CalculatorPanel()
{
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
display = new JLabel("0");
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
// KeyListener keyListener = new MyKeyListener();
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);
add(panel, BorderLayout.CENTER);
}
void addButton(String desp, ActionListener listener)
{
JButton btn = new JButton(desp);
btn.addActionListener(listener);
btn.addKeyListener(new MyKeyListener());
panel.add(btn);
}
class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input = e.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
public class MyKeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent evt)
{
String input = "";
String command = "";
}
}
public void calculate(double x)
{
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}
}
请高手在clas MyKeyListener中的代码补充完整。谢谢!!!(限于字数,部分省略)。
public static void main(String[] agrs)
{
new Calculator().setVisible(true);
}
上次因为字数限制我没写main函数 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询