欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Java swing組件實(shí)現(xiàn)簡(jiǎn)易計(jì)算器

 更新時(shí)間:2020年04月18日 08:43:00   作者:海螺肉  
這篇文章主要介紹了基于Java swing組件實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

本文記錄了筆者的第一個(gè)Java程序,基于Java抽象窗口工具(abstract window toolkit , AWT)和Swing(Swing屬于Java Foundation Classes的一部分)實(shí)現(xiàn)的建議計(jì)算器,由于筆者經(jīng)驗(yàn)有限,初學(xué)Java,代碼略帶bug,無(wú)法實(shí)現(xiàn)7+5×8之類式子的計(jì)算,只能實(shí)現(xiàn)算術(shù)運(yùn)算符按從高到低的式子運(yùn)算,部分代碼略顯冗雜,希望大家在評(píng)論區(qū)積極討論完善代碼!

計(jì)算器示意圖

一、代碼相關(guān)知識(shí)簡(jiǎn)介

JFrame(框架)

使用JFrame frame = new JFrame("My Frame");可以創(chuàng)建一個(gè)名為My Frame的windows框架

import javax.swing.*;
public class Test {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame("My Frame");
    
    frame.setSize(300,300);
    frame.setVisible(true);
  }

}

JButton(按鈕)

使用JButton b = new JButtton("My Button");可創(chuàng)建一個(gè)按鈕組件。

import java.awt.*;
import javax.swing.*;
public class Test {
  JFrame frame;

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame("My Frame");
    JButton b = new JButton("My Button");
    
    frame.getContentPane().add(b,BorderLayout.CENTER); //將按鈕放在frame框架中央
    frame.setSize(300,300);
    frame.setVisible(true);
  }

}

JPanel(面板)

面板是一個(gè)容器,與頂層容器不同,JPanel不能獨(dú)立存在,必須放在其他容器的內(nèi)部,下面代碼創(chuàng)建了含有一個(gè)按鈕的紅色面板。

import java.awt.*;
import javax.swing.*;
public class Test {
  JFrame frame;

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame("My Frame");
    JButton b = new JButton("My Button");
    JPanel panel = new JPanel();
    
    panel.add(b);
    panel.setBackground(Color.red);
    frame.getContentPane().add(panel,BorderLayout.SOUTH); //將面板放在frame框架南方
    frame.setSize(300,300);
    frame.setVisible(true);
  }

}

JTextArea(文本輸入框)

使用 JTextArea 類可實(shí)現(xiàn)一個(gè)文本域,其常用構(gòu)造方法如下。

①JTextArea():創(chuàng)建一個(gè)默認(rèn)的文本域。

②JTextArea(int rows,int columns):創(chuàng)建一個(gè)具有指定行數(shù)和列數(shù)的文本域。

③JTextArea(String text):創(chuàng)建一個(gè)包含指定文本的文本域。

④JTextArea(String text,int rows,int columns):創(chuàng)建一個(gè)既包含指定文本,又包含指定行數(shù)和列數(shù)的多行文本域。

出相關(guān)組件介紹外與實(shí)現(xiàn)計(jì)算器還需對(duì)布局有簡(jiǎn)單了解,本文僅使用GridLayout布局管理器,因此只對(duì)此做出介紹,若讀者需要還可自行理解其他布局管理器。

GridLayout是一種網(wǎng)絡(luò)式的布局管理器,將容器空間化為幾行幾列的形式網(wǎng)格,可將每個(gè)組件放在其中一格。

GridLayout定義在java.awt包中,有如下三種構(gòu)造方法

public GridLayout()
public GridLayout(int rows , int cols) //定義的布局有rows行cools列
public GridLayout(int rows , int cols,int h , int w) ////定義的布局有rows行cools列,水平間距為h,垂直間距為w

二、計(jì)算器功能

可實(shí)現(xiàn)加、減、乘、除功能,但由于筆者目前能力有限,若使用加、減、乘、除混合功能時(shí)需按運(yùn)算符優(yōu)先級(jí),從高到小輸入式子如7×8+5而不能按5+7×8輸入,源代碼如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator implements ActionListener{
  JFrame frame;
  JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bd,be,bf,bg,bh,b0,Clear;
  JTextArea ta;
  String Textcontent ="",sum="";
  double result=0;
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Calculator cl = new Calculator();
    cl.go();
  }
  
  public void go()
  {
    frame = new JFrame("Calculator");
    ta = new JTextArea(1,20);        //設(shè)置文本框大小為1行20列
    ta.setBackground(Color.lightGray);
    JPanel cp = new JPanel();
    cp.setLayout(new GridLayout(4,4,5,5));  //四行四列,邊距為5
    JPanel c = new JPanel();
    c.setLayout(new GridLayout(1,2,5,5));  //一行兩列,邊距為5
    b0 = new JButton("0");
    b0.addActionListener(this);       //為每個(gè)按鈕添加監(jiān)聽(tīng)接口
    
    b1 = new JButton("1");
    b1.addActionListener(this);
    
    b2 = new JButton("2");
    b2.addActionListener(this);
    
    b3 = new JButton("3");
    b3.addActionListener(this);
    
    b4 = new JButton("4");
    b4.addActionListener(this);
    
    b5 = new JButton("5");
    b5.addActionListener(this);
    
    b6 = new JButton("6");
    b6.addActionListener(this);
    
    b7 = new JButton("7");
    b7.addActionListener(this);
    
    b8 = new JButton("8");
    b8.addActionListener(this);
    
    b9 = new JButton("9");
    b9.addActionListener(this);
    
    ba = new JButton(".");
    ba.addActionListener(this);
    
    bd = new JButton("+");
    bd.addActionListener(this);
    
    be = new JButton("-");
    be.addActionListener(this);
    
    bf = new JButton("×");
    bf.addActionListener(this);
    
    bg = new JButton("/");
    bg.addActionListener(this);
    
    bh = new JButton("=");
    bh.addActionListener(this);
    
    Clear= new JButton("Clear");
    Clear.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        Textcontent ="";
        result=0;
        sum="";
        ta.setText("");
      }
    });
    
    c.add(ta);
    c.add(Clear);
    c.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    cp.add(b7);
    cp.add(b8);
    cp.add(b9);
    cp.add(bd);
    cp.add(b4);
    cp.add(b5);
    cp.add(b6);
    cp.add(be);
    cp.add(b1);
    cp.add(b2);
    cp.add(b3);
    cp.add(bf);
    cp.add(b0);
    cp.add(ba);
    cp.add(bh);
    cp.add(bg);
    cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    
    Container f = frame.getContentPane();
    f.add(c,BorderLayout.NORTH);
    f.add(cp,BorderLayout.SOUTH);
    
    frame.pack();
    frame.setVisible(true);
  }
  
  public void actionPerformed(ActionEvent e)
  {
    String content = e.getActionCommand();
    ta.append(e.getActionCommand());
    getTextContent(content);
  }
  
  
  public void getTextContent(String content)
  {
    if(content.equals("+")||content.equals("-")||content.equals("×")||content.equals("/"))
    {
      Textcontent = Textcontent+" "+content+" ";
    }
    else if(content.equals("="))
    {
      Textcontent = Textcontent+" "+content;
      sum=GetResult(Textcontent);
    }
    else
    {
      Textcontent = Textcontent+content;
    }
    ta.append(sum);
  }
  
  public String GetResult(String Textcontent)
  {
    String n=Textcontent;
    String []content=n.split(" ");
    result = Double.valueOf(content[0]);
    for(int i=1;i<content.length;i++)
    {
      switch(content[i])
      {
      case "+":
          result = result+Double.valueOf(content[i+1]);
        break;
      case "-":
          result = result-Double.valueOf(content[i+1]);
        break;
      case "×":
        result = result*Double.valueOf(content[i+1]);
        break;
      case "/":
          result = result/Double.valueOf(content[i+1]);
        break;
      case "=":
        break;
      }
    }
    return result+"";
  }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot使用validation-api實(shí)現(xiàn)參數(shù)校驗(yàn)的示例

    SpringBoot使用validation-api實(shí)現(xiàn)參數(shù)校驗(yàn)的示例

    這篇文章主要介紹了SpringBoot使用validation-api實(shí)現(xiàn)參數(shù)校驗(yàn)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 最新評(píng)論