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)

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

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏向治洪

获取手机短信内容

原理是通过,contentprovider获取系统短信数据库中的字段信息而达到获取内容目的 效果图如下: ? 具体代码如下: package com.inter...

24080
来自专栏数据结构与算法

洛谷P2925 [USACO08DEC]干草出售Hay For Sale

题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate ...

30740
来自专栏码匠的流水账

聊聊sentinel的FlowSlot

com/alibaba/csp/sentinel/slots/block/flow/FlowSlot.java

18510
来自专栏岑玉海

RavenDb学习(七) 异步工作以及维度查询

1、异步执行 var entity = new Company {Name = "Async Company #2", Id = "companies/2"}...

30650
来自专栏小樱的经验随笔

HDU 2438 Turn the corner(三分查找)

托一个学弟的福,学了一下他的最简便三分写法,然后找了一道三分的题验证了下,AC了一题,写法确实方便,还是我太弱了,漫漫AC路!各路大神,以后你们有啥好的简便写法...

30550
来自专栏ml

java版本的学生管理系统

1 import java.awt.BorderLayout; 2 import java.awt.Color; 3 import java.awt...

79560
来自专栏HansBug's Lab

3891: [Usaco2014 Dec]Piggy Back

3891: [Usaco2014 Dec]Piggy Back Time Limit: 10 Sec  Memory Limit: 128 MB Submit:...

38490
来自专栏Hongten

java编程_socket_套接字_网络编程_简易的GUI多线程聊天程序

===============================================================

14610
来自专栏ml

Java之线程———GUI线程(包含打字游戏和计时器俩个GUI实列)

     当java程序包含图形用户界面(GUI)时,Java虚拟机在运行应用程序时会自动启动更多的线程,其中有两个重要的线程:AWT-EventQuecue ...

61160
来自专栏码匠的流水账

聊聊sentinel的NettyHttpCommandCenter

com/alibaba/csp/sentinel/transport/command/NettyHttpCommandCenter.java

19710

扫码关注云+社区

领取腾讯云代金券