书上的程序,是一个计算器,我想把它改成支持键盘输入,结果一直没有实现,大家帮我看一下,提点建议。
西面是我改后的代码:
/**
@version 1.32 2004-05-05
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
A frame with a calculator panel.
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}/**
A panel with calculator buttons and a result display.
*/
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout()); result = 0;
lastCommand = "=";
start = true;
// add the display display = new JButton("0");
display.setEnabled(true);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
KeyListener insert1 = new InsertAction();
KeyListener command1 = new CommandAction();
display.addKeyListener(insert1);
display.addKeyListener(command1);
// add the buttons in a 4 x 4 grid 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);
} /**
Adds a button to the center panel.
@param label the button label
@param listener the button listener
*/
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
} /**
This action inserts the button action string to the
end of the display text.
*/
private class InsertAction implements ActionListener,KeyListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
public void keyTyped(KeyEvent event)
{
String input = event.getKeyText(1);
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
} /**
This action executes the command that the button
action string denotes.
*/
private class CommandAction implements ActionListener,KeyListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.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 void keyTyped(KeyEvent event)
{
String command1 = event.getKeyText(1); if (start)
{
if (command1.equals("-"))
{
display.setText(command1);
start = false;
}
else
lastCommand = command1;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command1;
start = true;
}
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
} /**
Carries out the pending calculation.
@param x the value to be accumulated with the prior result.
*/
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);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}
西面是我改后的代码:
/**
@version 1.32 2004-05-05
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
A frame with a calculator panel.
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}/**
A panel with calculator buttons and a result display.
*/
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout()); result = 0;
lastCommand = "=";
start = true;
// add the display display = new JButton("0");
display.setEnabled(true);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
KeyListener insert1 = new InsertAction();
KeyListener command1 = new CommandAction();
display.addKeyListener(insert1);
display.addKeyListener(command1);
// add the buttons in a 4 x 4 grid 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);
} /**
Adds a button to the center panel.
@param label the button label
@param listener the button listener
*/
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
} /**
This action inserts the button action string to the
end of the display text.
*/
private class InsertAction implements ActionListener,KeyListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
public void keyTyped(KeyEvent event)
{
String input = event.getKeyText(1);
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
} /**
This action executes the command that the button
action string denotes.
*/
private class CommandAction implements ActionListener,KeyListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.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 void keyTyped(KeyEvent event)
{
String command1 = event.getKeyText(1); if (start)
{
if (command1.equals("-"))
{
display.setText(command1);
start = false;
}
else
lastCommand = command1;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command1;
start = true;
}
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
} /**
Carries out the pending calculation.
@param x the value to be accumulated with the prior result.
*/
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);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}
/**
@version 1.32 2004-05-05
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
A frame with a calculator panel.
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}/**
A panel with calculator buttons and a result display.
*/
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout()); result = 0;
lastCommand = "=";
start = true;
// add the display display = new JButton("0");
display.setEnabled(true);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
KeyListener insert1 = new InsertAction();
KeyListener command1 = new CommandAction();
display.addKeyListener(insert1);
display.addKeyListener(command1);
// add the buttons in a 4 x 4 grid panel = new JPanel();
panel.setLayout(new GridLayout(4, 4)); addButton("7", insert,insert1);
addButton("8", insert,insert1);
addButton("9", insert,insert1);
addButton("/", command,command1); addButton("4", insert,insert1);
addButton("5", insert,insert1);
addButton("6", insert,insert1);
addButton("*", command,command1); addButton("1", insert,insert1);
addButton("2", insert,insert1);
addButton("3", insert,insert1);
addButton("-", command,command1); addButton("0", insert,insert1);
addButton(".", insert,insert1);
addButton("=", command,command1);
addButton("+", command,command1); add(panel, BorderLayout.CENTER);
} /**
Adds a button to the center panel.
@param label the button label
@param listener the button listener
*/
private void addButton(String label, ActionListener listener,KeyListener listener1)
{
JButton button = new JButton(label);
button.addActionListener(listener);
button.addKeyListener(listener1);
panel.add(button);
} /**
This action inserts the button action string to the
end of the display text.
*/
private class InsertAction implements ActionListener,KeyListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
public void keyTyped(KeyEvent event)
{
String input = event.getKeyText(1);
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
} /**
This action executes the command that the button
action string denotes.
*/
private class CommandAction implements ActionListener,KeyListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.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 void keyTyped(KeyEvent event)
{
String command1 = event.getKeyText(1); if (start)
{
if (command1.equals("-"))
{
display.setText(command1);
start = false;
}
else
lastCommand = command1;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command1;
start = true;
}
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
} /**
Carries out the pending calculation.
@param x the value to be accumulated with the prior result.
*/
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);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}
只是增加了加法而已但连我自己都感觉很麻烦- -!
用Tab键选择相应数字按钮和运算符号,用ENTER键确定,并且每一步运算都必须得到结果后才能运算下一次的,比如运算5+4-3,必须先运算5+4,键盘敲击界面上的等号输出结果后,才能再用键盘敲击“-”“3”,才能再敲击界面上的等号输出最终结果。并且鼠标操作和键盘操作不能和起来使用
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class test
{
public static void main(String[] args)
{
testFrame frame = new testFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
A frame with a test panel.
*/
class testFrame extends JFrame
{
public testFrame()
{
setTitle("test");
testPanel panel = new testPanel();
add(panel);
pack();
}
}/**
A panel with test buttons and a result display.
*/
class testPanel extends JPanel
{
public testPanel()
{
setLayout(new BorderLayout()); result = 0;
lastCommand = "=";
start = true;
// add the display display = new JButton("0");
display.setEnabled(true);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
KeyListener insert1 = new InsertAction();
KeyListener command1 = new CommandAction();
display.addKeyListener(insert1);
display.addKeyListener(command1);
// add the buttons in a 4 x 4 grid 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);
} /**
Adds a button to the center panel.
@param label the button label
@param listener the button listener
*/
private void addButton(String label, ActionListener listener)
{
final JButton button = new JButton(label);
button.addActionListener(listener);
/**
*
*添加的键盘监听
*
*/
button.addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyPressed(java.awt.event.KeyEvent evt)
{
if(!(button.getText().trim().equals("+"))&&!(button.getText().trim().equals("-"))&&!(button.getText().trim().equals("*"))&&!(button.getText().trim().equals("/"))&&!(button.getText().trim().equals("="))&&!(button.getText().trim().equals(".")))
{
display.setText(button.getText().trim());
}
if(button.getText().trim().equals("+"))
{
re = Double.valueOf(display.getText().trim()).doubleValue();
temp="+";
}
if(button.getText().trim().equals("-"))
{
re = Double.valueOf(display.getText().trim()).doubleValue();
temp="-";
}
if(button.getText().trim().equals("*"))
{
}
if(button.getText().trim().equals("/"))
{
}
if(button.getText().trim().equals("="))
{
if(temp=="+")
{
display.setText(String.valueOf(re+Double.valueOf(display.getText().trim()).doubleValue()));
}
if(temp=="-")
{
display.setText(String.valueOf(re-Double.valueOf(display.getText().trim()).doubleValue()));
}
}
//display.setText(String.valueOf(re));
}
}
);
panel.add(button);
}
/**
This action inserts the button action string to the
end of the display text.
*/
private class InsertAction implements ActionListener,KeyListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
public void keyTyped(KeyEvent event)
{
String input = event.getKeyText(1);
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
} /**
This action executes the command that the button
action string denotes.
*/
private class CommandAction implements ActionListener,KeyListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.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 void keyTyped(KeyEvent event)
{
String command1 = event.getKeyText(1); if (start)
{
if (command1.equals("-"))
{
display.setText(command1);
start = false;
}
else
lastCommand = command1;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command1;
start = true;
}
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
} /**
Carries out the pending calculation.
@param x the value to be accumulated with the prior result.
*/
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);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand,temp;
private boolean start;
private double dou;
private double re;
}有点繁琐,不好意思,实在懒得写了,再好一点解决方案,就是直接监听键盘按钮按下数字键和运算符按键的事件,然后直接运算,界面只用来看结果.
你可以用弹出消息对话框啊,如下:
JOptionPane jop=new JOptionPane;
String number=jop.showInputDialog(null,"请输入:").tostring();
我也是刚学完JAVA,如果有错,请高手不要见笑.