求一个JAVA做的小程序 交作业用 最好是个计算器这种

 我来答
匿名用户
2014-01-01
展开全部
package util2.calculator;import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
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.JTextField;import myutil.MyFrame;
import myutil.MyJPanel;
public class Calculator implements ActionListener {
private MyFrame frame;
private JTextField textField;
private JButton c_Button;
private JButton[] keyButtons;
private String operation;
private Double num1;
private Double num2;
private String tempNum1;
private String tempNum2;
private Double ans;
private static Calculator calculator=new Calculator();
public Calculator() {
frame = new MyFrame("images/4.jpg","猪头牌计算器");
textField = new JTextField(12);
textField.setHorizontalAlignment(JTextField.RIGHT);
c_Button = new JButton("C");
keyButtons = new JButton[16];
String str = "123+456-789*0.=/";
for (int i = 0; i < keyButtons.length; i++) {
keyButtons[i] = new JButton(str.substring(i, i + 1));
}
init();
setFrontAndColor();
addEventHander();
}
public static Calculator newInstance(){
return calculator;
} private void init() {
MyJPanel northPanel = new MyJPanel();
MyJPanel centerPanel = new MyJPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(textField);
northPanel.add(c_Button); centerPanel.setLayout(new GridLayout(4, 4));
for (int i = 0; i < keyButtons.length; i++) {
centerPanel.add(keyButtons[i]);
} frame.setLayout(new BorderLayout());
frame.add(northPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);
} public void showme() {
frame.pack();
//frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} public void setFrontAndColor() {
Font f = new Font("", Font.BOLD, 14);
textField.setFont(f);
for (int i = 0; i < keyButtons.length; i++) {
keyButtons[i].setFont(f); }
// Color c=new Color(0x8f,0x2a,0x03);
// c_Button.setBackground(c);
// c_Button.setForeground(Color.white); } @Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand(); if ("0123456789.".indexOf(str) != -1) {
String temp = textField.getText();
textField.setText(temp + str); } else if (("+-*/").indexOf(str) != -1) { tempNum1 = textField.getText();
if (!tempNum1.equals("")) {

textField.setText("");
operation = str;
}else{
num1=0.0;
operation=str;
} } else if (str.equals("=")) {
tempNum2 = textField.getText();
num1 = Double.parseDouble(tempNum1);
num2 = Double.parseDouble(tempNum2);
if (operation.equals("+")) {
ans = num1 + num2;
} else if (operation.equals("-")) {
ans = num1 - num2;
} else if (operation.equals("*")) {
ans = num1 * num2;
} else if (operation.equals("/")) {
ans = num1 / num2;
}

textField.setText(ans+""); } else if (str.equalsIgnoreCase("C")) {
num1 = 0.0;
num2 = 0.0;
textField.setText("");
} } private void addEventHander() {
for (int i = 0; i < keyButtons.length; i++) {
keyButtons[i].addActionListener(this);
}
c_Button.addActionListener(this); } public static void main(String[] args) {
new Calculator().showme(); }}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式