java计数器代码(求一java计算器编辑 源代码)

:暂无数据 2026-08-28 05:00:06 :0

java计数器代码(求一java计算器编辑 源代码)

各位老铁们好,相信很多人对java计数器代码都不是特别的了解,因此呢,今天就来为大家分享下关于java计数器代码以及求一java计算器编辑 源代码的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

本文目录

求一java计算器编辑 源代码

那样的可能不是很好做,我给你这个,你看一下吧。我用这个经过了Java的实验答辩。

import java.awt.*;//AWT核心包

import java.awt.event.*;//提供事件类和监听器

public class Counter extends Frame implements ActionListener

{

 TextField t=new TextField("");

 Panel p1=new Panel();//new一个panel,用于存放数字键和符号键。

 Panel p2=new Panel();//new一个panel,用于存放开方、平方、和清除键。

 Button;//创建对象Button

 Button bAdd=new Button("加");//实例化Button对象

 Button bSub=new Button("减");

 Button bMul=new Button("乘以");

 Button bPoint=new Button(".");

 Button bDiv=new Button("除以");

 Button bEqual=new Button("等於");

 Button bSqrt=new Button("开方");

 Button bPow=new Button("平方");

 Button bNull=new Button("清除");

 String str1="";     //str1和str2存放两个输入的数

 String str2="";

 String operator=null;   //存放加减乘除以及开平方的符号

 boolean first=true;    //检验输入的是否为第一个数

 int countOper=0;    //累计输入符号的个数,连加连减等操作中会用到

 double result=0.0;    //暂存结果

 double num1=0.0,num2=0.0;  //两个输入的数做运算时转化为double存放

 boolean error=false;   //检验除数是否为0

 //构造方法

 public Counter()

 {

  super("计算器");

  t.setEditable(false);

  for(int i=0;i《10;i++)//利用for循环将数字键添加进p1中

  {

   b=new Button(String.valueOf(i));

   p1.add(b);

   b.setActionCommand("number");

   b.addActionListener(this);//调用addActionListener()方法注册事件监听器

  }

  p1.add(bPoint);

  bPoint.setActionCommand("number");

  p1.add(bAdd); //数字键,符号键放置在panel的p1中

  p1.add(bSub);

  p1.add(bMul);

  p1.add(bDiv);

  p1.add(bEqual);

  p2.add(bSqrt);//开方键,平方键,清除键放置在panel的p2中

  p2.add(bPow);    

  p2.add(bNull);

  bAdd.setActionCommand("oper");

  bSub.setActionCommand("oper");

  bMul.setActionCommand("oper");

  bDiv.setActionCommand("oper");

  bAdd.addActionListener(this);//调用addActionListener()方法注册事件监听器

  bSub.addActionListener(this);

  bMul.addActionListener(this);

  bDiv.addActionListener(this);

  bPoint.addActionListener(this);

  bEqual.addActionListener(this);

  bSqrt.addActionListener(this);

  bNull.addActionListener(this);

  p1.setLayout(new GridLayout(4,4,5,5));//网格布局管理器,把容器根据行数和列数分成同样大小的单元,

                                     //每个单元可容纳一个组件,并且此组件会填满网格单元,不能控

                                     //制其占据网格的大小。4、4为网格的行、列数。5、5为组建之间的

                                     //间距

  p2.setLayout(new FlowLayout());//用FlowLayout布局管理器将组建默认剧中排放,默认间隙为5个像素

  add(t,"North");      //frame的north放置输入框,panel放置在center和south

  add(p1,"Center");//将p1添加到Center中

  add(p2,"South");//将p2添加到South中

  setLocation(400,200);//设计按钮尺寸

  setSize(200,200);//设计窗口尺寸

  setBackground(new Color(20,200,10));//设置Frame的背景,默认为白色

  setVisible(true);//设置Frame设置为可见

  addWindowListener(new WindowAdapter(){   //关闭窗口功能

   public void windowClosing(WindowEvent e)

   {

    System.exit(0);

   }

  });

 }

 //实现接口ActionListener

 public void actionPerformed(ActionEvent e)

 {

  Button temp=(Button)e.getSource();

 if(e.getActionCommand().equals("number"))

  {

   if(first)

   {

    str1=str1+temp.getLabel();

    t.setText(str1);//将输入的str1显示在文本框中

   }

   else

   {

    str2=str2+temp.getLabel();

    t.setText(str2);//将输入的str2显示在文本框中

   }

  }

  else if(e.getActionCommand().equals("oper"))

  {

   if(str1=="")//如果还没有输入数就点击运算符执行if

   {

    countOper=0;//若此,则将计数清零

    first=true;

   }

   else

   {

    countOper++;//计算输入符号的个数

    if(countOper》1)//若输入的符号个数多余一个,则可以进行计算

    {

     getResult();

    }

    operator=temp.getLabel();//存放加减乘除以及开方、平方的符号

    first=false;

   }

  }

  else if(e.getActionCommand().equals("开方"))

  {

    double d=Math.sqrt(Double.parseDouble(str1));

    str1=String.valueOf(d);//将计算出来的结果再次传给str1,为连计算准备

    t.setText(String.valueOf(d));//将计算出来的结果传至文本框中

    first=false;//置为false,即已输入第一个数

   }  

  else if(e.getActionCommand().equals("平方"))

  {

    double f=Math.pow(Double.parseDouble(str1),2);

    str1=String.valueOf(f);

    t.setText(String.valueOf(f));

    first=false;

   }  

  else if(e.getActionCommand().equals("清除"))

  {

   str1="";//清空

   str2="";

   t.setText("");//将文本框清空

   countOper=0;//将按键计数器清零

   first=true;

   error=false;

  }

  else if(e.getActionCommand().equals("等於"))

  {

   if((str1=="")||(str2==""))   //两个数没有输全就点击等号,执行if

   {

    countOper=0;//将按键计数器清零

    first=true;

   }

  else

   {

    getResult();

    countOper=0;

   }

  }

 }

 //运算结果的方法

 public void getResult()

 {

  num1=Double.parseDouble(str1);

  num2=Double.parseDouble(str2);

  if(operator.equals("加"))

  {

   result=num1+num2;

  }

  else if(operator.equals("减"))

  {

   result=num1-num2;

  }

  else if(operator.equals("乘以"))

  {

   result=num1*num2;

  }

  else if(operator.equals("除以"))

  {

   if(num2==0.0)    //除数为0的处理方法

   {

    error=true;

   }

   else

   {

    result=num1/num2;

   }

  }

  if(error)

  {

   t.setText("error");

  }

  else

  {

   t.setText(String.valueOf(result));

   str1=String.valueOf(result);  //运算后把结果放入str1中,str2清空,为连加连减等操作做准备

   str2="";

  }

 }

 //主方法

 public static void main(String args)

 {

  new Counter();//创建一个对象"计算器"

 }

}

用java编写一个计数器或计时器

import java.awt.BorderLayout;
import java.awt.Container;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TimerDemo extends JFrame implements ActionListener {
    private static final long serialVersionUID = 201306211111L;
    private JTextField screen = new JTextField("0");
    private JButton start = new JButton("开始");
    private JButton reset = new JButton("重置");
    private JPanel panel = new JPanel();
    private boolean isRunning;
    private int time;
    private int timeBetween;
    public TimerDemo(int timeBetween) {
        super("计时器");
        this.timeBetween = timeBetween;
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public TimerDemo() {
        super("计时器");
        this.timeBetween = 100;
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void init() {
        panel.setLayout(new GridLayout());
        panel.add(start);
        panel.add(reset);
        start.addActionListener(this);
        reset.addActionListener(this);
        screen.setFont(new Font("幼圆", Font.BOLD, 60));
        screen.setHorizontalAlignment(JTextField.CENTER);
        screen.setEditable(false);
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        c.add(panel, BorderLayout.SOUTH);
        c.add(screen, BorderLayout.CENTER);
        this.setSize(200, 150);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    public static void main(String args) {
        new TimerDemo(1);// 设定 1ms/次
        // new TimerDemo();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == start) {
            if (start.getText().equals("开始")) {
                start.setText("暂停");
                isRunning = true;
            } else if (start.getText().equals("暂停")) {
                start.setText("开始");
                isRunning = false;
            }
        }
        if (e.getSource() == reset) {
            start.setText("开始");
            screen.setText("0");
            isRunning = false;
            time = 0;
        }
        new Thread(new TimeZone()).start();
    }
    class TimeZone implements Runnable {
        @Override
        public void run() {
            while (isRunning) {
                time++;
                if (time 》= Integer.MAX_VALUE) {
                    screen.setText("ERROR");
                    JOptionPane.showMessageDialog(null, "ERROR");
                    isRunning = false;
                }
                screen.setText(String.valueOf(time));
                try {
                    Thread.sleep(timeBetween);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

用java 设计一个counter

/**
 * 
 * @author Administrator
 *
 */
public class Counter {
//单例模式
private static Counter uniqueCounter = null;
 
private Counter() {
  // Exists only to defeat instantiation.
}
//获取实例
public synchronized static Counter getInstance() {
   if (uniqueCounter == null) {
   uniqueCounter = new Counter();
   }
   return uniqueCounter;
}
//计数
private static int count = 0;
/**
 * 获取计数 
 * @return
 */
public static int getCount() {
return count;
}
/**
 * 互斥访问
 * 增加偏移量
 * @param offset
 */
public synchronized static void add (int offset) {
count = count + offset;
}
/**
 * 互斥访问
 * 减少偏移量
 * @param offset
 */
public synchronized static void sub (int offset) {
count = count - offset;
}
}

访问的时候用Counter.getInstance()获取实例。

java程序编写

写快了没注意,谢谢提醒
public class Computer {
protected int CountValue = 0;

public int getCountValue()
{
return this.CountValue;
}

public Computer(int a){
this.CountValue = a;
}

public void increment()
{
this.CountValue++;
}

public void decrement()
{
this.CountValue--;
}

public void reset()
{
this.CountValue = 0;
}

public static void main(String args)
{
Computer com = new Computer(10);
com.increment();
com.increment();
com.increment();
System.out.println(com.getCountValue());
com.decrement();
System.out.println(com.getCountValue());
com.reset();
System.out.println(com.getCountValue());
}

}

计数器的java代码

Java技术与Java虚拟机中文JAVA技术网g0cVV+]
中文JAVA技术网I5Zz[ }’Og G6Y
  说起Java,人们首先想到的是Java编程语言,然而事实上,Java是一种技术,它由四方面组成:Java编程语言、Java类文件格式、Java虚拟机和Java应用程序接口(Java API)。它们的关系如下图所示:
_2tRtm*K
中文JAVA技术网yz;_Y0FB
图1 Java四个方面的关系
,YW&x ^L |Q0  运行期环境代表着Java平台,开发人员编写Java代码(.java文件),然后将之编译成字节码(.class文件)。最后字节码被装入内存,一旦字节码进入虚拟机,它就会被解释器解释执行,或者是被即时代码发生器有选择的转换成机器码执行。从上图也可以看出Java平台由Java虚拟机和Java应用程序接口搭建,Java语言则是进入这个平台的通道,用Java语言编写并编译的程序可以运行在这个平台上。这个平台的结构如下图所示:
&x[+DX0\:W-o@a0中文JAVA技术网"? c3Y6bj-_
中文JAVA技术网P @+O3lf_pJ b

中文JAVA技术网5h:OY TyL2\|Z
  在Java平台的结构中, 可以看出,Java虚拟机(JVM) 处在核心的位置,是程序与底层操作系统和硬件无关的关键。它的下方是移植接口,移植接口由两部分组成:适配器和Java操作系统, 其中依赖于平台的部分称为适配器;JVM 通过移植接口在具体的平台和操作系统上实现;在JVM 的上方是Java的基本类库和扩展类库以及它们的API, 利用Java API编写的应用程序(application) 和小程序(Java applet) 可以在任何Java平台上运行而无需考虑底层平台, 就是因为有Java虚拟机(JVM)实现了程序与操作系统的分离,从而实现了Java 的平台无关性。
h6l-v;HV1SvYO0中文JAVA技术网-G5cAK(e w}
  那么到底什么是Java虚拟机(JVM)呢?通常我们谈论JVM时,我们的意思可能是:中文JAVA技术网,T,Y5d(B5r
中文JAVA技术网yWv~s;J
  对JVM规范的的比较抽象的说明;
b0r[/o_k+p0  对JVM的具体实现;中文JAVA技术网Xdt8M1sP T_"_s
  在程序运行期间所生成的一个JVM实例。中文JAVA技术网H k0g,~ hO\
中文JAVA技术网C3@ w6O-a
  对JVM规范的的抽象说明是一些概念的集合,它们已经在书《The Java Virtual Machine Specification》(《Java虚拟机规范》)中被详细地描述了;对JVM的具体实现要么是软件,要么是软件和硬件的组合,它已经被许多生产厂商所实现,并存在于多种平台之上;运行Java程序的任务由JVM的运行期实例单个承担。在本文中我们所讨论的Java虚拟机(JVM)主要针对第三种情况而言。它可以被看成一个想象中的机器,在实际的计算机上通过软件模拟来实现,有自己想象中的硬件,如处理器、堆栈、寄存器等,还有自己相应的指令系统。中文JAVA技术网&nC/D IHP,|
A)R-DFw o2X0  JVM在它的生存周期中有一个明确的任务,那就是运行Java程序,因此当Java程序启动的时候,就产生JVM的一个实例;当程序运行结束的时候,该实例也跟着消失了。下面我们从JVM的体系结构和它的运行过程这两个方面来对它进行比较深入的研究。
Z6?gXm!~0Java虚拟机的体系结构
2XFZ@"I I,\0
e-i’_;P X a0  刚才已经提到,JVM可以由不同的厂商来实现。由于厂商的不同必然导致JVM在实现上的一些不同,然而JVM还是可以实现跨平台的特性,这就要归功于设计JVM时的体系结构了。中文JAVA技术网{ v2T&tMlG?"]*N
中文JAVA技术网,HK3YQN k
  我们知道,一个JVM实例的行为不光是它自己的事,还涉及到它的子系统、存储区域、数据类型和指令这些部分,它们描述了JVM的一个抽象的内部体系结构,其目的不光规定实现JVM时它内部的体系结构,更重要的是提供了一种方式,用于严格定义实现时的外部行为。每个JVM都有两种机制,一个是装载具有合适名称的类(类或是接口),叫做类装载子系统;另外的一个负责执行包含在已装载的类或接口中的指令,叫做运行引擎。每个JVM又包括方法区、堆、Java栈、程序计数器和本地方法栈这五个部分,这几个部分和类装载机制与运行引擎机制一起组成的体系结构图为:中文JAVA技术网BW0EZCI
2o\d oz0中文JAVA技术网%K1Hl:f ~(@Y(U
图3 JVM的体系结构中文JAVA技术网0P/b(jt U$x
7{F*d8f7ySt:Q0  JVM的每个实例都有一个它自己的方法域和一个堆,运行于JVM内的所有的线程都共享这些区域;当虚拟机装载类文件的时候,它解析其中的二进制数据所包含的类信息,并把它们放到方法域中;当程序运行的时候,JVM把程序初始化的所有对象置于堆上;而每个线程创建的时候,都会拥有自己的程序计数器和Java栈,其中程序计数器中的值指向下一条即将被执行的指令,线程的Java栈则存储为该线程调用Java方法的状态;本地方法调用的状态被存储在本地方法栈,该方法栈依赖于具体的实现。中文JAVA技术网 t?!Pz!B]
"^x1MT5qj0  下面分别对这几个部分进行说明。
/}C2v7b:]%vo[|0中文JAVA技术网’z/@1RG7[E1`8B
  执行引擎处于JVM的核心位置,在Java虚拟机规范中,它的行为是由指令集所决定的。尽管对于每条指令,规范很详细地说明了当JVM执行字节码遇到指令时,它的实现应该做什么,但对于怎么做却言之甚少。Java虚拟机支持大约248个字节码。每个字节码执行一种基本的CPU运算,例如,把一个整数加到寄存器,子程序转移等。Java指令集相当于Java程序的汇编语言。中文JAVA技术网 ^s&H2X*b,Z
q3[ OT`z"M4V0  Java指令集中的指令包含一个单字节的操作符,用于指定要执行的操作,还有0个或多个操作数,提供操作所需的参数或数据。许多指令没有操作数,仅由一个单字节的操作符构成。
P.R.Uxbfc0
o"o qq9_0  虚拟机的内层循环的执行过程如下:中文JAVA技术网3Hn8B r,xE
MjHj(zBG t"m P0do{
l$I&z V!M9[0取一个操作符字节;中文JAVA技术网7[?3o?,v A|3b+O R
根据操作符的值执行一个动作;
I(^e)G)Hf6`0}while(程序未结束)中文JAVA技术网y}0J/|:so
中文JAVA技术网c|%L(ud
  由于指令系统的简单性,使得虚拟机执行的过程十分简单,从而有利于提高执行的效率。指令中操作数的数量和大小是由操作符决定的。如果操作数比一个字节大,那么它存储的顺序是高位字节优先。例如,一个16位的参数存放时占用两个字节,其值为:
0H.w`wb}.X0中文JAVA技术网y{X;n.u4PT+e)D7\&E
  第一个字节*256+第二个字节字节码。中文JAVA技术网"VJ!_0v+D
5^t6E[3aW![0  指令流一般只是字节对齐的。指令tableswitch和lookup是例外,在这两条指令内部要求强制的4字节边界对齐。
#Rop’v6w FS0中文JAVA技术网g8Ai _)iw8r
  对于本地方法接口,实现JVM并不要求一定要有它的支持,甚至可以完全没有。Sun公司实现Java本地接口(JNI)是出于可移植性的考虑,当然我们也可以设计出其它的本地接口来代替Sun公司的JNI。但是这些设计与实现是比较复杂的事情,需要确保垃圾回收器不会将那些正在被本地方法调用的对象释放掉。
&~:Jin+U$zE SI7Y0
"j1lJ`7Dd0v"v0  Java的堆是一个运行时数据区,类的实例(对象)从中分配空间,它的管理是由垃圾回收来负责的:不给程序员显式释放对象的能力。Java不规定具体使用的垃圾回收算法,可以根据系统的需求使用各种各样的算法。中文JAVA技术网’z/wgb.s
中文JAVA技术网\TMY&[:H)Z#@ tr

java编一个计算器的代码

界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!
代码如下:
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;i《20;i++)
{
final JButton b=new JButton(arg);
//this.add(new JButton(arg));
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;i《16;i++)
{
final JButton b=new JButton(arg);
//this.add(new JButton(arg));
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);
}
}
------------------------------------------------------------
才子_辉祝您愉快!

试编写java代码实现一个计数器类(Computer),其中包括:变量value初始值为0

class Computer{
    int value;
    Computer(int value){
        this.value=value;
    }
    public void add(){
        System.out.println("Value:"+value+"-》"+(value+1));
        value++;
    }
    public void sub(){
        System.out.println("Value:"+value+"-》"+(value-2));
        value-=2;
    }
    public void clear(){
        System.out.println("Value:"+value+"-》"+0);
        value=0;
    }
}
public class Demo{
    public static void main(String args){
        Computer computer=new Computer(10);
        computer.add();
        computer.sub();
        computer.clear();
    }
}

计算器java代码

import java.awt.Color;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
class Normal{
double i,j;
public Normal(double num1,double num2){
i=num1;
j=num2;
}
public double puls(){
return i+j;
}
public double subtract(){
return i-j;
}
public double multiply(){
return i*j;
}
public double divide(){
return i/j;
}
public double surpuls(){
return i%j;
}
}
class scientific extends Normal{
public scientific(int num1, int num2) {
super(num1, num2);
}
}
public class calc extends JFrame{
public static void main(String args) {
viewNormal VN= new viewNormal("normal");
}
}
class viewNormal extends JFrame implements ActionListener{
JPanel jp1 = new JPanel(new GridLayout(4,3,5,5));
JPanel jp2 = new JPanel(new GridLayout(5,1,5,5));
JLabel jl;
JButton jb;
JButton jbs,jbo,jba,jbb,jbc,jby;
StringBuffer sb = new StringBuffer();
Normal normal;
int dot=0;
double fnum=0;
double lnum=0;
double result;
String sign=null;
public viewNormal(String title){
setTitle(title);
setLayout(null);
setVisible(true);
setBounds(200,200,305,350);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jb= new JButton;
for(int i=0;i《9;i++){
jb=new JButton(""+(i+1));
jp1.add(jb);
jb.addActionListener(this);
}
jb=new JButton(".");
jb=new JButton("0");
jb=new JButton("=");
jb.addActionListener(this);
jb.addActionListener(this);
jb.addActionListener(this);
jp1.add(jb);
jp1.add(jb);
jp1.add(jb);
jp1.setBounds(10, 100, 200, 200);
jbs= new JButton("+");jbo= new JButton("-");jba= new JButton("*");
jbb= new JButton("/");jby= new JButton("%");jbc= new JButton("C");
jbs.addActionListener(this);jbo.addActionListener(this);jba.addActionListener(this);
jbb.addActionListener(this);jby.addActionListener(this);jbc.addActionListener(this);
//jp2.add(jby);
jp2.add(jbs);jp2.add(jbo);jp2.add(jba);jp2.add(jbb);jp2.add(jbc);
jp2.setBounds(215, 100, 70, 200);
jl= new JLabel("0",JLabel.RIGHT);
jl.setFont(new Font("Batang",Font.BOLD, 20));
jl.setBorder(new LineBorder(Color.black,2));
jl.setBackground(Color.white);
jl.setBounds(10, 40, 275, 50);
jl.setOpaque(true);
add(jl);
add(jp1);
add(jp2);
}
//+
public void sum(){
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.puls();
result=fnum;
}
//-
private void sub() {
System.out.println(sb.toString());
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.subtract();
result=fnum;
}
//*
private void mul() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.multiply();
result=fnum;
}
// /
private void div() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.divide();
result=fnum;
}
//%
private void sur() {
lnum=Double.parseDouble(sb.toString());
normal=new Normal(fnum,lnum);
fnum=normal.surpuls();
result=fnum;
}
// =
private void same(){
if(sign.equals("+")){
sum();
}
if(sign.equals("-")){
sub();
}
if(sign.equals("*")){
mul();
}
if(sign.equals("/")){
div();
}
if(sign.equals("%")){
sur();
}
}
//result
public void Result(){
if(result%1!=0)
jl.setText(""+result);
else
{
int i=(int)result;
jl.setText(""+i);
}
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println(sb.toString());
// 1~9
for(int i=0;i《9;i++){
if(e.getSource()==jb&&!sb.toString().equals("0")){
sb.append(jb.getText());
jl.setText(sb.toString());
}
else if(e.getSource()==jb&&sb.toString().equals("0")){
int d=sb.length();
sb.delete(0, d);
sb.append(jb.getText());
jl.setText(sb.toString());
}
}
// 0
if(e.getSource()==jb&&!sb.toString().equals("0")){
sb.append(jb.getText());
jl.setText(sb.toString());
}
// .
if(e.getSource()==jb&˙==0&&!sb.toString().equals("")){
dot++;
sb.append(jb.getText());
jl.setText(sb.toString());
}
// =
if(e.getSource()==jb&&!sb.toString().equals("")){
same();
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
// +
if(e.getSource()==jbs&&!sb.toString().equals("")){
if(sign!="+"&&sign!=null)
same();
else
sum();
sign ="+";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//-
if(e.getSource()==jbo&&!sb.toString().equals("")){
if(fnum==0)
fnum=2*Double.parseDouble(sb.toString());
if(sign!="-"&&sign!=null)
same();
else
sub();
sign ="-";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//*
if(e.getSource()==jba&&!sb.toString().equals("")){
if(fnum==0)
fnum=1;
if(sign!="*"&&sign!=null)
same();
else
mul();
sign ="*";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
// /
if(e.getSource()==jbb&&!sb.toString().equals("")){
if(fnum==0)
fnum=Math.pow(Double.parseDouble(sb.toString()),2);
if(sign!="/"&&sign!=null)
same();
else
div();
sign ="/";
Result();
int d=sb.length();
sb.delete(0, d);
dot=0;
}
//%
// if(e.getSource()==jby&&!sb.toString().equals("")){
// if(fnum==0){
// fnum=Double.parseDouble(sb.toString());
// result=fnum;
// }
// else {
// if(sign!="%"&&sign!=null)
// same();
// else{
// lnum=Double.parseDouble(sb.toString());
// normal=new Normal(fnum,lnum);
// fnum=normal.surpuls();
// result=fnum;
// }
// }
// sign ="%";
// Result();
// int d=sb.length();
// sb.delete(0, d);
// dot=0;
// }
//clear
if(e.getSource()==jbc){
int d=sb.length();
sb.delete(0, d);
jl.setText("0");
dot=0;
fnum=0;
lnum=0;
sign=null;
}
}
}
class viewScientific extends viewNormal{
public viewScientific(String title){
super(title);
setBounds(200,200,800,500);
}
}
//等号以后输入符号用不了, String转 double 本来就有错误,你可以用我的扩展成科学型的。

用java编程词频计数器,跪求各位大哥帮帮啊!!!!!

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TestWin extends JFrame implements ActionListener {
private JButton openBtn = new JButton("选择文件");
private JLabel resultLabel = new JLabel("", JLabel.CENTER);
private JTextField textField = new JTextField();
private JButton calcBtn = new JButton("计算");
private JFileChooser fileChooser = new JFileChooser(".");
private File file;
public TestWin() {
openBtn.addActionListener(this);
calcBtn.addActionListener(this);
calcBtn.setEnabled(false);
resultLabel.setFont(resultLabel.getFont().deriveFont(60f));
add(openBtn, "North");
add(resultLabel, "Center");
JPanel southPane = new JPanel(new BorderLayout());
southPane.add(textField, "Center");
southPane.add(calcBtn, "East");
add(southPane, "South");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (this.openBtn == source) {
if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog(this)) {
file = fileChooser.getSelectedFile();
this.openBtn.setText(file.getName());
this.openBtn.setEnabled(false);
this.calcBtn.setEnabled(true);
}
} else if (this.calcBtn == source) {
this.calcBtn.setEnabled(false);
String word = this.textField.getText().trim();
if (word.isEmpty()) {
return;
}
Scanner in = null;
try {
in = new Scanner(file);
int count = 0;
while (in.hasNext()) {
if (word.equals(in.next())) {
count++;
}
}
this.resultLabel.setText("" + count);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} finally {
if (in != null) {
in.close();
System.out.println("Scanner关闭");
}
}
this.calcBtn.setEnabled(true);
this.openBtn.setEnabled(true);
this.openBtn.setText("选择文件");
}
}
public static void main(String args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestWin().setVisible(true);
}
});
}
}

关于本次java计数器代码和求一java计算器编辑 源代码的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。

java计数器代码(求一java计算器编辑 源代码)

本文编辑:admin

更多文章:


跷二郎腿太低好吗?想要通过贴墙站改正二郎腿影响的话,有哪些要点需要注意

跷二郎腿太低好吗?想要通过贴墙站改正二郎腿影响的话,有哪些要点需要注意

本篇文章给大家谈谈二郎腿贴地,以及跷二郎腿太低好吗对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。

2026年10月11日 05:10

易语言点击js按钮(易语言网页填表怎样点击链接为“javascript:void(0)“的按钮)

易语言点击js按钮(易语言网页填表怎样点击链接为“javascript:void(0)“的按钮)

本篇文章给大家谈谈易语言点击js按钮,以及易语言网页填表怎样点击链接为“javascript:void(0)“的按钮对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要

2026年10月11日 03:00

regards(Regards,什么意思)

regards(Regards,什么意思)

其实regards的问题并不复杂,但是又很多的朋友都不太了解Regards,什么意思,因此呢,今天小编就来为大家分享regards的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

2026年10月11日 01:10

dom4j文档(如何去掉用dom4j生成的XML文档中的)

dom4j文档(如何去掉用dom4j生成的XML文档中的)

其实dom4j文档的问题并不复杂,但是又很多的朋友都不太了解如何去掉用dom4j生成的XML文档中的,因此呢,今天小编就来为大家分享dom4j文档的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

2026年10月11日 01:00

compare with造句(用compared with和compared to造句)

compare with造句(用compared with和compared to造句)

“compare with造句”相关信息最新大全有哪些,这是大家都非常关心的,接下来就一起看看compare with造句(用compared with和compared to造句)!

2026年10月10日 23:30

xshell软件(Xshell怎么更改字体大小)

xshell软件(Xshell怎么更改字体大小)

各位老铁们好,相信很多人对xshell软件都不是特别的了解,因此呢,今天就来为大家分享下关于xshell软件以及Xshell怎么更改字体大小的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

2026年10月10日 22:50

tensorflow与keras对应版本(为什么tensorflow2.8没有keras)

tensorflow与keras对应版本(为什么tensorflow2.8没有keras)

各位老铁们好,相信很多人对tensorflow与keras对应版本都不是特别的了解,因此呢,今天就来为大家分享下关于tensorflow与keras对应版本以及为什么tensorflow2.8没有keras的问题知识,还望可以帮助大家,解决

2026年10月10日 22:10

个人网站首页界面(深圳社会保险服务个人网页主页)

个人网站首页界面(深圳社会保险服务个人网页主页)

“个人网站首页界面”相关信息最新大全有哪些,这是大家都非常关心的,接下来就一起看看个人网站首页界面(深圳社会保险服务个人网页主页)!

2026年10月10日 22:00

maven仓库jar网站(如何在maven仓库中添加jar包)

maven仓库jar网站(如何在maven仓库中添加jar包)

这篇文章给大家聊聊关于maven仓库jar网站,以及如何在maven仓库中添加jar包对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

2026年10月10日 19:50

利用地理空间数据云下载dem数据怎么知道具体位置?DEM数据获取

利用地理空间数据云下载dem数据怎么知道具体位置?DEM数据获取

各位老铁们好,相信很多人对dem数据都不是特别的了解,因此呢,今天就来为大家分享下关于dem数据以及利用地理空间数据云下载dem数据怎么知道具体位置的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

2026年10月10日 18:30

最近更新

热门文章

by网站域名444(444ppp现在是什么域名)
2026-08-19 20:20:31 浏览:13
韩国大片视频mv(韩国20个感人mv)
2026-07-11 13:10:02 浏览:12
标签列表