黑马程序员技术交流社区
标题:
gui 简单的计算器
[打印本页]
作者:
fmi110
时间:
2015-9-16 22:34
标题:
gui 简单的计算器
/*
* 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.itcasst.util;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
/**
*
* @author hyning
*/
public class UiUtil {
private UiUtil(){}
//设置窗体的图标,需要接收设置的窗体对象
public static void setFrameImage(JFrame jf){
//获取工具包对象
// public static ToolKit getDefaultToolKit();获取默认的工具包
Toolkit tk = Toolkit.getDefaultToolkit();
//获取图片
// tk.getImage("D:\\JavaGuiProject\\模拟四则运算\\src\\cn\\itcasst\\resource\\jjcc.jpg");
Image i = tk.getImage("src\\cn\\itcasst\\resource\\jjcc.jpg");
//给窗体设置图片
jf.setIconImage(i);
}
//设置窗口居中
public static void setFrameCenter(JFrame jf){
//获取屏幕宽高
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 x = (int)(screenWidth - frameWidth)/2;
int y = (int)(screenHeight - frameWidth)/2;
//设置窗口位置
jf.setLocation(x,y);
}
}
复制代码
作者:
fmi110
时间:
2015-9-16 22:36
/*
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
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2