- /*
-
- package cn.itcasst.view;
- import cn.itcasst.util.UiUtil;
- import javax.swing.JOptionPane;
- /**
- *
- * @author hyning
- */
- 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);
- }
-
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- 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[] { "+", "-", "*", "/" }));
- selectOperator.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- selectOperatorActionPerformed(evt);
- }
- });
- 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()
- .addGap(25, 25, 25)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(firstNumber))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
- .addComponent(selectOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(32, 32, 32)
- .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(18, 18, 18)
- .addComponent(jLabel4)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(42, 42, 42)
- .addComponent(jLabel3))
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton1)
- .addComponent(resultNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(21, 21, 21)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(jLabel2)
- .addComponent(jLabel3))
- .addGap(18, 18, 18)
- .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))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
- .addComponent(jButton1)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void selectOperatorActionPerformed(java.awt.event.ActionEvent evt) {
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- String regex = "\\d+";
- //获取第一个操作数
- String firstNumberString = this.firstNumber.getText().trim();
- //获取运算符
- String selectOperator = this.selectOperator.getSelectedItem().toString();
- //获取第二个操作数
- String secondNumberString = this.secondNumber.getText().trim();
- //校验数据
- if (!firstNumberString.matches(regex)) {
- JOptionPane.showMessageDialog(this, "第一个操作数必须是数字");
- this.firstNumber.setText("");
- this.firstNumber.requestFocus();
- return;
- }
- if (!secondNumberString.matches(regex)) {
- JOptionPane.showMessageDialog(this, "第二个操作数必须是数字");
- this.secondNumber.setText("");
- this.secondNumber.requestFocus();
- return;
- }
- //计算结果
- int resultNumber = 0;//定义变量接收结果
- int first = Integer.parseInt(firstNumberString);
- int second = Integer.parseInt(secondNumberString);
- switch (selectOperator) {
- case "+":
- resultNumber = first + second;
- break;
- case "-":
- resultNumber = first - second;
- break;
- case "*":
- resultNumber = first * second;
- break;
- case "/":
- if (second == 0) {
- JOptionPane.showMessageDialog(this, "除数不能为0");
- this.secondNumber.setText("");
- this.secondNumber.requestFocus();
- return;
- }
- resultNumber = first / second;
- break;
- }
- //结果复制给结果框
- this.resultNumber.setText(String.valueOf(resultNumber));
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
-
- 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);
- }
- //</editor-fold>
- /* 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.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
- }
复制代码 |