1 Star 0 Fork 0

航哥爱你 / 基本的四则运算

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

基本的四则运算

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor. */

package 模拟四则运算;

import cn.itcast.uiutil;

/** *

  • @author 曹航 */ public class NewJFrame extends javax.swing.JFrame {

    /**

    • Creates new form NewJFrame / public NewJFrame() { initComponents(); init(); } private void init(){ this.setTitle("模拟四则运算"); uiutil.setFrameImage(this); uiutil.setFrameCenter(this); } /*

    • This method is called from within the constructor to initialize the form.

    • WARNING: Do NOT modify this code. The content of this method is always

    • regenerated by the Form Editor. */ @SuppressWarnings("unchecked") //
      private void initComponents() {

      jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); SecondNumber = new javax.swing.JTextField(); FirstNumber = new javax.swing.JTextField(); Resurt = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); SelectName = new javax.swing.JComboBox(); jButton1 = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jLabel1.setText("第一个操作数");

      jLabel2.setText("结果");

      jLabel3.setText("第二个操作数");

      SecondNumber.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SecondNumberActionPerformed(evt); } });

      FirstNumber.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { FirstNumberActionPerformed(evt); } });

      jLabel4.setText("=");

      SelectName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-", "*", "/" }));

      jButton1.setText("计算"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE) .addComponent(jLabel3)) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(SelectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(28, 28, 28) .addComponent(SecondNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1) .addComponent(Resurt, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) .addComponent(jLabel2))) .addGap(41, 41, 41)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(40, 40, 40) .addComponent(FirstNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(358, Short.MAX_VALUE))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(SecondNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4) .addComponent(Resurt) .addComponent(SelectName, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jButton1) .addContainerGap(23, Short.MAX_VALUE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(64, 64, 64) .addComponent(FirstNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(69, Short.MAX_VALUE))) );

      pack(); }//

    private void FirstNumberActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here: }

    private void SecondNumberActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here: }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here: //获取操作符 //获取第一个操作数 //获取第二个操作数 String First =this.FirstNumber.getText().trim(); String Second =this.SecondNumber.getText().trim(); String Select=this.SelectName.getSelectedItem().toString(); System.out.println(First); System.out.println(Second ); System.out.println(Select); //字符串无法进行四则运算,必须先转换成整数 int First1=Integer.parseUnsignedInt(First); int Second2=Integer.parseInt(Second); int Result=0; switch(Select){ case "+": Result=First1+Second2; break; case "-": Result=First1-Second2; break; case "": Result=First1Second2; break; case "/": Result=First1/Second2; break; //得到的结果是整型,输出需要字符型

      }
      
                this.Resurt.setText(String.valueOf(Result));  

    }

    /**

    • @param args the command line arguments / public static void main(String args[]) { / Set the Nimbus look and feel / // / If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

      • For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //

      /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); }

    // Variables declaration - do not modify
    private javax.swing.JTextField FirstNumber; private javax.swing.JTextField Resurt; private javax.swing.JTextField SecondNumber; private javax.swing.JComboBox SelectName; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; // End of variables declaration
    }

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor. */

package cn.itcast;

import java.awt.Dimension; import java.awt.Image; import java.awt.Toolkit; import javax.swing.JFrame;

/** *专门 做界面的报

  • @author 曹航 */ public class uiutil { private uiutil(){ } //修改窗体的小图标 public static void setFrameImage(JFrame jf){ //第一步,获取工具类对象 //public static Toolkit getDeFaultToolkit();获取默认工具包 Toolkit tk=Toolkit.getDefaultToolkit(); Image i= tk.getImage("src\cn\itcast\source\jjcc.jpg"); //给窗体设置图标 jf.setIconImage(i); }

    public static void setFrameCenter(JFrame jf){ /** * 思路: * A:获取屏幕以及窗体宽和高 * B:屏幕的宽高减去窗体的宽高除42 * C:赋值 * D:联系 */ Toolkit tk=Toolkit.getDefaultToolkit(); Dimension d=tk.getScreenSize(); double Screenwidth=d.getWidth(); double Screenheight=d.getHeight(); //获取窗体宽高 int Framewidth=jf.getWidth(); int Frameheight=jf.getHeight(); //计算新的宽高 int width=(int)(Screenwidth-Framewidth)/2; int height=(int)(Screenheight-Frameheight)/2;

     //将新的宽高付给locate
     jf.setLocation(width, height);

    }

}

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/caohanghang/JiBenDeSiZeYunSuan.git
git@gitee.com:caohanghang/JiBenDeSiZeYunSuan.git
caohanghang
JiBenDeSiZeYunSuan
基本的四则运算
master

搜索帮助