java小计算器

只支持鼠标操作的。

打包好的源代码下载地址:

http://115.com/file/anc92nmj#
calculator.zip

/**
* @(#)CaculatorFrame.java
*
*
*
@author huangliangming
* @说明: 实现了基本功能,只支持鼠标操作

*
@version 1.00 2012/4/3
*/
package calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CaculatorFrame extends JFrame
{
private JButton jButton;
private JTextField textField;
int w = 242,
h = 260;
int buttonSizeX = 45,
buttonSizeY = 35;
static Double num1=0.0;
static String cal;
static boolean isANext = false;
public CaculatorFrame()
{
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int swidth = (int)screensize.getWidth();
int sheight = (int)screensize.getHeight();
setBounds(swidth/2-w/2,sheight/2-h/2,w,h);
setResizable(false);
setVisible(true);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
CaculatorFrame.this.windowClosed();
}
});
initButton();
repaint();
}

public void initButton()
{
setLayout(null);
textField = new JTextField("0");
add(textField);
textField.setBounds(2,2,w-10,30);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setEditable(false);
textField.setBackground(Color.WHITE);
//画数字键
JPanel numPanel = new JPanel();
numPanel.setLayout(new GridLayout(4,4,5,5));
String str = "789+456-123x0.c/";
for (int i=0; i<16; i++)
{
jButton = new JButton(str.substring(i,i+1));
numPanel.add(jButton);
if (i == 3 || i == 7 || i == 11 || i == 15)
{
jButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cal = e.getActionCommand();
try
{
num1 = Double.parseDouble(textField.getText());
if (num1 == 0.0)
{
textField.setText("");
}
}
catch (NumberFormatException event)
{
textField.setText("");
}
textField.setText(e.getActionCommand());
}
});
}
else if (i == 14)
{
jButton.addActionListener(new DelEvent(textField));
}
else
{
jButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text = textField.getText();
String s = e.getActionCommand();
System.out.println("text:"+text);
System.out.println("s:"+s);
if (isANext == true)
{
text = "";
isANext = false;
}
try
{
double tmp = Double.parseDouble(text);
System.out.println("tmp"+tmp);
if (tmp == 0.0)
{
text = "";
}
}
catch (NumberFormatException event)
{
text = "";
}
if (s.equals("."))
{
if (text.indexOf('.')>=0)
{
return;
}
else
{
textField.setText(text+s);
return;
}
}
textField.setText(text+s);
}
});
}
}
add(numPanel);
numPanel.setBounds(2,40,2+(buttonSizeX*4),50+buttonSizeY*4);

JButton jButtonR = new JButton("=");
add(jButtonR);
jButtonR.setBounds(7+(buttonSizeX*4),80+buttonSizeY*3,buttonSizeX, 5+buttonSizeY);
jButtonR.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String result;
double num2=0;
JButton jB = (JButton)e.getSource();
try
{
num2 = Double.parseDouble(textField.getText());
}
catch (NumberFormatException event)
{
textField.setText("");
System.out.println("num2输入错误");
}
System.out.println(cal);
if (cal.equals("+"))
{
result = String.valueOf(num1 + num2);
}
else if (cal.equals("-"))
{
result = String.valueOf(num1 - num2);
}
else if (cal.equals("x"))
{
result = String.valueOf(num1 * num2);
}
else if (cal.equals("/"))
{
result = String.valueOf(num1 / num2);
}
else
{
result = "";
System.out.println("计算出错");
}
textField.setText(result);
isANext = true;
}
});
validate();
}

protected void windowClosed()
{
// Exit application.
System.out.println("exited!");
System.exit(0);
}
}

class DelEvent implements ActionListener
{
private JTextField tField;
DelEvent(JTextField textField)
{
tField = textField;
}
public void actionPerformed(ActionEvent e)
{
tField.setText("0");
}
}
/**
* @(#)Caculator.java
*
* Caculator application
*
*
@author huangliangming
*
@version 1.00 2012/4/3
*/
package calculator;

public class Caculator
{
public static void main(String[] args)
{
CaculatorFrame caculatorFrame = new CaculatorFrame();
// TODO, add your application code
System.out.println("Hello World!");
}
}




你可能感兴趣的:(java)