📄 kc_sj.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Kc_Sj extends WindowAdapter implements ActionListener,ItemListener
{
Frame f; //定义组件
TextField tf1,tf2,tf3;
Checkbox cb1,cb2;
Choice c1,c2;
List ls1;
File file1 =null;
Button b1,b2,b3,b4,b5,b6,b7,b8;
FileDialog fd1;
String str;
public void display() //创建并初始化各个组件
{
Panel p1,p2,p3; //创建组件
CheckboxGroup cg;
f = new Frame("Student Information");
f.setSize(500,320);
f.setLocation(200,100);
f.setBackground(Color.lightGray);
f.setLayout(new GridLayout(1,2)); //网格布局,使用左右分隔窗口
ls1 = new List(); //创建列表框
ls1.addItemListener(this);
p1 = new Panel(); //占据窗口左半部分
p1.setLayout(new GridLayout(7,1)); //网格布局,7行1列
p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p3 = new Panel();
p3.setLayout(new GridLayout(2,4)); //网格布局 2行4列
p3.setBackground(Color.lightGray);
tf1 = new TextField ("1");
tf2 = new TextField ("姓名");
tf3 = new TextField (" ");
cg = new CheckboxGroup(); //创建复选框组
cb1 = new Checkbox("Male",cg,true); //创建单选按钮
cb2 = new Checkbox("Female",cg,false);
c1 = new Choice(); //创建选择框
c1.addItem("江苏省"); //添加选择框的选项
c1.addItem("浙江省");
c1.addItemListener(this); //注册选择框事件监听程序
c2 = new Choice(); //创建选择框
c2.addItem("南京");
b1 = new Button("First");
b2 = new Button("Prior");
b3 = new Button("Next");
b4 = new Button("Last");
b5 = new Button("Add");
b6 = new Button("Delete");
b7 = new Button("Open");
b8 = new Button("Save");
b1.addActionListener(this); //注册按钮事件监听程序
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
f.add(ls1);
f.add(p1);
p1.add(tf1);
p1.add(tf2);
p1.add(tf3);
p2.add(cb1);
p2.add(cb2);
p1.add(p2);
p1.add(c1);
p1.add(c2);
p3.add(b1);
p3.add(b2);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
p1.add(p3);
f.addWindowListener(this); //组件依次添加到面板p1上
f.setVisible(true);
}
public void windowClosing(WindowEvent e) //关闭件面
{
System.exit(0);
}
public void actionPerformed(ActionEvent e) // 事件动作
{
if ((e.getSource() == b1)&&(ls1.getSelectedIndex() > 0)) //First 功能
handleFirst(ls1.getSelectedIndex()); //调用函数实现
if ((e.getSource() == b2)&&(ls1.getSelectedIndex() > 0))
handlePrior(ls1.getSelectedIndex());
if ((e.getSource() == b3)&&(ls1.getSelectedIndex() >= 0))
handleNext(ls1.getSelectedIndex());
if (e.getSource()==b4)
handleLast();
if (e.getSource()==b5) //单击Add按钮时
handleAdd();
if ((e.getSource() == b6)&&(ls1.getSelectedIndex() >= 0))
handleDelete(ls1.getSelectedIndex());
if (e.getSource()==b7) //单击[打开]按钮时
handleOpen();
if (e.getSource()==b8)
{if ((e.getSource()==b8)&&(file1==null))
handleSave();
else save(file1);
}
}
private void handleFirst(int pos) //实现按钮"First"的功能
{
ls1.select(0);
show();
}
private void handlePrior(int pos) //实现按钮"Prior"的功能
{
ls1.select(pos-1);
show();
}
private void handleNext(int pos) //实现按钮"Next"的功能
{
if (pos < ls1.getItemCount()-1)
{
ls1.select(pos+1);
show();
}
}
private void handleLast() //实现按钮"Last"的功能
{
int index = ls1.getItemCount();
ls1.select(index -1);
show();
}
private void handleAdd() //实现按钮"Add"的功能
{
String str;
str = tf1.getText()+" "+tf2.getText();
if (cb1.getState()) //单选按钮选中时,添加标签
str = str +" "+cb1.getLabel();
if (cb2.getState())
str = str +" "+cb2.getLabel();
str = str+" "+c1.getSelectedItem(); //获得选择框的选中项
str = str+" "+c2.getSelectedItem();
ls1.add(str); //添加列表框选项
tf1.setText(""+(Integer.parseInt(tf1.getText())+1));//编号自加1
}
private void handleDelete(int pos) //实现上面按钮 b6 "Delete" 的功能
{
ls1.remove(pos);
ls1.select(pos);
}
private void handleOpen() //实现上面按钮 b7 "Open"的功能
{
fd1 = new FileDialog(f,"Open",FileDialog.LOAD);
fd1.setVisible(true); //创建并显示打开文件对话框
if((fd1.getDirectory()!=null)&&(fd1.getFile()!=null))
{
tf3.setText(fd1.getDirectory() + fd1.getFile());
try
{
file1 = new File(fd1.getDirectory(),fd1.getFile());
FileInputStream fin = new FileInputStream(file1);
ObjectInputStream in = new ObjectInputStream(fin);
String str;
ls1.removeAll(); //打开之前清除所有选项
while(!(str = (String)in.readObject()).equals(""))
ls1.addItem(str);
in.close();
}
catch (FileNotFoundException fe){}
catch (IOException ioe){}
catch (ClassNotFoundException ioe) {}
}
}
private void handleSave() //实现按钮"Save"的功能
{
fd1=new FileDialog(f,"Save",FileDialog.SAVE);
if(file1==null);
{fd1.setFile("Edit1.dat");}
fd1.setVisible(true); //创建并显示保存文件对话框
if ((fd1.getDirectory()!=null)&&(fd1.getFile()!=null))
{
tf3.setText(fd1.getDirectory()+fd1.getFile());
file1 = new File(fd1.getDirectory(),fd1.getFile());
save(file1);
}
}
private void save(File file1)
{
try
{
FileOutputStream fout = new FileOutputStream(file1);
ObjectOutputStream out = new ObjectOutputStream(fout);
for (int i=0;i<ls1.getItemCount();i++)
out.writeObject(ls1.getItem(i));
out.close();
}
catch(FileNotFoundException fe)
{System.err.println(fe);}
catch(IOException ioe)
{System.out.println(ioe);}
}
public void itemStateChanged(ItemEvent e)
{
int bz=0; //定义一个标志变量
if(e.getSource()==c1) //对选择框操作
{
if (c1.getSelectedIndex()==0)
bz=1;
if (c1.getSelectedIndex()==1)
bz=2;
}
if(e.getSource()==ls1)
bz=3;
itemStateChanged(bz); //调用itemStateChanged(int bz)函数
}
public void itemStateChanged(int bz)
{
if(bz==1)
{
c2.removeAll(); //清除选择框c2全部内容
c2.addItem("南京"); //选择框c2添加内容
c2.addItem("苏州");
c2.addItem("无锡");
c2.addItem("徐州");
}
if (bz==2)
{
c2.removeAll();
c2.addItem("杭州");
c2.addItem("宁波");
c2.addItem("温州");
}
if (bz==3)
{
show();
}
}
private void show()
{
String Null,strall,bh,strn,xm,strx,xb,strs,sf,strc;
int xmw,bhw,xbw,sfh;
Null=" ";
//对应编号
strall=ls1.getSelectedItem(); //总字符穿
bhw=strall.indexOf(Null); //编号后的地一个空个
bh=strall.substring(0,bhw); //返回编号
tf1.setText(bh); //写入编号
//对应姓名
strn=strall.substring(bhw+2); //以姓名为首的字符串
xmw=strn.indexOf(Null); //姓名后的第一个空格
xm=strn.substring(0,xmw); //返回姓名
tf2.setText(xm); //写入姓名
//对应性别
strx=strn.substring(xmw+2); //以性别为首的字符串
xbw=strx.indexOf(Null); //性别后的地一个空个
xb=strx.substring(0,xbw); //取出性别
if(xb.equals("男"))
cb1.setState(true);
else
{
cb2.setState(true);
}
//对省份列表框
strs=strx.substring(xbw+2); //以省份为首的字符串
sfh=strs.indexOf(Null); //省份后的第一个空格
sf=strs.substring(0,sfh); //取出省份
if (sf.equals("江苏省"))
{
c1.select("江苏省");
itemStateChanged(1);
}
if (sf.equals("浙江省"))
{
c1.select("浙江省");
itemStateChanged(2);
}
//对城市
strc=strs.substring(sfh+2); //城市字符串
if (sf.equals("江苏省"))
{
if (strc.equals("南京"))
c2.select("南京");
if (strc.equals("苏州"))
c2.select("苏州");
if (strc.equals("无锡"))
c2.select("无锡");
if (strc.equals("徐州"))
c2.select("徐州");
}
if (sf.equals("浙江省"))
{
if (strc.equals("杭州"))
c2.select("杭州");
if (strc.equals("宁波"))
c2.select("宁波");
if (strc.equals("温州"))
c2.select("温州");
}
}
public static void main(String arg[]) //主函数 main
{
(new Kc_Sj()).display();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -