重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
//引入各种类文件
创新互联公司-专业网站定制、快速模板网站建设、高性价比永清网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式永清网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖永清地区。费用合理售后完善,十多年实体公司更值得信赖。
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//定义JsqFrame继承Frame
class JsqFrame extends Frame {
double d1, d2; //定义两个double类型的变量
int op = -1; //定义两个int类型的变量
static TextField tf;//定义静态对象TextField
CalPanelL p1; //定义CalPanelL对象
// Constructor构造方法
JsqFrame() {
//以下设置属性
super("计算器");
setLayout(new FlowLayout());
setResizable(false);
setSize(250, 250);
tf = new TextField(18);
tf.setEditable(false);
tf.setBackground(Color.lightGray);
tf.setForeground(Color.red);
tf.setFont(new Font("Arial", Font.BOLD, 16));
add(tf);
p1 = new CalPanelL();
add(p1);
setVisible(true);
// addWindowListener(new Wclose());
}
//添加按钮继承Button类
class CalButton extends Button {
CalButton(String s) {
//设置按钮属性
super(s);
setBackground(Color.WHITE); //设置颜色为白色
}
}
//定义显示器继承Panel类
class CalPanelL extends Panel {
CalButton a, c, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bPN, bPoint,
bAdd, bSub, bMul, bDiv, bL, bR, bLn, bEqual, bCE, bQuit;
CalPanelL() {
//设置显示器的属性
setLayout(new GridLayout(6, 4));
setFont(new Font("TimesRoman", Font.BOLD, 16));
a = new CalButton("");
c = new CalButton("");
b0 = new CalButton("0");
b1 = new CalButton("1");
b2 = new CalButton("2");
b3 = new CalButton("3");
b4 = new CalButton("4");
b5 = new CalButton("5");
b6 = new CalButton("6");
b7 = new CalButton("7");
b8 = new CalButton("8");
b9 = new CalButton("9");
bPN = new CalButton("+/-");
bPoint = new CalButton(".");
// 设置按钮
bAdd = new CalButton("+");
bSub = new CalButton("-");
bMul = new CalButton("*");
bDiv = new CalButton("/");
bL = new CalButton("(");
bR = new CalButton(")");
bLn = new CalButton("ln");
bEqual = new CalButton("=");
bCE = new CalButton("CE");
bQuit = new CalButton("退出");
// 加入按钮
add(a);
add(c);
add(bCE);
bCE.addActionListener(new PressBCE());
add(bQuit);
bQuit.addActionListener(new PressBQuit());
add(b7);
b7.addActionListener(new PressB7());
add(b8);
b8.addActionListener(new PressB8());
add(b9);
b9.addActionListener(new PressB9());
add(bDiv);
bDiv.addActionListener(new PressBDiv());
add(b4);
b4.addActionListener(new PressB4());
add(b5);
b5.addActionListener(new PressB5());
add(b6);
b6.addActionListener(new PressB6());
add(bMul);
bMul.addActionListener(new PressBMul());
add(b1);
b1.addActionListener(new PressB1());
add(b2);
b2.addActionListener(new PressB2());
add(b3);
b3.addActionListener(new PressB3());
add(bSub);
bSub.addActionListener(new PressBSub());
add(b0);
b0.addActionListener(new PressB0());
add(bPoint);
bPoint.addActionListener(new PressBPoint());
add(bPN);
bPN.addActionListener(new PressBPN());
;
add(bAdd);
bAdd.addActionListener(new PressBAdd());
add(bL);
bL.addActionListener(new PressBL());
add(bR);
bR.addActionListener(new PressBR());
add(bLn);
bLn.addActionListener(new PressBLn());
add(bEqual);
bEqual.addActionListener(new PressBEqual());
}
}
//定义PressBAdd实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[加号的监听事件]
class PressBAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 0;
tf.setText(d1 + "+");
} catch (Exception ee) {
}
}
}
//定义PressBSub实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[减号的监听事件]
class PressBSub implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 1;
tf.setText(d1 + "-");
} catch (Exception ee) {
}
}
}
//定义PressBMul实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[乘号的监听事件]
class PressBMul implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 2;
tf.setText(d1 + "*");
} catch (Exception ee) {
}
}
}
//定义PressBDiv实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[除号的监听事件]
class PressBDiv implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 3;
tf.setText(d1 + "/");
} catch (Exception ee) {
}
}
}
//定义PressBL实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[向左键的监听事件]
class PressBL implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 3;
tf.setText(d1 + "(");
} catch (Exception ee) {
}
}
}
//定义PressBR实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[向右键的监听事件]
class PressBR implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String d1 = tf.getText();
op = 3;
tf.setText(d1 + ")");
} catch (Exception ee) {
}
}
}
//定义PressBEqual实现ActionListener
//大体的意思是按计算机按键的时出发的时间,设置按键的监听[等号的监听事件]
class PressBEqual implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
Jsq jsq = new Jsq();
String s = tf.getText();
System.out.println(s);
while (s.indexOf("(") + 1 0 s.indexOf(")") 0) {
String s1 = jsq.caculateHigh(s);
System.out.println(s1);
s = s1;
}
String str = jsq.cacluteMain(s);
System.out.println(str);
tf.setText(String.valueOf(str));
} catch (Exception ee) {
}
}
}
/*
* 批量写吧
* 以下是按1、2、3等等的监听事件
* 还有清零的等等监听事件
*/
class PressBLn implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
double x = Double.parseDouble(tf.getText());
double y;
y = Math.log10(x);
tf.setText(y + "");
} catch (Exception ee) {
}
}
}
class PressBCE implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf.setText("");
}
}
class PressBPN implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String text = tf.getText();
if (text != "") {
if (text.charAt(0) == '-')
tf.setText(text.substring(1));
else if (text.charAt(0) = '0' text.charAt(0) = '9')
tf.setText("-" + text.substring(0));
else if (text.charAt(0) == '.')
tf.setText("-0" + text.substring(0));
}
} catch (Exception ee) {
}
}
}
class PressBPoint implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
if (text.lastIndexOf(".") == -1)
tf.setText(text + ".");
}
}
class PressB0 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "0");
}
}
class PressB1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "1");
}
}
class PressB2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "2");
}
}
class PressB3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "3");
}
}
class PressB4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "4");
}
}
class PressB5 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "5");
}
}
class PressB6 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "6");
}
}
class PressB7 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "7");
}
}
class PressB8 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "8");
}
}
class PressB9 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(text + "9");
}
}
class PressBQuit implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public void Js() {
String text = tf.getText();
tf.setText(text);
}
}
import java.awt.*;//引入包java.awt中所有的类
import java.awt.event.*;//引入包java.awt.event中所有的类.
public class Calculator extends WindowAdapter implements ActionListener//创建Calculator类,
实现ActionListener接口.
{
private double result=0,data1=0,radixPointDepth=1;//定义变量
private boolean radixPointIndicate=false,resultIndicate=false;
private char prec='+';//创建优先默认字符"+"
private Frame f;//创建窗口
private TextField tf;//创建文本框
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;//创建按钮
private Panel p;//创建/面板容器
static public void main(String args[])//main方法,创建calGUI(图形用户界面),完成初试化
{
//构造器
Calculator de=new Calculator();//创建构造方法
de.go();
}
public void go()
{
f=new Frame("计算器");
p=new Panel();//运算界面p
p.setLayout(new GridLayout(4,4)); // 设置p的布局为GridLayout,四行四列
tf=new TextField(30);
//实例化按钮
b1=new Button("7");
b2=new Button("8");
b3=new Button("9");
b4=new Button("+");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("-");
b9=new Button("1");
b10=new Button("2");
b11=new Button("3");
b12=new Button("*");
b13=new Button("0");
b14=new Button(".");
b15=new Button("=");
b16=new Button("/");
b17=new Button("清零");
f.add(tf,"North"); //把文本区域添加到框架的上方
f.add(p,"Center"); //把面版添加到框架的中间
f.add(b17,"South"); //把按钮(清零)添加到框架的下方
//把按钮添加到面版上
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
//为按钮添加监听
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
f.addWindowListener(this); //为框架添加监听
f.setSize(300,190);//设置框架的大小
f.setVisible(true);//设置框架为可见
}
//监听程序
public void actionPerformed(ActionEvent e)
{
String s;
s=e.getActionCommand();
//SWITCH开关
switch(s.charAt(0))
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case
'7': case '8': case '9'://按了“0-9”,就执行下面
if(resultIndicate)
{
result=0;
data1=0;
prec='+';
}
Integer Int1=new Integer(s);
if(radixPointIndicate)
{
radixPointDepth=radixPointDepth/10;
data1=data1+(Int1.intValue())*radixPointDepth;
}
else
{
data1=data1*10+(Int1.intValue());
}
Double displayNumber=new Double(data1);
tf.setText(displayNumber.toString());
resultIndicate=false;
break;
case '+': case '-':case '*':case '/':case '='://按了“+、-、*、/”,就
执行下面
if(s.charAt(0)!='='resultIndicate)
{
prec=s.charAt(0);
resultIndicate=false;
}
else
{
//用SWITCH开关运算出执行了“+、-、*、/”的结果
switch(prec)
{
case '+':
result=result+data1;
break;
case '-':
result=result-data1;
break;
case '*':
result=result*data1;
break;
case '/':
result=result/data1;
break;
}
}
radixPointIndicate=false;
radixPointDepth=1;
displayNumber=new Double(result);
tf.setText(displayNumber.toString());
//监听是否按了“=”
if(s.charAt(0)!='=')
{
data1=0;
prec=s.charAt(0);
}
else
{
resultIndicate=true;
}
break;
case '.':
radixPointIndicate=true;
break;
}
//监听是否按了为“清零”,是则对各数据清零
if(s.equals("清零"))
{
result=0;
data1=0;
radixPointDepth=1;
tf.setText("");
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
我给你找找
package com.bj.calcultor;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calcultor extends Frame implements ActionListener {
public static void main(String[] args) {//定义主方
new Calcultor();//创建匿名对象,并调用test()方法;
}
//定义按钮名称
String[] arr={"1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","."};
JButton [] button=new JButton[arr.length];
JButton reset = new JButton("CE");
JTextField display = new JTextField(20);
//创建窗口,定义组件
//执行窗口事件:关闭窗口
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public Calcultor(){
super("计算器");//定义标题
//定义面板容器,并布局
JPanel jpanel=new JPanel(new GridLayout(4,4));
//添加按钮,并给按钮添加名称
for (int i = 0; i arr.length; i++) {
button[i]= new JButton(arr[i]);
jpanel.add(button[i]);
}
JPanel panel2 = new JPanel();
panel2.add("Northr", display);
display.setEnabled(false);
panel2.add("East", reset);
this.add("North", panel2);
this.add("Center", jpanel);
for (int i = 0; i arr.length; i++){
addWindowListener(new WindowCloser());
setVisible(true);
setSize(400,400);
pack();
button[i].addActionListener(this);
reset.addActionListener(this);
display.addActionListener(this);
}
}
@Override
public void actionPerformed(ActionEvent e) {//定义事件
// TODO Auto-generated method stub
Object target=e.getSource();
String lable=e.getActionCommand();
if(target==reset){
handleReset();
}else if("0123456789.".indexOf(lable)0){
handleNumber(lable);
}else{
hadleOperator(lable);
}
}
boolean isFirstDigit=true;
private void hadleOperator(String key) {
if(operator.equals("+")){
number += Double.valueOf(display.getText());
}else if (operator.equals("-")){
number -= Double.valueOf(display.getText());
}else if (operator.equals("*")){
number *= Double.valueOf(display.getText());
}else if (operator.equals("/")){
number /= Double.valueOf(display.getText());
}else if(operator.equals("=")){
number =Double.valueOf(display.getText());
}
display.setText(String.valueOf(number));
operator=key;
isFirstDigit=true;
}
private void handleNumber(String key) {
if(true){
display.setText(key);//把键值设置为文本框内容
}else if(key.equals(".") display.getText().indexOf(".")0){
display.setText(display.getText()+".");//把文本框内容设置:display.getText()+"."
}else if(!key.equals(".")){
display.setText(display.getText() + key);//把文本框内容设置:display.getText()+key
isFirstDigit=false;
}
}
private void handleReset() {
display.setText("0");
isFirstDigit=true;
operator="=";
}
String operator="=";
Double number=0.0;
}
其实你这种学习编程的方法我是极度不推荐的,正确的方法应该从基础开始,你这个程序是用图形用户界面来写的,入门的话最好还是控制台程序,这样你可以了解整个程序的流程和语法,如果你没有C语言的基础,请找一本讲解C语言的书学习,然后学习好了你再来看JAVA感觉就很简单了。(绝对原创,请求给分)
import java.awt.*;
import java.awt.event.*; //导入包,因为后面要用到里面的东西
public class jisuanqi extends WindowAdapter {
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel(); //构造3个panel对象,这个是个容器,里面可以添加别的组件
TextField txt; // TextField 组件,相当于一个文本区域
private Button[] b = new Button[17]; //看名字就知道是按钮了吧
private String ss[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2",
"3", "*", "清空", "0", "=", "/", "关闭" };
static int a;
static String s, str;
//入口函数,相当于C里面的MAIN函数
public static void main(String args[]) {
(new jisuanqi()).frame(); //调用函数,java里叫方法
}
//将窗口实例化,frame是一个窗口,awt包里的。算法方面自己看看就能明白
public void frame() {
Frame fm = new Frame("简单计算器");
for (int i = 0; i = 16; i++) {
b[i] = new Button(ss[i]);
}
for (int i = 0; i = 15; i++) {
p2.add(b[i]);
}
fm.setBounds(300,200,200,200);
b[16].setBackground(Color.yellow);
txt = new TextField(15);
txt.setEditable(false);
for (int i = 0; i = 16; i++) {
b[i].addActionListener(new buttonlistener());
}
b[16].addActionListener(new close());
fm.addWindowListener(this);
fm.setBackground(Color.red);
p1.setLayout(new BorderLayout());
p1.add(txt, "Center");
p2.setLayout(new GridLayout(4, 4));
p3.setLayout(new BorderLayout());
p3.add(b[16]);
fm.add(p1, "North");
fm.add(p2, "Center");
fm.add(p3, "South");
fm.pack();
fm.setVisible(true);
}
//关闭窗口的方法
public void windowClosing(WindowEvent e) {
System.exit(0);
}
//用这种方法在一个类里面定义另一个类是不好的,应该单独用以个文件承载,这个类是实现了ActionListener接口,用于监听按钮事件。正规的写法应该是在主类里面实现这个接口,然后写一个方法来调用。
class buttonlistener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getLabel() == "=") {
jisuan();
str = String.valueOf(a);
txt.setText(str);
s = "";
} else if (btn.getLabel() == "+") {
jisuan();
txt.setText("");
s = "+";
} else if (btn.getLabel() == "-") {
jisuan();
txt.setText("");
s = "-";
} else if (btn.getLabel() == "/") {
jisuan();
txt.setText("");
s = "/";
} else if (btn.getLabel() == "*") {
jisuan();
txt.setText("");
s = "*";
} else {
txt.setText(txt.getText() + btn.getLabel());
if (btn.getLabel() == "清空")
txt.setText("");
}
}
public void jisuan() {
if (s == "+")
a += Integer.parseInt(txt.getText());
else if (s == "-")
a -= Integer.parseInt(txt.getText());
else if (s == "*")
a *= Integer.parseInt(txt.getText());
else if (s == "/")
a /= Integer.parseInt(txt.getText());
else
a = Integer.parseInt(txt.getText());
}
}
}
//关闭按钮事件
class close implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}