需求:第一個窗口為歡迎 界面 鱼填,點擊按鈕 擒贸,會進入管理系統(tǒng)界面
思路:1.用可視化工具 橘沥,首先實現(xiàn)圖形界面 窗轩,然后實現(xiàn)每個組件的具體功能 ,
2. 每一次執(zhí)行操作之后座咆,對顯示水果列表的組件進行刷新痢艺。
刷新功能
private int update_UI(JPanel panel, Statement stmt, JTextPane textPane, JTextPane textPane_1,
JTextPane textPane_2, JTextPane textPane_3) throws SQLException {
panel.updateUI();//首先將其全部組件進行刷新
panel.add(textPane);
panel.add(textPane_1);
panel.add(textPane_2);
panel.add(textPane_3);//添加上基礎的四個組件
Statement stmt1=stmt;//獲取操作數(shù)據庫的權限
String sql="select * from goods_info";
ResultSet rs=stmt1.executeQuery(sql);//執(zhí)行sql語句,這條語句會返回一個Set集合介陶,里面包含要獲取的信息
int pos=21;
while(rs.next()) {//對其進行迭代
String name=rs.getString("name");
int price=rs.getInt("price");
int id=rs.getInt("id");
String unit=rs.getString("unit");//每一次獲取到完整的四個數(shù)據
System.out.println(id+name+price+"/"+"unit");
JTextPane t1=new JTextPane();//創(chuàng)建臨時按鈕堤舒,設置文本,設置不可編輯
t1.setText(id+"");//填充數(shù)值
t1.setEditable(false);//設置不可被改動
t1.setFont(new Font("宋體", Font.BOLD, 12));
t1.setBorder(BorderFactory.createEtchedBorder());//設置邊框
JTextPane t2=new JTextPane();
t2.setText(name);
t2.setEditable(false);
t2.setFont(new Font("宋體", Font.BOLD, 12));
t2.setBorder(BorderFactory.createEtchedBorder());
JTextPane t3=new JTextPane();
t3.setText(price+"");
t3.setEditable(false);
t3.setFont(new Font("宋體", Font.BOLD, 12));
t3.setBorder(BorderFactory.createEtchedBorder());
JTextPane t4=new JTextPane();
t4.setText(unit+"");
t4.setEditable(false);
t4.setFont(new Font("宋體", Font.BOLD, 12));
t4.setBorder(BorderFactory.createEtchedBorder());
t1.setBounds(0,pos, 100, 20);//X Y? WIDTH HEIGH
panel.add(t1);
t2.setBounds(100,pos, 100, 20);
panel.add(t2);
t3.setBounds(200,pos, 100, 20);
panel.add(t3);
t4.setBounds(300,pos, 100, 20);
panel.add(t4);
pos+=20;
}
return pos-20;
}
刪除功能:
添加功能:
btnNewButton.addMouseListener(new MouseAdapter() {//提交按鈕事件哺呜,比較特殊舌缤,因為不需要刷新,只要在末尾添加即可某残。国撵。大概吧
@Override
public void mouseClicked(MouseEvent e) {
/*
* 當點擊提交按鈕時, 先判斷前四個按鈕 是否都有文本玻墅,
* 如果含有文本介牙,那么就創(chuàng)建 四個不可改變的textPane
* 給它們對應賦值以后,放到主顯示框上
*/
//獲取數(shù)據
String id=textPane1.getText();
String name=textPane2.getText();
String price=textPane3.getText();
String danwei=textPane4.getText();
//判斷是否為空值
if(!id.isEmpty()&&!name.isEmpty()&&!price.isEmpty()&&!danwei.isEmpty()) {//必須用是否為空判斷 ==澳厢?
y_pos+=20;
System.out.println(id+"---"+name+"---"+price+"---"+danwei);
//輸入正確环础,處理數(shù)據1、清空 輸入框的數(shù)據 2剩拢、將數(shù)據存儲到mysql 中3线得、將數(shù)據上傳
textPane1.setText(null);
? ? textPane2.setText(null);
textPane3.setText(null);
textPane4.setText(null);
JTextPane t1=new JTextPane();//創(chuàng)建臨時按鈕,設置文本裸扶,設置不可編輯
t1.setText(id);//填充數(shù)值
t1.setEditable(false);//設置不可被改動
t1.setFont(new Font("宋體", Font.BOLD, 12));
t1.setBorder(BorderFactory.createEtchedBorder());//設置邊框
JTextPane t2=new JTextPane();
t2.setText(name);
t2.setEditable(false);
t2.setFont(new Font("宋體", Font.BOLD, 12));
t2.setBorder(BorderFactory.createEtchedBorder());
JTextPane t3=new JTextPane();
t3.setText(price);
t3.setEditable(false);
t3.setFont(new Font("宋體", Font.BOLD, 12));
t3.setBorder(BorderFactory.createEtchedBorder());
JTextPane t4=new JTextPane();
t4.setText(danwei);
t4.setEditable(false);
t4.setFont(new Font("宋體", Font.BOLD, 12));
t4.setBorder(BorderFactory.createEtchedBorder());
t1.setBounds(0,y_pos, 100, 20);//X Y? WIDTH HEIGH
t2.setBounds(100,y_pos, 100, 20);
t3.setBounds(200,y_pos, 100, 20);
t4.setBounds(300,y_pos, 100, 20);
//將數(shù)據存入MySQL中
String sql="insert into goods_info value("+id+",\""+name+"\","+price+",\""+danwei+"\");";
try {
stmt.execute(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
y_pos=update_UI(panel,stmt,textPane,textPane_1,textPane_2,textPane_3);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
改動功能:
button.addMouseListener(new MouseAdapter() {//改動按鈕事件
@Override
public void mouseClicked(MouseEvent e) {
/*
* 當點擊改動按鈕的時候框都,獲取到改動的值搬素,id值是必須要輸入的呵晨!
* 。熬尺。摸屠。好吧如果只改動一個值那么就要判斷好多次,粱哼,季二,還是全都輸入才能改動吧(其實就是懶)
*
*/
//獲取數(shù)據
String id=textPane5.getText();
String name=textPane6.getText();
String price=textPane7.getText();
String danwei=textPane8.getText();
//判斷是否為空值
if(!id.isEmpty()&&!name.isEmpty()&&!price.isEmpty()&&!danwei.isEmpty()) {//必須用是否為空判斷 ==?
textPane5.setText(null);
? ? textPane6.setText(null);
textPane7.setText(null);
textPane8.setText(null);//首先清除所有的值
//然后執(zhí)行sql語句,將數(shù)據值進行更新
String sql="update goods_info set name=\""+name+"\",price="+price+",unit=\""+danwei+"\" where id="+id;
try {
stmt.execute(sql);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//然后刷新一下就好了
try {
y_pos=update_UI(panel,stmt,textPane,textPane_1,textPane_2,textPane_3);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
難點在于?
1.結合了數(shù)據庫 胯舷,需要不斷嘗試將sql語句轉換成有效的字符串刻蚯,
2.組件之間的互動 ,要明確功能? 和需要的組件 桑嘶。