大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
打开IED:打开自己java编程的软件,采用的是eclipse软件。
创新互联公司专业为企业提供吴起网站建设、吴起做网站、吴起网站设计、吴起网站制作等企业网站建设、网页设计与制作、吴起企业网站模板建站服务,十多年吴起做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
建立java工程。
编写类。
编写类的详细步骤
1.类的基本结构:
由于这里用到了界面,所以要进行窗口界面的编程,按钮事件的处理,和计算处理界面;
package MyCaculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyCaculator extends JFrame {
private int add=1,sub=2,mul=3,div=4;
private int op=0;
boolean ifOp;
private String output="0";
private Button[] jba=new Button[]{new Button("7"),new Button("8"),
new Button("9"),new Button("+"),
new Button("4"),new Button("5"),new Button("6"),new Button("-"),
new Button("1"),new Button("2"),new Button("3"),new Button("*"),
new Button("0"),new Button("."),new Button("="),new Button("/")};
private JPanel jpt=new JPanel();
private JPanel jpb=new JPanel();
private JTextField jtf=new JTextField("");
private MyCaculator(){
}
private void operate(String x){
}
public String add(String x){
return output;
}
public String subtract(String x){
return output;
}
public String multiply(String x){
return output;
}
public String divide(String x){
return output;
}
public String Equals(String x){
return output;
}
public void opClean(){
}
class setOperate_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class setLabel_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String[] args) {
}
}
2.建立构造方法:
所谓构造方法就是,对自己的主类进行初始化,代码如下:
private MyCaculator(){
jpt.setLayout(new BorderLayout());
jpt.add(jtf);
this.add(jpt,BorderLayout.NORTH);
jpb.setLayout(new GridLayout(4,4));
for(int i=0;ijba.length;i++){
jpb.add(jba[i]);
if(i==3||i==7||i==11||i==15||i==14)
jba[i].addActionListener(new setOperate_Act());
else
jba[i].addActionListener(new setLabel_Act());
}
this.add(jpb,BorderLayout.CENTER);
this.setSize(250, 200);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
3.建立数据计算方法
这里的数据计算方法有6个,一个是主方法其他几个是加减乘除的处理方法,代码如下:
private void operate(String x){
double x1=Double.valueOf(x);
double y=Double.valueOf(output);
switch(op){
case 0:output=x;break;
case 1:output=String.valueOf(y+x1);break;
case 2:output =String.valueOf(y-x1);break;
case 3:output =String.valueOf(y*x1);break;
case 4:
if(x1!=0) output=String.valueOf(y/x1);
else output="不能为0";
break;
}
}
public String add(String x){
operate(x);
op=add;
return output;
}
public String subtract(String x){
operate(x);
op=sub;
return output;
}
public String multiply(String x){
operate(x);
op=mul;
return output;
}
public String divide(String x){
operate(x);
op=div;
return output;
}
public String Equals(String x){
operate(x);
op=0;
return output;
}
public void opClean(){
op=0;
output ="0";
}
4.事件处理方法
这里的时间处理方法,没有建立一个整体的方法,二是在为了便于处理的方法,将按钮事件分成两个部分,并采用两个子类来实现,这两个类时内部类要写在主类内部的,代码如下:
class setOperate_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jba[3]){
jtf.setText(add(jtf.getText()));
ifOp=true;
}
else if(e.getSource()==jba[7]){
jtf.setText(subtract(jtf.getText()));
ifOp=true;
}
else if(e.getSource()==jba[11]){
jtf.setText(multiply(jtf.getText()));
ifOp=true;
}
else if(e.getSource()==jba[15]){
jtf.setText(divide(jtf.getText()));
ifOp=true;
}
else if(e.getSource()==jba[14]){
jtf.setText(Equals(jtf.getText()));
ifOp=true;
}
}
}
class setLabel_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
Button tempb=(Button)e.getSource();
if(ifOp){
jtf.setText(tempb.getLabel());
ifOp=false;
}else {
jtf.setText(jtf.getText()+tempb.getLabel());
}
}
}
5.建立main方法:
要想实现我们的代码,我们需在main方法中,实例化我们的对象。
public static void main(String[] args) {
new MyCaculator();
}
具体代码如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
采用java编写一个简单计算器,使用awt和swing
代码如下:
import java.awt.Color;
import java.awt.Font;
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;
/*用java做一个计算器
* 实现整数和小数的加减乘除
* 具有删除和复位功能
*/
public class CaculatorDemo extends JFrame {
private static final long serialVersionUID = 1L;
private StringBuilder sBuilder = new StringBuilder();
private Double a;//中间变量用于存储输入的第一个数
private Double b;//中间变量,用于存储输入的第二个数
private Double double1;//用于接收计算结果
private Integer i;// i用于表示加减乘除
public CaculatorDemo() {
this.setTitle("计算器");
this.setSize(318, 457);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.getContentPane().add(panel);
panel.setLayout(null);
// 定义一个label用于显示输入数据和计算结果,这里不用textfield,可以避免用户输入造成bug,简化程序开发
final JLabel label = new JLabel();
label.setBounds(0, 0, 300, 50);
label.setFont(new Font("dialog", 1, 30));
label.setOpaque(true);// 由于jlabel默认透明,直接设置背景色无效,需要先将不透明设置为true
label.setBackground(Color.white);
panel.add(label);
// 定义按钮组件
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button0 = new JButton("0");
JButton buttonadd = new JButton("+");// 加
JButton buttonminus = new JButton("-");// 减
JButton buttontime = new JButton("×");// 乘
JButton buttondivid = new JButton("÷");// 除
JButton buttonequal = new JButton("=");// 等于
JButton buttondecimal = new JButton(".");// 小数点
JButton buttondelet = new JButton("←");// 删除
JButton buttonclear = new JButton("C");// 清除
// 定义按钮组件位置
button0.setBounds(0, 50, 100, 60);
button0.setFont(new Font("dialog", 1, 30));
panel.add(button0);
button1.setBounds(100, 50, 100, 60);
button1.setFont(new Font("dialog", 1, 30));
panel.add(button1);
button2.setBounds(200, 50, 100, 60);
button2.setFont(new Font("dialog", 1, 30));
panel.add(button2);
button3.setBounds(0, 110, 100, 60);
button3.setFont(new Font("dialog", 1, 30));
panel.add(button3);
button4.setBounds(100, 110, 100, 60);
button4.setFont(new Font("dialog", 1, 30));
panel.add(button4);
button5.setBounds(200, 110, 100, 60);
button5.setFont(new Font("dialog", 1, 30));
panel.add(button5);
button6.setBounds(0, 170, 100, 60);
button6.setFont(new Font("dialog", 1, 30));
panel.add(button6);
button7.setBounds(100, 170, 100, 60);
button7.setFont(new Font("dialog", 1, 30));
panel.add(button7);
button8.setBounds(200, 170, 100, 60);
button8.setFont(new Font("dialog", 1, 30));
panel.add(button8);
button9.setBounds(0, 230, 100, 60);
button9.setFont(new Font("dialog", 1, 30));
panel.add(button9);
buttonadd.setBounds(100, 230, 100, 60);
buttonadd.setFont(new Font("dialog", 1, 30));
panel.add(buttonadd);//加
buttonminus.setBounds(200, 230, 100, 60);
buttonminus.setFont(new Font("dialog", 1, 30));
panel.add(buttonminus);//减
buttontime.setBounds(0, 290, 100, 60);
buttontime.setFont(new Font("dialog", 1, 30));
panel.add(buttontime);//乘
buttondivid.setBounds(100, 290, 100, 60);
buttondivid.setFont(new Font("dialog", 1, 30));
panel.add(buttondivid);//除
buttonequal.setBounds(200, 290, 100, 60);
buttonequal.setFont(new Font("dialog", 1, 30));
panel.add(buttonequal);//等于
buttondecimal.setBounds(0, 350, 100, 60);
buttondecimal.setFont(new Font("dialog", 1, 30));
panel.add(buttondecimal);//小数点
buttonclear.setBounds(100, 350, 100, 60);
buttonclear.setFont(new Font("dialog", 1, 30));
panel.add(buttonclear);//复位
buttondelet.setBounds(200, 350, 100, 60);
buttondelet.setFont(new Font("dialog", 1, 30));
panel.add(buttondelet);//删除
// 给各个按钮设置动作监听器
// 输入数值操作0~9
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("0");
label.setText(sBuilder.toString());
}
});
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("1");
label.setText(sBuilder.toString());
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sBuilder.append("2");
label.setText(sBuilder.toString());
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("3");
label.setText(sBuilder.toString());
}
});
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("4");
label.setText(sBuilder.toString());
}
});
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("5");
label.setText(sBuilder.toString());
}
});
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("6");
label.setText(sBuilder.toString());
}
});
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("7");
label.setText(sBuilder.toString());
}
});
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("8");
label.setText(sBuilder.toString());
}
});
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("9");
label.setText(sBuilder.toString());
}
});
// 输入运算符操作,需要先判断a是否为0.0
buttonadd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("+");
i = 0;
}
});
buttonminus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("-");
i = 1;
}
});
buttontime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("×");
i = 2;
}
});
buttondivid.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("÷");
i = 3;
}
});
buttonequal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 该判断中间变量是否为空
if (!"".equals(sBuilder.toString()) (!(a == 0.0))) {
b = Double.parseDouble(sBuilder.toString());
if (i == 0) {
double1 = a + b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
} else if (i == 1) {
double1 = a - b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
} else if (i == 2) {
double1 = a * b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
} else if (i == 3) {
double1 = a / b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
} else {
label.setText(sBuilder.toString());
}
}
}
});
buttondecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append(".");
label.setText(sBuilder.toString());
}
});
buttonclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder = new StringBuilder();
label.setText("");
}
});
buttondelet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!"".equals(sBuilder.toString())) {
sBuilder.deleteCharAt(sBuilder.length() - 1);
label.setText(sBuilder.toString());
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new CaculatorDemo();
}
}
————————————————
版权声明:本文为CSDN博主「散人陈某」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:
界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Counter {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frame.show();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenu menuFile1 = new JMenu();
JMenu menuFile2 = new JMenu();
JMenu menuFile3 = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("文件");
menuFile1.setText("编辑");
menuFile2.setText("查看");
menuFile3.setText("帮助");
menuFileExit.setText("退出");
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CounterFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuBar.add(menuFile1);
menuBar.add(menuFile2);
menuBar.add(menuFile3);
setTitle("计算器");
setJMenuBar(menuBar);
setSize(new Dimension(400, 280));
this.getContentPane().add(new Allpanel());
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (this.field);
}
}
class Allpanel extends JPanel {
public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/**
* Method Centercenter
*
*
*/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Tool.getinstance().getfield();
this.setLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
for(int i=0;i20;i++)
{
final JButton b=new JButton(arg[i]);
//this.add(new JButton(arg[i]));
this.add(b);
if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();
if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}
else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();
}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}
}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}
begin="no";
to="yes";
}
});
}
if(i==17)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}
if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}
if(!Vec.contains(".")!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}
}
});
}
if(i==18)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
jq=Double.parseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}
if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="/";
}
});
}
if(i==4)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
text.setText(String.valueOf(Math.sqrt(cq)));
}
});
}
if(i==8)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}
if(i==19)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}
else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}
else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}
else
{
jg=String.valueOf(dy);
}
}
text.setText(jg);
Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Tool.getinstance().getfield();
JButton jb1=new JButton("Backspace");
JButton jb2=new JButton(" CE ");
JButton jb3=new JButton(" C ");
this.add(jb1);
this.add(jb2);
this.add(jb3);
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String back=Tool.getinstance().getfield().getText();
text.setText(backmethod(back));
Centercenter.Vec.remove(Centercenter.Vec.size()-1);
}
});
jb3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
text.setText("0.");
Centercenter.Vec.clear();
Centercenter.Vec.add(".");
Centercenter.vc.add("a");
Centercenter.begin="yes";
Centercenter.vc1.clear();
Centercenter.what=null;
Centercenter.tool=0;
}
});
}
public String backmethod(String str)
{
return str.substring(0,str.length()-1);
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
this.setLayout(new GridLayout(4,1,3,3));
this.add(new JButton("MC"));
this.add(new JButton("MR"));
this.add(new JButton("MS"));
this.add(new JButton("M+"));
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}
---------------------------------------------------------------------------
=============《按你要求特意后改过的最简单功能的代码如下》========================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Counter2 {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frame.show();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
setTitle("计算器");
setSize(new Dimension(400, 280));
this.getContentPane().add(new Allpanel());
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (this.field);
}
}
class Allpanel extends JPanel {
public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/**
* Method Centercenter
*
*
*/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Tool.getinstance().getfield();
this.setLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","4","5","6","*","1","2","3","-","0","=",".","+"};
for(int i=0;i16;i++)
{
final JButton b=new JButton(arg[i]);
//this.add(new JButton(arg[i]));
this.add(b);
if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();
if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}
else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();
}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}
}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}
begin="no";
to="yes";
}
});
}
if(i==14)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}
if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}
if(!Vec.contains(".")!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}
}
});
}
if(i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==11)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
jq=Double.parseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}
if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="/";
}
});
}
if(i==7)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}
else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}
else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}
else
{
jg=String.valueOf(dy);
}
}
text.setText(jg);
Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Tool.getinstance().getfield();
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}
------------------------------------------------------------
才子_辉祝您愉快!