Java实现有界面的简单的两个整数之间的加减乘除运算
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现有界面的简单的两个整数之间的加减乘除运算相关的知识,希望对你有一定的参考价值。
这个挺简单的,原来练习时老师要求做的,我没做,现在考试忙的要命,懒得去做他,麻烦谁有源代码给我一下(计算结果要显示在文本框内,最好输入运算数时这个数也能显示在文本框,类似Windows自带计算器)。当然了,分不高,原因就是希望谁有直接发过来,不会让你现做,现做当然不值这点分,任何一个程序都不是那么容易的,这个很经典,应该好多人都有,谢谢大家的帮忙!
这个必须通过swing技术实现,先看下效果图:
import java.awt.BorderLayout;import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Jisuanqi extends JFrame implements ActionListener
/**
*
*/
private static final long serialVersionUID = 1L;
Result result = new Result(); // 定义text的面板
Number_Key number_key = new Number_Key(); // 定义按钮面板
// 当点击按钮+、-、*、/时,com = true
boolean com = false;
// 当i=0时说明是我们第一次输入,字符串text不会累加
int i = 0;
// 存放text的内容
String text = "";
// 存放点击按钮+、-、*、/之前的数值
double defbutton = 0;
// +、-、*、/的代号分别为1,2,3,4
int symbol = 0;
// 构造函数
Jisuanqi()
super("WangJiao"); // 设定标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定关闭窗体时退出程序
JPanel pane = new JPanel(); // 定义主面板
pane.setLayout(new BorderLayout());
setBounds(380, 220, 30, 80); // 前两个参数是在屏幕上显示的坐标,后两个是大小
// 替换图标
ImageIcon icon = new ImageIcon("F:1.GIF");
// Jisuanqi.class.getResource("APPLE.GIF")
// );
setIconImage(icon.getImage());
pane.add(result, BorderLayout.NORTH);
pane.add(number_key, BorderLayout.CENTER);
pane.add(number_key.equal, BorderLayout.SOUTH);
number_key.one.addActionListener(this); // 对1按钮添加监听事件
number_key.two.addActionListener(this); // 对2按钮添加监听事件
number_key.three.addActionListener(this); // 对3按钮添加监听事件
number_key.four.addActionListener(this); // 对4按钮添加监听事件
number_key.five.addActionListener(this); // 对5按钮添加监听事件
number_key.six.addActionListener(this); // 对6按钮添加监听事件
number_key.seven.addActionListener(this); // 对7按钮添加监听事件
number_key.eight.addActionListener(this); // 对8按钮添加监听事件
number_key.nine.addActionListener(this); // 对9按钮添加监听事件
number_key.zero.addActionListener(this); // 对0按钮添加监听事件
number_key.ce.addActionListener(this); // 对置零按钮添加监听事件
number_key.plus.addActionListener(this); // 对+按钮添加监听事件
number_key.equal.addActionListener(this); // 对=按钮添加监听事件
number_key.sub.addActionListener(this); // 对-按钮添加监听事件
number_key.mul.addActionListener(this); // 对*按钮添加监听事件
number_key.div.addActionListener(this); // 对/按钮添加监听事件
number_key.point.addActionListener(this); // 对.按钮添加监听事件
setContentPane(pane);
pack(); // 初始化窗体大小为正好盛放所有按钮
// 各个按钮触发的事件
public void actionPerformed(ActionEvent e)
/*
* 如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true 如果没有com=
* false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
*/
if (e.getSource() == number_key.one)
if (com || i == 0)
result.text.setText("1");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "1");
else if (e.getSource() == number_key.two)
if (com || i == 0)
result.text.setText("2");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "2");
else if (e.getSource() == number_key.three)
if (com || i == 0)
result.text.setText("3");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "3");
else if (e.getSource() == number_key.four)
if (com || i == 0)
result.text.setText("4");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "4");
else if (e.getSource() == number_key.five)
if (com || i == 0)
result.text.setText("5");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "5");
else if (e.getSource() == number_key.six)
if (com || i == 0)
result.text.setText("6");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "6");
else if (e.getSource() == number_key.seven)
if (com || i == 0)
result.text.setText("7");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "7");
else if (e.getSource() == number_key.eight)
if (com || i == 0)
result.text.setText("8");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "8");
else if (e.getSource() == number_key.nine)
if (com || i == 0)
result.text.setText("9");
com = false;
i = 1;
else
text = result.text.getText();
result.text.setText(text + "9");
/*
* 对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是
* 如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添 加0;否则保持原来的数值不变
*/
else if (e.getSource() == number_key.zero) // result.text.getText()是得到text里内容的意思
if (com || i == 0)
result.text.setText("0");
com = false;
i = 1;
else
text = result.text.getText();
if (Float.parseFloat(text) > 0 || Float.parseFloat(text) < 0) // Float.parseFloat(text)就是类型转换了,下面都是一样
result.text.setText(text + "0");
else
if (text.trim().indexOf(".") == -1)
result.text.setText(text);
else
result.text.setText(text + "0");
else if (e.getSource() == number_key.ce)
result.text.setText("0");
i = 0;
com = true;
// text = "";
defbutton = 0;
/*
* 本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加, 不存在就添加.
*/
else if (e.getSource() == number_key.point)
if (com || i == 0)
result.text.setText("0.");
com = false;
i = 1;
else
text = result.text.getText();
if (text.trim().indexOf(".") == -1)
result.text.setText(text + ".");
else
result.text.setText(text);
// 获得点击+之前的数值
else if (e.getSource() == number_key.plus)
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 1;
// 获得点击-之前的数值
else if (e.getSource() == number_key.sub)
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 2;
// 获得点击*之前的数值
else if (e.getSource() == number_key.mul)
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol = 3;
// 获得点击/之前的数值
else if (e.getSource() == number_key.div)
com = true;
i = 0;
defbutton = Double.parseDouble(result.text.getText());
symbol = 4;
else if (e.getSource() == number_key.equal)
switch (symbol)
case 1: // 计算加法
double ad = defbutton
+ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
case 2: // 计算减法
double ad = defbutton
- Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i = 0;
text = "";
break;
case 3: // 计算乘法
double ad = defbutton
* Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
case 4: // 计算除法
double ad = defbutton
/ Double.parseDouble(result.text.getText());
result.text.setText(ad + "");
i = 0;
text = "";
break;
System.out.println(com);
System.out.println(result.text.getText());
@SuppressWarnings("deprecation")
public static void main(String[] args)
Jisuanqi loveyou = new Jisuanqi();
loveyou.show();
// 计算器数字按钮定义面板
class Number_Key extends JPanel
/**
*
*/
private static final long serialVersionUID = 1L;
JButton zero = new JButton("0"); // 数字键0
JButton one = new JButton("1"); // 数字键1
JButton two = new JButton("2"); // 数字键2
JButton three = new JButton("3"); // 数字键3
JButton four = new JButton("4"); // 数字键4
JButton five = new JButton("5"); // 数字键5
JButton six = new JButton("6"); // 数字键6
JButton seven = new JButton("7"); // 数字键7
JButton eight = new JButton("8"); // 数字键8
JButton nine = new JButton("9"); // 数字键9
JButton plus = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");
JButton equal = new JButton("=");
JButton ce = new JButton("清零"); // 置零键
JButton point = new JButton(".");
Number_Key()
setLayout(new GridLayout(4, 4, 1, 1)); // 定义布局管理器为网格布局
setBackground(Color.blue); // 设置背景颜色
// 添加按钮
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
add(zero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
// 计算器显示结果的窗体
class Result extends JPanel
/**
*
*/
private static final long serialVersionUID = 1L;
// text先是输入和结果
JTextField text = new JTextField("0");
@SuppressWarnings("deprecation")
Result() // 讲输入的数字或得到的结果在text的右边显示
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.enable(false); // 文本框不能编辑
setLayout(new BorderLayout()); // 设定布局管理器边框布局
add(text, BorderLayout.CENTER); // text放置在窗体的中间
参考技术A 共两个类,已经测试,满足要求了。
Calculator类:
public class Calculator
private String result = "0";
private int op = 0, add = 1, sub = 2, mul = 3, div = 4;
private double stringToDouble(String x)
double y = Double.parseDouble(x);
return y;
private void operate(String x)
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op)
case 0:
result = x;
break;
case 1:
result = String.valueOf(y + x1);
break;
case 2:
result = String.valueOf(y - x1);
break;
case 3:
result = String.valueOf(y * x1);
break;
case 4:
if (x1 != 0)
result = String.valueOf(y / x1);
else
result = "The divisor can't be zero!";
break;
public String opAdd(String x)
operate(x);
op = add;
return result;
public String opSubtract(String x)
operate(x);
op = sub;
return result;
public String opMultiply(String x)
operate(x);
op = mul;
return result;
public String opDivide(String x)
operate(x);
op = div;
return result;
public String opEquals(String x)
operate(x);
op = 0;
return result;
public void opClean()
op = 0;
result = "0";
GUI界面类:
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI
private Frame f;
private Panel p1, p2;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button bPoint, bAdd, bDec, bMul, bDiv, bCal;
private TextField tf;
private String s, op;
private Calculator cal = new Calculator();
private boolean ifOp;
public CalculatorGUI()
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");
tf = new TextField(25);
tf.setEditable(false);
public void launchFrame()
f.setSize(220, 160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1, BorderLayout.NORTH);
p2.setLayout(new GridLayout(4, 4));
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
f.add(p2, BorderLayout.SOUTH);
f.setVisible(true);
public void setTextFieldText_Temp()
if (tf.getText().length() < 15 && (tf.getText().indexOf(".") == -1 || !s.equals(".")))
tf.setText(tf.getText() + s);
else
tf.setText((tf.getText() + s).substring(0, 15));
public void setTextFieldText()
if (ifOp)
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
else
setTextFieldText_Temp();
public static void main(String[] args)
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
class myWindowListener extends WindowAdapter
@Override
public void windowClosing(WindowEvent e)
System.exit(0);
class setLabelText_ActionListener implements ActionListener
public void actionPerformed(ActionEvent e)
Button tempB = (Button) e.getSource();
s = tempB.getLabel();
setTextFieldText();
class setOperator_ActionListener implements ActionListener
public void actionPerformed(ActionEvent e)
Button tempB = (Button) e.getSource();
op = tempB.getLabel();
if (op.equals("+"))
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
else if (op.equals("-"))
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
else if (op.equals("*"))
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
else if (op.equals("/"))
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
else if (op.equals("="))
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
本回答被提问者采纳
Java实现超市管理系统(含数据库)
作者:AiY.. 来源:https://blog.csdn.net/qq_44859533
序言: 学Java已经有几个月了,上一次总结是针对GUI界面写的简易计算器,实现加减乘除功能以及计算器标准型和科学型之间的转换,有兴趣可以看看 Java混合计算器以及界面切换 . 这次写的超市管理系统,实现的功能有账户的注册、登录,超市商品类别的添加、修改和删除以及商品的添加、修改和删除的功能。用户注册之后把注册信息导入数据库;用户登录时候查询用户表,方可登录进去;商品类别和商品的增加也如注册信息一样,把信息导入商品类别表和商品表。由于快期末考试了,超市管理系统的一些功能还没有完善,后续还会更新顾客登录超市系统后,只能拥有查询商品的权限,并且还可以实现购买商品的功能,哈~哈 ~…
1、首先是建立数据库表:
2、实现主页面:
3、实现用户注册:
核心代码:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Register().setVisible(true);
}
});
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name=this.jTextField1.getText();
String age=this.jTextField3.getText();
String QQ=this.jTextField4.getText();
String userName=this.jTextField5.getText();
String password1=this.jPasswordField1.getText();
String password2=this.jPasswordField2.getText();
if(StringUtil.isEmpty(name)){
JOptionPane.showMessageDialog(null,"姓名不能为空");
return;
}
if(StringUtil.isEmpty(age)){
JOptionPane.showMessageDialog(null,"年龄不能为空");
return;
}
if(StringUtil.isEmpty(QQ)){
JOptionPane.showMessageDialog(null,"QQ不能为空");
return;
}
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null,"注册账号不能为空");
return;
}
if(StringUtil.isEmpty(password1)){
JOptionPane.showMessageDialog(null,"注册密码不能为空");
return;
}
if(StringUtil.isEmpty(password2)){
JOptionPane.showMessageDialog(null,"确认密码不能为空");
return;
}
if(!password1.equals(password2)){
JOptionPane.showMessageDialog(null,"两个密码填写不一致");
return;
}
User user=new User(userName,password1);
Connection con=null;
try{
con=dbUtil.getCon();
int n=userDao.add(con,user);
if(n==1){
JOptionPane.showMessageDialog(null,"用户注册成功!");
}else{
JOptionPane.showMessageDialog(null,"注册失败!!");
}
}catch (Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"注册失败!!");
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
//上一步
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
new HomePage().setVisible(true);
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
注册成功之后可以返回上一步,重新登录
4、实现用户登录:
核心代码:
//重置
private void resetValueActionPerformed(ActionEvent evt) {
this.userNameTxt.setText("");
this.passwordTxt.setText("");
}
//登录事件处理
private void loginActionPerformed(ActionEvent evt) {
String userName=this.userNameTxt.getText();
String password=new String(this.passwordTxt.getPassword());
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null,"用户名不能为空!");
return;
}
if(StringUtil.isEmpty(password)){
JOptionPane.showMessageDialog(null,"密码不能为空");
return;
}
User usr=new User(userName,password);
Connection con=null;
try{
con=dbUtil.getCon();
User currentUser=userDao.login(con,usr);
if(currentUser!=null){
dispose();
new MainFrm().setVisible(true);
}else{
JOptionPane.showMessageDialog(null,"登录失败,用户名密码错误!");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
12345678910111213141516171819202122232425262728293031323334353637383940
接下来是商品类别和商品的实现,主要是在JFrame窗口中加入Inter窗口
5、主菜单之关于:
核心代码:
6、主菜单之商品类别管理:
核心代码:
private void jButton1ActionPerformed(ActionEvent evt) {
String goodsTypeName=this.goodsTypeNameTxt.getText();
String goodsTypeDesc=this.goodsTypeDescTxt.getText();
if(StringUtil.isEmpty(goodsTypeName)){
JOptionPane.showMessageDialog(null,"商品类别不能为空");
return;
}
GoodsType goodsType=new GoodsType(goodsTypeName,goodsTypeDesc);
Connection con=null;
try{
con=dbUtil.getCon();
int n=goodsTypeDao.add(con,goodsType);
if(n==1){
JOptionPane.showMessageDialog(null,"商品类别添加成功!");
jButton2ActionPerformed(evt);
}else{
JOptionPane.showMessageDialog(null,"添加失败!!");
}
}catch (Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"添加失败!!");
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
//重置
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.goodsTypeNameTxt.setText("");
this.goodsTypeDescTxt.setText("");
}
123456789101112131415161718192021222324252627282930313233343536
//表格行点击事件
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int row=jTable1.getSelectedRow();//获取行数
idTxt.setText((String)jTable1.getValueAt(row,0));
goodsTypeNameTxt.setText((String)jTable1.getValueAt(row,1));
goodsTypeDescTxt.setText((String)jTable1.getValueAt(row,2));
}
//鼠标单击后,删除
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String id=idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null,"Are you 确定delete this记录?");
if(n==0){
Connection con=null;
try{
con=dbUtil.getCon();
int deleteNum=goodsTypeDao.delect(con,id);
//System.out.println(deleteNum);
if(deleteNum==1){
JOptionPane.showMessageDialog(null,"删除成功");
this.resetValue();
this.fillTable(new GoodsType());
}else{
JOptionPane.showMessageDialog(null,"删除失败");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
//鼠标单击后,修改
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String id=idTxt.getText();
String goodsTypeName=goodsTypeNameTxt.getText();
String goodsTypeDesc=goodsTypeDescTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择要修改的记录");
return;
}
GoodsType goodsType=new GoodsType(Integer.parseInt(id),goodsTypeName,goodsTypeDesc);
Connection con=null;
try{
con=dbUtil.getCon();
int modifyNum=goodsTypeDao.update(con,goodsType);
if(modifyNum==1){
JOptionPane.showMessageDialog(null,"修改成功");
this.resetValue();
this.fillTable(new GoodsType());
}else{
JOptionPane.showMessageDialog(null,"修改失败");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
//查询商品类型
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String t_goodsTypeName=this.jTextField2.getText();
GoodsType goodsType=new GoodsType();
goodsType.setGoodsTypeName(t_goodsTypeName);
this.fillTable(goodsType);
}
//插入商品类型
private void fillTable(GoodsType goodsType){
DefaultTableModel dtm=(DefaultTableModel)jTable1.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,goodsType);
while(rs.next()){
Vector v=new Vector();
v.add(rs.getString("id"));
v.add(rs.getString("goodsTypeName"));
v.add(rs.getString("goodsTypeDesc"));
dtm.addRow(v);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
//修改后重置
private void resetValue(){
this.idTxt.setText("");
this.goodsTypeNameTxt.setText("");
this.goodsTypeDescTxt.setText("");
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
7、主菜单之商品管理:
核心代码:
//查询
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String t_goodsName=this.s_goodsNameTxt.getText();
Goods goods=new Goods();
goods.setGoodsName(t_goodsName);
this.fillTable(goods);
}
//下拉框
private void fillGoodsType(String type){
Connection con=null;
GoodsType goodsType=null;
try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,new GoodsType());
if("search".equals(type)){
goodsType=new GoodsType();
goodsType.setGoodsTypeName("请选择");
goodsType.setId(-1);
this.s_goodsJcb.addItem(goodsType);
}
while(rs.next()){
goodsType=new GoodsType();
goodsType.setGoodsTypeName(rs.getString("goodsTypeName"));
goodsType.setId(rs.getInt("id"));
if("search".equals(type)){
this.s_goodsJcb.addItem(goodsType);
}else if("modify".equals(type)){
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
//表
private void fillTable(Goods goods){
DefaultTableModel dtm=(DefaultTableModel) jTable1.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=dbUtil.getCon();
ResultSet rs=goodsDao.list(con,goods);
while(rs.next()){
Vector v=new Vector();
v.add(rs.getInt("id"));
v.add(rs.getString("goodsName"));
v.add(rs.getFloat("price"));
v.add(rs.getString("goodsDesc"));
dtm.addRow(v);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
//鼠标点击表格事件
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int row=jTable1.getSelectedRow();
this.idTxt.setText((Integer)jTable1.getValueAt(row,0)+"");
this.goodsNameTxt.setText((String)jTable1.getValueAt(row,1));
this.priceTxt.setText((Float)jTable1.getValueAt(row,2)+"");
this.goodsDescTxt.setText((String)jTable1.getValueAt(row,3));
}
//鼠标单击后,修改
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String id=this.idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择要修改的记录");
return;
}
String goodsName=this.goodsNameTxt.getText();
String price=this.priceTxt.getText();
String goodsDesc=this.goodsDescTxt.getText();
if(StringUtil.isEmpty(goodsName)){
JOptionPane.showMessageDialog(null,"商品名称不能为空");
return;
}
if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null,"商品价钱不能为空");
return;
}
if(StringUtil.isEmpty(goodsDesc)){
JOptionPane.showMessageDialog(null,"商品描述不能为空");
return;
}
Goods goods=new Goods(Integer.parseInt(id),goodsName, Float.parseFloat(price),goodsDesc);
Connection con=null;
try{
con=dbUtil.getCon();
int modifyNum=goodsDao.update(con,goods);
if(modifyNum==1){
JOptionPane.showMessageDialog(null,"修改成功");
this.resetValue();
this.fillTable(new Goods());
}else{
JOptionPane.showMessageDialog(null,"修改失败");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
//鼠标单击后删除
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String id=idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null,"Are you 确定delete this记录?");
if(n==0){
Connection con=null;
try{
con=dbUtil.getCon();
int deleteNum=goodsDao.delete(con,id);
if(deleteNum==1){
JOptionPane.showMessageDialog(null,"删除成功");
this.resetValue();
this.fillTable(new Goods());
}else{
JOptionPane.showMessageDialog(null,"删除失败");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
//修改后重置
private void resetValue(){
this.idTxt.setText("");
this.goodsNameTxt.setText("");
this.goodsDescTxt.setText("");
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
private void jButton2ActionPerformed(ActionEvent evt) {
this.resetValue();
}
//商品添加
private void jButton1ActionPerformed(ActionEvent evt){
String goodsName=this.goodsNameTxt.getText();
String price=this.priceTxt.getText();
String goodsDesc=this.goodsDecTxt.getText();
if(StringUtil.isEmpty(goodsName)){
JOptionPane.showMessageDialog(null,"商品类别名称不能为空");
return;
}
if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null,"商品价格不能为空");
return;
}
GoodsType goodsType=(GoodsType)goodsTypeJcb.getSelectedItem();
int goodsTypeId=goodsType.getId();
Goods goods=new Goods(goodsName,Float.parseFloat(price),goodsDesc);
Connection con=null;
try{
con=dbUtil.getCon();
int addNum=goodsDao.add(con,goods);
if(addNum==1){
JOptionPane.showMessageDialog(null,"商品添加成功");
resetValue();
}else{
JOptionPane.showMessageDialog(null,"商品添加失败");
}
}catch (Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"商品添加失败");
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
* 重置表单
*/
private void resetValue(){
this.goodsNameTxt.setText("");
this.priceTxt.setText("");
this.goodsDecTxt.setText("");
if(this.goodsTypeJcb.getItemCount()>0){
this.goodsTypeJcb.setSelectedIndex(0);
}
}
//初始化商品类别下拉框
private void fillGoodsType(){
Connection con=null;
GoodsType goodsType=null;
try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,new GoodsType());
while(rs.next()){
goodsType=new GoodsType();
goodsType.setId(rs.getInt("id"));
goodsType.setGoodsTypeName(rs.getString("goodsTypeName"));
this.goodsTypeJcb.addItem(goodsType);
}
}catch (Exception e){
e.printStackTrace();
}finally {
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
8、退出:
2. 回复关键词:统计思维导图
以上是关于Java实现有界面的简单的两个整数之间的加减乘除运算的主要内容,如果未能解决你的问题,请参考以下文章
用java设计一个计算器 1.可以没有界面,但要有输入和输出 2.整数,小数,加减乘除,求余,括号