1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      計算器java代碼gui 計算器java代碼布局

      要用java編的計算器代碼

      給你一個比較簡單的吧。以前寫的。

      創(chuàng)新互聯(lián)建站為客戶提供專業(yè)的網(wǎng)站設(shè)計制作、做網(wǎng)站、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、成都手機(jī)網(wǎng)站制作等網(wǎng)站方面業(yè)務(wù)。

      共兩個類。還只是完成+、-、×、÷運(yùn)算而已。

      GUI只是用了AWT,很簡單,相信一看就能懂了。

      Calculator.java

      public class Calculator{

      private String result = "0";

      private int op = 0,add = 1,sub = 2,mul = 3,div = 4;

      private double stringToDouble(String x){

      double y = Double.parseDouble(x);

      return y;

      }

      private void operate(String x){

      double x1 = stringToDouble(x);

      double y = stringToDouble(result);

      switch (op){

      case 0:

      result = x;

      break;

      case 1:

      result = String.valueOf(y+x1);

      break;

      case 2:

      result = String.valueOf(y-x1);

      break;

      case 3:

      result = String.valueOf(y*x1);

      break;

      case 4:

      if(x1!=0){

      result = String.valueOf(y/x1);

      }else{

      result = "The divisor can't be zero!";

      }

      break;

      }

      }

      public String opAdd(String x){

      operate(x);

      op = add;

      return result;

      }

      public String opSubtract(String x){

      operate(x);

      op = sub;

      return result;

      }

      public String opMultiply(String x){

      operate(x);

      op = mul;

      return result;

      }

      public String opDivide(String x){

      operate(x);

      op = div;

      return result;

      }

      public String opEquals(String x){

      operate(x);

      op = 0;

      return result;

      }

      public void opClean(){

      op = 0;

      result = "0";

      }

      }

      CalculatorGUI.java

      import java.awt.*;

      import java.awt.event.*;

      import java.util.EventObject;

      public class CalculatorGUI{

      private Frame f;

      private Panel p1,p2;

      private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;

      private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;

      private TextField tf;

      private String s,op;

      private Calculator cal = new Calculator();

      private boolean ifOp;

      public CalculatorGUI(){

      f = new Frame("Calculator");

      p1 = new Panel();

      p2 = new Panel();

      b0 = new Button("0");

      b1 = new Button("1");

      b2 = new Button("2");

      b3 = new Button("3");

      b4 = new Button("4");

      b5 = new Button("5");

      b6 = new Button("6");

      b7 = new Button("7");

      b8 = new Button("8");

      b9 = new Button("9");

      bPoint = new Button(".");

      bAdd = new Button("+");

      bDec = new Button("-");

      bMul = new Button("*");

      bDiv = new Button("/");

      bCal = new Button("=");

      tf = new TextField(25);

      tf.setEditable(false);

      }

      public void launchFrame(){

      f.setSize(220,160);

      f.setResizable(false);

      f.addWindowListener(new myWindowListener());

      p1.setLayout(new FlowLayout(FlowLayout.CENTER));

      p1.add(tf);

      f.add(p1,BorderLayout.NORTH);

      p2.setLayout(new GridLayout(4,4));

      b0.addActionListener(new setLabelText_ActionListener());

      b1.addActionListener(new setLabelText_ActionListener());

      b2.addActionListener(new setLabelText_ActionListener());

      b3.addActionListener(new setLabelText_ActionListener());

      b4.addActionListener(new setLabelText_ActionListener());

      b5.addActionListener(new setLabelText_ActionListener());

      b6.addActionListener(new setLabelText_ActionListener());

      b7.addActionListener(new setLabelText_ActionListener());

      b8.addActionListener(new setLabelText_ActionListener());

      b9.addActionListener(new setLabelText_ActionListener());

      bPoint.addActionListener(new setLabelText_ActionListener());

      bAdd.addActionListener(new setOperator_ActionListener());

      bDec.addActionListener(new setOperator_ActionListener());

      bMul.addActionListener(new setOperator_ActionListener());

      bDiv.addActionListener(new setOperator_ActionListener());

      bCal.addActionListener(new setOperator_ActionListener());

      p2.add(b7);

      p2.add(b8);

      p2.add(b9);

      p2.add(bAdd);

      p2.add(b4);

      p2.add(b5);

      p2.add(b6);

      p2.add(bDec);

      p2.add(b1);

      p2.add(b2);

      p2.add(b3);

      p2.add(bMul);

      p2.add(b0);

      p2.add(bPoint);

      p2.add(bCal);

      p2.add(bDiv);

      f.add(p2,BorderLayout.SOUTH);

      f.setVisible(true);

      }

      public void setTextFieldText_Temp(){

      if (tf.getText().length()15 (tf.getText().indexOf(".")==-1 || !s.equals("."))){

      tf.setText(tf.getText()+s);

      }else{

      tf.setText((tf.getText()+s).substring(0,15));

      }

      }

      public void setTextFieldText(){

      if(ifOp){

      ifOp = false;

      tf.setText("");

      setTextFieldText_Temp();

      }else{

      setTextFieldText_Temp();

      }

      }

      public static void main(String[] args){

      CalculatorGUI calculator = new CalculatorGUI();

      calculator.launchFrame();

      }

      class myWindowListener extends WindowAdapter{

      public void windowClosing(WindowEvent e){

      System.exit(0);

      }

      }

      class setLabelText_ActionListener implements ActionListener{

      public void actionPerformed(ActionEvent e){

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

      s = tempB.getLabel();

      setTextFieldText();

      }

      }

      class setOperator_ActionListener implements ActionListener{

      public void actionPerformed(ActionEvent e){

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

      op = tempB.getLabel();

      if(op.equals("+")){

      tf.setText(cal.opAdd(tf.getText()));

      ifOp = true;

      }else if(op.equals("-")){

      tf.setText(cal.opSubtract(tf.getText()));

      ifOp = true;

      }else if(op.equals("*")){

      tf.setText(cal.opMultiply(tf.getText()));

      ifOp = true;

      }else if(op.equals("/")){

      tf.setText(cal.opDivide(tf.getText()));

      ifOp = true;

      }else if(op.equals("=")){

      tf.setText(cal.opEquals(tf.getText()));

      ifOp = true;

      }

      }

      }

      }

      java課程設(shè)計題目及代碼是什么?

      java課程設(shè)計題目及代碼分別是:

      1、題目:計算器。設(shè)計內(nèi)容是設(shè)計一個圖形界面(GUI)的計算器應(yīng)用程序,完成簡單的算術(shù)運(yùn)算。

      設(shè)計要求是設(shè)計的計算器應(yīng)用程序可以完成家法、減法、乘法、除法和取余運(yùn)算。且有小數(shù)點、正負(fù)號、求倒數(shù)、退格和清零功能。

      2、代碼:

      數(shù)字按鈕NumberButton類如下:

      import java.awt.

      import java.awt.event.

      import javax.swing.

      public class NumberButton extends Button.

      {

      int number.

      public NumberButton(int number).

      {

      super(""+number).

      this.number=number.

      setForeground(Color.blue).

      }

      public int getNumber().

      {

      return number;

      }

      }

      其它java課程設(shè)計題目及代碼是:

      題目:華容道。編寫一個按鈕的子類,使用該子類創(chuàng)建的對象代表華容道中的人物。通過焦點事件控制人物顏色,當(dāng)人物獲得焦點時顏色為藍(lán)色,當(dāng)失去焦點時顏色為灰色。

      通過鍵盤事件和鼠標(biāo)事件來實現(xiàn)曹操、關(guān)羽等人物的移動。當(dāng)人物上發(fā)生鼠標(biāo)事件或鍵盤事件時,如果鼠標(biāo)指針的位置是在人物的下方(也就是組件的下半部分)或按下鍵盤的“↓“鍵,該人物向下移動。向左、向右和向上的移動原理類似。

      代碼是:

      String name[]={"曹操","關(guān)羽","張","劉","馬","許","兵","兵","兵","兵"}.

      for(int i=0;iname.length;i++).

      {

      person[i]=new Person(i,name[i]).

      person[i].addKeyListener(this).

      person[i].addMouseListener(this).

      //? ? ?person[i].addFocusListener(new Person).

      add(person[i]).

      }

      person[0].setBounds(104,54,100,100).

      person[1].setBounds(104,154,100,50).

      person[2].setBounds(54,154,50,100).

      person[3].setBounds(204,154,50,100).

      person[4].setBounds(54,54,50,100).

      person[5].setBounds(204,54,50,100);

      person[6].setBounds(54,254,50,50);

      person[7].setBounds(204,254,50,50);

      person[8].setBounds(104,204,50,50);

      person[9].setBounds(154,204,50,50);

      JAVA用GUI編寫計算器,程序有些地方不太明白,在線等,急急急!!!!

      這一堆代碼都是給按鈕添加事件,從給出的代碼可以看到有數(shù)字:9?0?小數(shù)點、還有加減乘除的事件

      求解:寫一段Java程序,要求簡單實現(xiàn)計算器的功能,是GUI編程,代碼簡潔最好。

      import java.awt.BorderLayout;

      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.JPanel;

      import javax.swing.JTextField;

      //暫時不用考慮連加問題

      //點第一個運(yùn)算符 點運(yùn)算符 點第二個運(yùn)算符 點=出結(jié)果

      public class 計算器 implements ActionListener {

      JTextField jtf = new JTextField(10);

      private boolean append = false;

      private String op1 = "0";

      private String operator = "+";

      @Override

      public void actionPerformed(ActionEvent e) {

      String comn = e.getActionCommand();

      // 處理數(shù)字

      if ("0123456789".indexOf(comn) != -1) {

      if (append) {// 追加

      String temp = jtf.getText();

      jtf.setText(temp + comn);

      } else {// 替換

      jtf.setText(comn);

      append = true;

      }

      }

      // 處理運(yùn)算符

      else if ("+-*/".indexOf(comn) != -1) {

      op1 = jtf.getText();

      operator = comn;

      append = false;

      } else if ("=".indexOf(comn) != -1) {

      String op2 = jtf.getText();

      double d1 = Double.parseDouble(op1);

      double d2 = Double.parseDouble(op2);

      if ("+".equals(operator)) {

      d1 = d1 + d2;

      } else if ("-".equals(operator)) {

      d1 = d1 - d2;

      } else if ("*".equals(operator)) {

      d1 = d1 * d2;

      } else if ("/".equals(operator)) {

      d1 = d1 / d2;

      }

      jtf.setText(d1 + "");

      append = false;

      } else if (".".equals(comn)) {

      String temp = jtf.getText();

      if (temp.indexOf(".") == -1) {

      jtf.setText(temp + ".");

      append = true;

      }

      } else if ("+/-".equals(comn)) {

      String temp = jtf.getText();

      if (temp.startsWith("-1")) {

      jtf.setText(temp.substring(1));

      } else {

      jtf.setText("-" + temp);

      }

      } else if ("Backspace".equals(comn)) {

      String temp = jtf.getText();

      if (temp.length() 0) {

      jtf.setText(temp.substring(0, temp.length() - 1));

      }

      } else if ("CE".equals(comn) || "C".equals(comn)) {

      jtf.setText("0");

      append = false;

      }

      }

      public 計算器() {

      JFrame jf = new JFrame("計算器");

      jf.add(jtf, BorderLayout.NORTH);

      String[] s1 = { "Backspace", "CE", "C", "+", "7", "8", "9", "/", "4",

      "5", "6", "*", "1", "2", "3", "-", "0", "+/-", ".", "=" };

      JPanel jp = new JPanel();

      jf.add(jp, BorderLayout.CENTER);

      GridLayout gl = new GridLayout(5, 4);

      jp.setLayout(gl);

      JButton[] jb = new JButton[s1.length];

      for (int i = 0; i s1.length; i++) {

      jb[i] = new JButton(s1[i]);

      jp.add(jb[i]);

      jb[i].addActionListener(this);

      }

      jf.add(jp);

      jtf.setEditable(false);

      jf.setLocation(400, 300);

      jf.pack();

      jf.setResizable(false);// 設(shè)置窗口的大小不可變

      jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

      jf.setVisible(true);

      }

      public static void main(String[] args) {

      new 計算器();

      }

      }

      這個功能比較簡單 不知道能不能滿足要求

      有關(guān)java里面gui的編程題,實現(xiàn)這樣一個建議的計算器

      import javax.swing.*;

      import javax.swing.JTextField;

      import java.awt.*;

      import java.awt.event.*;

      import java.lang.*;

      import java.awt.Color;

      public class Ex5_2 extends JFrame implements ActionListener

      {

      private JPanel p1 = new JPanel(); //創(chuàng)建面板

      private JPanel p2 = new JPanel();

      private JTextField t1; //文本框1用來顯示輸入信息

      private JTextField t2; //文本框2用來顯示結(jié)果信息

      private JLabel label; //標(biāo)簽信息

      StringBuffer str; //顯示屏所顯示的字符串

      double x,y; //x和y都是運(yùn)算數(shù)

      int z; //Z表示單擊了那一個運(yùn)算符.0表示"+",1表示"-",2表示"*",3表示"/"

      private JButton b[] = new JButton[12]; //創(chuàng)建一個有12個按鈕的數(shù)組

      private JButton b1,b2,b3,b4,b5,b6,b7,b8; //算術(shù)功能按鈕

      public Ex5_2()

      {

      super("簡易計算器"); //窗口名稱

      Container c = getContentPane(); //創(chuàng)建內(nèi)容面板對象

      t1 = new JTextField(30);

      t1.setEditable(false); //只能顯示,不能編輯

      t2 = new JTextField(30);

      t2.setEditable(false); //只能顯示,不能編輯

      label = new JLabel("歡迎使用小巫版計算器^_^o~ 努力!");

      label.setForeground(Color.blue);

      //創(chuàng)建一個空字符串緩沖區(qū)

      str=new StringBuffer();

      p2.add(label); //添加標(biāo)簽到面板

      p2.add(t2); //添加文本框到面板

      p2.add(t1); //添加文本框到面板

      p2.setLayout(new GridLayout(4,1)); //把面扳布局為4行1列

      for(int i=0;i10;i++) //分別為數(shù)組中0~9號的按鈕設(shè)置標(biāo)簽,并注冊監(jiān)聽器

      {

      String s=""+i;

      b[i]= new JButton(s);

      b[i].addActionListener(this);

      }

      //實例化各個按鈕

      b[10]= new JButton("-/+"); b[11]= new JButton(".");

      b1= new JButton("/"); b2= new JButton("Back");

      b3= new JButton("*"); b4= new JButton("C");

      b5= new JButton("+"); b6= new JButton("Sqrt");

      b7= new JButton("-"); b8= new JButton("=");

      //設(shè)置按鈕前景色

      for(int i=0;i12;i++)

      {

      b[i].setForeground(Color.blue);

      }

      b1.setForeground(Color.red); b3.setForeground(Color.red);

      b5.setForeground(Color.red); b7.setForeground(Color.red);

      b2.setForeground(Color.blue); b4.setForeground(Color.blue);

      b6.setForeground(Color.red); b8.setForeground(Color.blue);

      //添加到面板

      p1.add(b[7]); p1.add(b[8]); p1.add(b[9]); p1.add(b1); p1.add(b2);

      p1.add(b[4]); p1.add(b[5]); p1.add(b[6]); p1.add(b3); p1.add(b4);

      p1.add(b[1]); p1.add(b[2]); p1.add(b[3]); p1.add(b5); p1.add(b6);

      p1.add(b[0]); p1.add(b[10]); p1.add(b[11]);p1.add(b7);p1.add(b8);

      p1.setLayout(new GridLayout(4,5,5,5));

      //注冊監(jiān)聽器

      b[10].addActionListener(this); b[11].addActionListener(this);

      b1.addActionListener(this); b2.addActionListener(this);

      b3.addActionListener(this); b4.addActionListener(this);

      b5.addActionListener(this); b6.addActionListener(this);

      b7.addActionListener(this); b8.addActionListener(this);

      //將面板添加到內(nèi)容面板

      c.add(p2);

      c.add(p1);

      c.setLayout(new FlowLayout()); //設(shè)置為順序布局

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置窗口關(guān)閉動作

      setVisible(true); //設(shè)置為可見

      setResizable(false); //禁止調(diào)整框架大小

      setSize(400,300); //設(shè)置窗口大小

      }

      //主方法實現(xiàn)創(chuàng)建一個窗口

      public static void main(String[] args)

      { Ex5_2 f = new Ex5_2(); }

      //按鈕的事件處理

      public void actionPerformed(ActionEvent e)

      {

      try

      {

      if(e.getSource()==b4) //選擇"C"清零

      {

      t1.setText("0"); //把文本框清零

      t1.setHorizontalAlignment(JTextField.RIGHT); //文本對齊右邊

      str.setLength(0); //清空字符串緩沖區(qū)以準(zhǔn)備接收新的輸入運(yùn)算數(shù)

      }

      else if(e.getSource()==b[10])//單擊"+/-"選擇輸入的運(yùn)算數(shù)是正數(shù)還是負(fù)數(shù)

      {

      x=Double.parseDouble(t1.getText().trim());//trim函數(shù)作用是去掉字符串中的空格

      t1.setText(""+(-x));

      t1.setHorizontalAlignment(JTextField.RIGHT);

      }

      else if (e.getSource()==b5)//單擊加號按鈕獲得x的值和z的值并清空y的值

      {

      x=Double.parseDouble(t1.getText().trim());

      str.setLength(0);

      y=0d;

      z=0;

      }

      else if(e.getSource()==b7)//單擊減號按鈕獲得x的值和z的值并清空y的值

      {

      x=Double.parseDouble(t1.getText().trim());

      str.setLength(0);

      y=0d;

      z=1;

      }

      else if(e.getSource()==b3)//單擊乘號按鈕獲得x的值和z的值并清空y的值

      {

      x=Double.parseDouble(t1.getText().trim());

      str.setLength(0);

      y=0d;

      z=2;

      }

      else if(e.getSource()==b1)//單擊除號按鈕獲得x的值和z的值并清空y的值

      {

      x=Double.parseDouble(t1.getText().trim());

      str.setLength(0);

      y=0d;

      z=3;

      }

      else if(e.getSource()==b8)//單擊等號按鈕輸出計算結(jié)果

      {

      str.setLength(0);

      switch(z)

      {

      case 0: t1.setText(""+(x+y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;

      case 1: t1.setText(""+(x-y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;

      case 2: t1.setText(""+(x*y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;

      case 3: t1.setText(""+(x/y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;

      }

      }

      else if(e.getSource()==b[11])//單擊"."按鈕輸入小數(shù)

      {

      if(t1.getText().trim().indexOf('.')!=-1)//判斷字符串中是否已經(jīng)包含了小數(shù)點

      {

      }

      else //如果沒有小數(shù)點

      {

      if(t1.getText().trim().equals("0"))//如果初時顯示為0

      {

      t1.setText(str.append(e.getActionCommand()).toString());

      t1.setHorizontalAlignment(JTextField.RIGHT);

      }

      else if(t1.getText().trim().equals(""))//如果初時顯示為空則不做任何操作

      {}

      else

      {

      t1.setText(str.append(e.getActionCommand()).toString());

      t1.setHorizontalAlignment(JTextField.RIGHT);

      }

      }

      y=0d;

      }

      else if(e.getSource()==b6) //求平方根

      {

      x=Double.parseDouble(t1.getText().trim());

      if(x0)

      {

      t1.setText("數(shù)字格式異常");

      t1.setHorizontalAlignment(JTextField.RIGHT);

      }

      else

      {

      t1.setText(""+Math.sqrt(x));

      t1.setHorizontalAlignment(JTextField.RIGHT);

      }

      str.setLength(0);

      y=0d;

      }

      else

      {

      if(e.getSource()==b[0])//如果選擇的是"0"這個數(shù)字鍵

      {

      if(t1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作

      {}

      else

      t1.setText(str.append(e.getActionCommand()).toString());

      t1.setHorizontalAlignment(JTextField.RIGHT);

      y=Double.parseDouble(t1.getText().trim());

      }

      else if (e.getSource()==b2) //選擇的是back鍵

      {

      if(!t1.getText().trim().equals("0"))//如果顯示屏顯示的不是零

      {

      if(str.length()!=1)

      {

      t1.setText(str.delete(str.length()-1,str.length()).toString());//可能拋出字符串越界異常

      t1.setHorizontalAlignment(JTextField.RIGHT);

      }

      else

      {

      t1.setText("0"); t1.setHorizontalAlignment(JTextField.RIGHT);

      str.setLength(0);

      }

      }

      y=Double.parseDouble(t1.getText().trim());

      }

      else

      {

      t1.setText(str.append(e.getActionCommand()).toString());

      t1.setHorizontalAlignment(JTextField.RIGHT);

      y=Double.parseDouble(t1.getText().trim());

      }

      }

      }

      catch(NumberFormatException e1){ t1.setText("數(shù)字格式異常");

      t1.setHorizontalAlignment(JTextField.RIGHT); }

      catch(StringIndexOutOfBoundsException e1){t1.setText("字符串索引越界");

      t1.setHorizontalAlignment(JTextField.RIGHT);}

      }

      }


      文章標(biāo)題:計算器java代碼gui 計算器java代碼布局
      分享URL:http://ef60e0e.cn/article/hgesii.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        马鞍山市| 普宁市| 手游| 民勤县| 红原县| 凯里市| 滨州市| 望都县| 贡嘎县| 永胜县| 福鼎市| 云龙县| 苏尼特右旗| 共和县| 铜山县| 旌德县| 库尔勒市| 长治市| 罗城| 开阳县| 台山市| 光泽县| 益阳市| 图木舒克市| 林芝县| 屏边| 建湖县| 双桥区| 高密市| 师宗县| 海安县| 云林县| 嘉鱼县| 兴义市| 思茅市| 吉隆县| 连州市| 延津县| 西畴县| 海淀区| 高碑店市|