📄 database.java
字号:
if(i!=0){
list.select(i-1);
selected(list);}
}
else if(source==addbutton||source==additem){//添加
String str;
int f=0;
try{
f=Integer.parseInt(nofield.getText());
if (f<0){
JOptionPane.showMessageDialog(null,"编号不能为负数");
}
else{
str=nofield.getText()+" "+namefield.getText();
if(sexm.isSelected()) //单选按钮选中时,添加标签
str=str+" "+sexm.getText();
if(sexw.isSelected())
str=str+" "+sexw.getText();
str=str+" "+sheng.getSelectedItem(); //获得选择框的选中项
str=str+" "+otherfield.getText()+" ";
list.add(str); //添加列表框选项
nofield.setText(""+(Integer.parseInt(nofield.getText())+1));
}
}catch(NumberFormatException a){JOptionPane.showMessageDialog(null,"编号必须为数字");}
namefield.setText("");otherfield.setText("");
}
else if(source==deletebutton||source==deleteitem){//删除选项
int i;
if(list.getItemCount()==0){
JOptionPane.showMessageDialog(null,"已经没有信息可删");
}
else{
i=list.getSelectedIndex();
if(i!=-1){
list.remove(i);
}
else JOptionPane.showMessageDialog(null,"请选择要删除的信息");
}
}
else if(source==updateitem){//修改选项信息
String str="";
int f=0;
try{
f=Integer.parseInt(nofield.getText());
if (f<0){
JOptionPane.showMessageDialog(null,"编号不能为负数");
}
else{
str=nofield.getText()+" "+namefield.getText();
if(sexm.isSelected()) //单选按钮选中时,添加标签
str=str+" "+sexm.getText();
if(sexw.isSelected())
str=str+" "+sexw.getText();
str=str+" "+sheng.getSelectedItem(); //获得选择框的选中项
str=str+" "+otherfield.getText()+" ";
list.replaceItem(str,list.getSelectedIndex());
}
}catch(NumberFormatException a){JOptionPane.showMessageDialog(null,"编号必须为数字");}
nofield.setText("");namefield.setText("");
}
else if(source==openbutton||source==openitem){//打开文件
JFileChooser chooser=new JFileChooser();//创建JFileChooser对象
chooser.setCurrentDirectory(new File("."));//设置当前目录
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()//设置文件过滤器
{
public boolean accept(File f)//测试是否接受一个文件
{
return f.getName().toLowerCase().endsWith(".dat");
}
public String getDescription()//获取文件描述
{
return "*.dat";
}
});
int r=chooser.showOpenDialog(this);
if(r==JFileChooser.APPROVE_OPTION)//用户接收了一个文件
{
filename=chooser.getSelectedFile().getName();
/*for(int h=0;h<list.getItemCount();h++){
list.remove(h);
}*/
list.clear();
readfile(filename);
textarea.enable(false);
//list.enable(false);
}
}
else if(source==savebutton||source==saveitem){//保存文件
if(filename!="NoName") writefile(filename);//文件存在,直接保存
else{
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".dat");//获得以.dat结尾的文件名
}
public String getDescription()
{
return "*.dat";
}
});
int r=chooser.showSaveDialog(this);
if(r==JFileChooser.APPROVE_OPTION)
{
filename=chooser.getSelectedFile().getName()+".dat";//获取选择的文件名
writefile(filename);
}
}
}
else if(source==saveasitem){ //文件另存为
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".dat");
}
public String getDescription()
{
return "*.dat";
}
});
int r=chooser.showSaveDialog(this);
if(r==JFileChooser.APPROVE_OPTION)
{
filename=chooser.getSelectedFile().getName()+".dat";
writefile(filename);
}
}
else if(source==exititem){//退出
System.exit(0);
}
else if(source==aboutitem){
JOptionPane.showMessageDialog(null,"请联系作者梁恩云!");
}
}
public void selected(List listed){//将列表选项返回
String str="";
String s="";
String st[]=new String[6];
str=listed.getSelectedItem();
char ch[]=new char[str.length()];
ch=str.toCharArray();
int h=0;
for(int j=0;j<str.length();j++){
if(ch[j]!=' ') s+=String.valueOf(ch[j]);
else{
st[h]=s;
s="";
h++;
}
}
nofield.setText(st[0]);
namefield.setText(st[1]);
if(st[2].equals("男")) sexm.setSelected(true);
else sexw.setSelected(true);
sheng.setSelectedItem(st[3]);
otherfield.setText(st[4]);
}
public void writefile(String filename){//写入文件
String str="";
for(int i=0;i<list.getItemCount();i++){
str+=list.getItem(i)+'\n';
}
try{
try{
File file=new File(filename);
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(file)));
out.write(str);
out.close();
}catch(EOFException e){//捕捉在文件写入过程中意外地到达文件尾部异常
System.err.println("end of stream");
}
}
catch(IOException e){
System.out.println("ioexception");
}
}
public void readfile(String filename){//读文件
String str="";
try{
File file=new File(filename);
FileReader in=new FileReader(file);
int size=(int)file.length();
int charsread=0;
char ch[]=new char[size];
while(in.ready())
charsread+=in.read(ch,charsread,size-charsread);
in.close();
textarea.setText(new String(ch,0,charsread));
for(int j=0;j<size;j++){
if(ch[j]!='\n') str+=String.valueOf(ch[j]);
else{
list.add(str);
str="";
//str[i]+=String.valueOf(ch[j+1]);
}
}
}catch(IOException e){
System.out.println("打开文件错误");
}
}
public static void main(String[] args){
LoginWindow login=new LoginWindow();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -