1.首先利用windowbuilder軟件創建好界面
2.創建點擊按鈕要彈出的窗口
3.自動生成代碼(界面代碼無功能)
package com.jxx.ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.ImageIcon;
public class SaveMoney extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SaveMoney frame = new SaveMoney();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SaveMoney() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 359, 254);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u4F1A\u5458\u624B\u673A\u53F7:");
label.setBounds(46, 40, 72, 15);
contentPane.add(label);
textField = new JTextField();
textField.setBounds(129, 40, 141, 21);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u5145\u503C\u91D1\u989D:");
label_1.setBounds(46, 84, 72, 15);
contentPane.add(label_1);
textField_1 = new JTextField();
textField_1.setBounds(129, 84, 141, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel label_2 = new JLabel("\u4FEE\u6539\u7B49\u7EA7:");
label_2.setBounds(46, 123, 72, 15);
contentPane.add(label_2);
JComboBox comboBox = new JComboBox();
comboBox.setBounds(129, 123, 66, 21);
contentPane.add(comboBox);
JButton btnNewButton = new JButton("");
btnNewButton.setIcon(new ImageIcon("D:\\img\\login.png"));
btnNewButton.setBounds(210, 122, 25, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("");
btnNewButton_1.setIcon(new ImageIcon("D:\\img\\remove.png"));
btnNewButton_1.setBounds(245, 122, 25, 23);
contentPane.add(btnNewButton_1);
}
}
4.修改要操作的組件名稱 鼠標右鍵組件 選擇最下方的rename
修改名字要見名知意 輸入框以xxxTet命名 ,選擇框以xxxBox命名,按鈕以xxxBtn命名
將下拉框屬性放在一個字符串數組中,將字符串數組傳入到下拉框
查看效果
5.添加其功能
因爲我們要獲得手機號的值和充值金額的值還有等級的值,所以我們選擇給按鈕sureBtn添加事件
右鍵這個組建
系統自動生成這個代碼
在79行按回車,向其中添加操作
sureBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String sphone=phoneTxt.getText();
String smoney=moneyTxt.getText();//利用getText得到輸入的值
String slv=(String) lvBox.getSelectedItem();//利用getSelectedItem得到下拉框的值
if("".equals(sphone)||sphone==null) {
JOptionPane.showMessageDialog(null, "手機號不能爲空");//固定方法,""裏面加自己想輸入的字符串
return;
}
if("".equals(smoney)||smoney==null) {
JOptionPane.showMessageDialog(null, "收入金額不能爲空");//固定方法,""裏面加自己想輸入的字符串
return;
}
if("".equals(slv)||slv==null) {
JOptionPane.showMessageDialog(null, "會員類型不能爲空");//固定方法,""裏面加自己想輸入的字符串
return;
}
char[] arr=smoney.toCharArray();//將字符串轉化爲字符數組,一個一個字母判斷輸入的金額是否爲數字或者.
for(int i=0;i<arr.length;i++) {
if(arr[i]>'9'||arr[i]<'0') {
JOptionPane.showMessageDialog(null, "輸入金額有誤");//固定方法,""裏面加自己想輸入的字符串
return;
}
}
double money=Double.valueOf(smoney);//確認輸入金額無誤,轉化爲數據庫內類型
try {
Connection con=null;
Statement stm=null;
PreparedStatement pstm=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/cook", "root", "root");
//第一個參數最後一個/後面接數據庫名
String sql="select * from mus where Mphone=?";//sql語句 這裏首先要判斷是否有這個用戶
pstm=con.prepareStatement(sql);//去數據庫查詢
pstm.setString(1, sphone);//第一個參數代表問號序列,第二參數代表賦值爲什麼
rs=pstm.executeQuery();//如果爲查詢操作則rs=pstm.executeQuery()
if(rs.next()) {//如果rs中存在元素代表查找成功,則爲會員進行充值並修改會員類型
String sql1="update mus set Mmoney=Mmoney+?,Mlv=? where Mphone=?";
pstm=con.prepareStatement(sql1);
pstm.setDouble(1,money);
pstm.setString(2, slv);
pstm.setString(3, sphone);
pstm.executeUpdate();//如果爲增刪改則執行詞條語句
}else {//否則查找失敗
JOptionPane.showMessageDialog(null, "無此用戶");//固定方法,""裏面加自己想輸入的字符串
return;
}
con.close();//關閉所有打開的對象
pstm.close();
//stm.close();
rs.close();
JOptionPane.showMessageDialog(null, "充值成功!金額"+money);//固定方法,""裏面加自己想輸入的字符串
return;
}
catch(Exception ep) {
ep.printStackTrace();
}
}
});
樣例顯示無此用戶
這是我數據可之前的數據,現在爲13104041917 季顯欣充值金額100並修改爲黃金會員
大家可以看到數據庫以及實時修改
接下來設置重置按鈕的功能
添加事件步驟如上,系統會自動生成並跳轉到事件
JButton removeBtn = new JButton("");
removeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
phoneTxt.setText("");
moneyTxt.setText("");//利用setText("")方法爲其賦空值
lvBox.setSelectedIndex(0);//因爲JcomboBox沒有setTxt但是可以用setSelectedIndex(0)爲其賦空值
//前提是JcomboBox的初始值第一個爲空
}
});
因爲無法添加視頻,但是這個功能是可以正常實現的
希望通過本博客大家可以有所收穫!