专栏首页Java帮帮-微信公众号-技术文章全总结Java基础-25(06)图形用户界面编程GUI

Java基础-25(06)图形用户界面编程GUI

package cn.itcast.view;(7)
import cn.itcast.util.UiUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class NewJFrame extends javax.swing.JFrame {
    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
        init();
    }
     public NewJFrame(String name) {
        initComponents();
        init(name);
    }
    private void init() {
        this.setTitle("模拟四则运算");
        UiUtil.setFrameImage(this,"jjcc.jpg");
        UiUtil.setFrameCenter(this);
    }
     private void init(String name) {
        this.setTitle("欢迎"+name+"光临");
        UiUtil.setFrameImage(this,"jjcc.jpg");
        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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        firstNumber = new javax.swing.JTextField();
        secondNumber = new javax.swing.JTextField();
        resultNumber = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        selectOperator = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("第一个操作数");
        jLabel2.setText("第二个操作数");
        jLabel3.setText("结果");
        jLabel4.setText("=");
        selectOperator.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(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(firstNumber)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selectOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(secondNumber))
                .addGap(14, 14, 14)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jLabel3)
                    .addComponent(resultNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, 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)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(secondNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(resultNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(selectOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //获取第一个操作数
        String firstNumberString = this.firstNumber.getText().trim();
        //获取运算符
        String selectOperator = this.selectOperator.getSelectedItem().toString();
        //获取第二个操作数
        String secondNumberString = this.secondNumber.getText().trim();
//        System.out.println(firstNumberString);
//        System.out.println(selectOperator);
//        System.out.println(secondNumberString);
        //数据校验,必须是数字字符串
        String regex = "\\d+";
        //校验第一个数
        if (!firstNumberString.matches(regex)) {
//            System.out.println("数据不匹配");
            //public static void showMessageDialog(Component parentComponent,Object message)
            JOptionPane.showMessageDialog(this, "第一个操作数不满足要求必须是数字");
            this.firstNumber.setText("");
            this.firstNumber.requestFocus();
            return;//回去吧
        }
 //校验第二个数
        if (!secondNumberString.matches(regex)) {
//            System.out.println("数据不匹配");
            //public static void showMessageDialog(Component parentComponent,Object message)
            JOptionPane.showMessageDialog(this, "第二个操作数不满足要求必须是数字");
             this.secondNumber.setText("");
            this.secondNumber.requestFocus();
            return;//回去吧
        }
        //把字符串数据转换为整数
        int firstNumber = Integer.parseInt(firstNumberString);
        int secondNumber = Integer.parseInt(secondNumberString);
        //定义变量接收结果
        int resultNumber = 0;
        switch (selectOperator) {
            case "+":
                resultNumber = firstNumber + secondNumber;
                break;
            case "-":
                resultNumber = firstNumber - secondNumber;
                break;
            case "*":
                resultNumber = firstNumber * secondNumber;
                break;
            case "/":
                if(secondNumber==0){
                     JOptionPane.showMessageDialog(this, "除数不能为0");
                     this.secondNumber.setText("");
                     this.secondNumber.requestFocus();
                     return;
                }else {
                    resultNumber = firstNumber / secondNumber;
                }
                break;
        }
        //把结果赋值给结果框
        this.resultNumber.setText(String.valueOf(resultNumber));
    }//GEN-LAST:event_jButton1ActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new NewJFrame().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField firstNumber;
    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;
    private javax.swing.JTextField resultNumber;
    private javax.swing.JTextField secondNumber;
    private javax.swing.JComboBox selectOperator;
    // End of variables declaration//GEN-END:variables
}

本文分享自微信公众号 - Java帮帮(javahelp)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2016-12-16

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Java基础-25(05)图形用户界面编程GUI

    package cn.itcast.view;(6) import cn.itcast.dao.UserDao; import cn.itcast.dao.i...

    Java帮帮
  • Java基础-25(04)图形用户界面编程GUI

    package cn.itcast.view;(5) import cn.itcast.dao.UserDao; import cn.itcast.dao.i...

    Java帮帮
  • Java基础-25(02)图形用户界面编程GUI

    G:一级菜单 package cn.itcast_08; import java.awt.FlowLayout; import java.awt.Frame; ...

    Java帮帮
  • Hibernate中Session的load和get方法的区别是什么?

    主要有以下三项区别: ① 如果没有找到符合条件的记录,get方法返回null,load方法抛出异常。 ② get方法直接返回实体类对象,load方法返回实体...

    唐怀瑟
  • 工业4.0带来了什么?企业应该如何应对?(长文慎入!)

    工业革命是最具影响力的重大历史性事件。很多人认为,到现在为止世界上只发生过三次工业革命。第一次发生于1700年代,以商用蒸汽机和纺织机为标志。大约在20世纪初...

    点滴科技资讯
  • 【论文解读】UniLM:一种既能阅读又能自动生成的预训练模型

      UniLM是微软研究院在Bert的基础上,最新产出的预训练语言模型,被称为统一预训练语言模型。它可以完成单向、序列到序列和双向预测任务,可以说是结合了AR和...

    zenRRan
  • MSRA周明:计算机能“理解”多少我们的语言了?

    【编者按】对自然语言计算技术的研究人员来说,能够实现人与计算机可以进行有效沟通,计算机能理解用户的意图,执行命令或回答问题一直是他们努力迈进的目标。基于近30年...

    CSDN技术头条
  • adobe flash player升级coredump分析

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuzhina/article/detai...

    血狼
  • MSRA周明:计算机能“理解”多少我们的语言了?

    用户1737318
  • 4分钟训练ImageNet!腾讯机智创造AI训练世界纪录

    腾讯技术工程官方号

扫码关注云+社区

领取腾讯云代金券