java实现计算器的输入框

 我来答
匿名用户
2013-12-06
展开全部
这有什么难的 我把我那个代码给你算了 是SWing写的import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;public class TextOne extends JFrame{ /**
* @param args
*/
public String strCo = "";
public double preNum;
public double nextNum;
public int loca;
public boolean flag = false;
public int mark;
public static void main(String[] args) {
// TODO Auto-generated method stub
TextOne frame = new TextOne();
frame.setVisible(true);
}
public TextOne(){
super();
setTitle("计算器");
setBounds(100,100,230,230);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel viewPanel = new JPanel();
getContentPane().add(viewPanel,BorderLayout.NORTH);
final JTextField textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setColumns(18);
viewPanel.add(textField);
final JPanel buttonPanel = new JPanel();
final GridLayout gridLayout = new GridLayout(4,4);
gridLayout.setVgap(10);
gridLayout.setHgap(10);
buttonPanel.setLayout(gridLayout);
getContentPane().add(buttonPanel,BorderLayout.CENTER);
String[][] names = {{"1","2","3","+"},
{"4","5","6","-"},
{"7","8","9","*"},
{".","0","=","/"}};
final JButton[][] buttons = new JButton[4][4];
for(int i = 0; i < 4; ++ i){
for(int j = 0; j < 4; ++ j){
buttons[i][j] = new JButton(names[i][j]);
buttonPanel.add(buttons[i][j]);
String str = buttons[i][j].getName();

buttons[i][j].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String str = arg0.getActionCommand();
String cStr = textField.getText();
for(int i = 0; i < cStr.length(); ++ i)
if(cStr.charAt(i) == '=' && i != cStr.length() - 1){
strCo = null;
flag = false;
preNum = 0;
nextNum = 0;
loca = 0;
break;
}
if(str.equals("0") || str.equals("1")|| str.equals("2")|| str.equals("3")|| str.equals("4")|| str.equals("5")
|| str.equals("6")|| str.equals("7")|| str.equals("8")|| str.equals("9")|| str.equals(".")){
if(flag){
strCo += str;
// textField.setText(textCon);
}
else{
strCo = str;
flag = true;
}
}
else if(str.equals("=")){
String nexN = cStr.substring(loca,cStr.length());
nextNum = toDouble(nexN);
strCo += "=";
double re = 0.0;
switch(mark){
case 0:re = preNum + nextNum;break;
case 1:re = preNum - nextNum;break;
case 2:re = preNum * nextNum;break;
case 3:re = preNum / nextNum;break;
}
strCo += re;
}
else{
String preN = cStr;
loca = preN.length() + 1;
preNum = toDouble(preN);
switch(str.charAt(0)){
case '+':strCo += "+";mark = 0;break;
case '-':strCo += "-";mark = 1;break;
case '*':strCo += "*";mark = 2;break;
case '/':strCo += "/";mark = 3;break;
}
}
textField.setText(strCo);
}
});

}
}
final JLabel leftLabel = new JLabel();
leftLabel.setPreferredSize(new Dimension(10,0));
getContentPane().add(leftLabel,BorderLayout.WEST);
final JLabel rightLabel = new JLabel();
rightLabel.setPreferredSize(new Dimension(10,0));
getContentPane().add(rightLabel,BorderLayout.EAST);
}
public static double toDouble(String s){
double res = 0.0;
int i = 0;
for(; i < s.length(); ++ i){
if(s.charAt(i) == '.')
break;
else{
res = res * 10 + s.charAt(i) - '0';
}
}
double dRes = 0.0;
double t = 10;
for(int j = i + 1; j < s.length(); ++ j){
dRes += (s.charAt(j) - '0') / t;
t *= 10;
}
return res + dRes;
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式