?? modify.java
字號:
/*修改學生信息程序Modify.java*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Modify implements ActionListener
{ JTextField [] value=new JTextField[6];
JLabel [] fieldName=new JLabel[6];
JButton modifyButton,queryButton,nextButton;
JPanel panel1,panel2,panel3;
JTextField studentNo=new JTextField(11); //記錄輸入的學號
StudentManager server;
public Modify(StudentManager server,JPanel content)
{ this.server=server;
content.setLayout(new GridLayout(2,1));
fieldName[0]= new JLabel("學號");
fieldName[1]= new JLabel("姓名");
fieldName[2]= new JLabel("出生年月");
fieldName[3]= new JLabel("性別");
fieldName[4]= new JLabel("入學成績");
fieldName[5]= new JLabel("備注");
for(int i=0;i<6; i++) value[i] = new JTextField(10);
queryButton = new JButton("查詢");
nextButton = new JButton("不修改---下一個");
modifyButton = new JButton("修改后保存");
panel1=new JPanel(new GridLayout(1,3)); //創建窗格對象…
panel2=new JPanel(new GridLayout(2,6));
panel3=new JPanel(new GridLayout(1,2));
panel1.add(new JLabel("輸入要修改內容信息的學生的學號:")); //將構件加入窗格……
panel1.add(studentNo);
panel1.add(queryButton);
for(int i=0;i<6; i++) panel2.add(fieldName[i]);
for(int i=0;i<6; i++) panel2.add(value[i]);content.add(panel1);
panel3.add(nextButton);
panel3.add(modifyButton);
content.add(panel1); //將窗格加入用戶界面(框架容器)……
content.add(panel2);
content.add(panel3);
panel2.setVisible(false); //隱藏panel2
panel3.setVisible(false); //隱藏panel3
queryButton.addActionListener(this); //注冊按鈕的監聽對象……
modifyButton.addActionListener(this);
nextButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) //實現單擊按鈕的事件方法
{ Object obj = evt.getSource();
try
{if(obj==queryButton) //查看要修改的記錄
{
String sqlstr="select * from student_login where 學號='"+studentNo.getText()+"'";
Vector rs=server.query(sqlstr);
if(rs.size()>0)
{ for(int i=0; i<value.length; i++)
value[i].setText((String)rs.get(i));//顯示字段項
panel2.setVisible(true);
panel3.setVisible(true);
}
else
{ JOptionPane.showMessageDialog(null,"無此學號,請重新輸入!","提示信息",JOptionPane.PLAIN_MESSAGE);}
}
else
{if(obj==modifyButton) //單擊了修改按鈕
{ String modiStr="update student_login set 學號=?,姓名=?,出生年月=?,性別=?,入學成績=?,備注=? where 學號=?";
String[] mvalues=new String[7];
for(int i=0; i<value.length; i++) mvalues[i]=value[i].getText();
mvalues[6]= studentNo.getText();
int n=server.modify(modiStr,mvalues);
String mes="遇到操作錯誤,記錄修改失敗!!!";
if(n==1) mes="記錄已被成功修改!!!";
JOptionPane.showMessageDialog(null,mes,"操作提示信息",JOptionPane.PLAIN_MESSAGE);
}
panel2.setVisible(false);
panel3.setVisible(false);
}
}
catch(Exception e) { System.out.println("Error:"+e); }
} //事件方法結束
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -