📄 julia.java
字号:
import java.awt.*; //AWT核心包,包括组件类、事件类
import java.awt.event.*;//AWT核心包,包括组件类、事件类
import java.io.*;//输入输出流的文件包
public class Julia extends WindowAdapter implements ActionListener,ItemListener
{
Frame f;//声明对象
TextField tf1,tf2,tf3;
Checkbox cb1,cb2;
Choice c1,c2;
List Is1;
Button b1,b2,b3,b4,b5,b6,b7,b8;
File file=null;
public static void main(String arg[])
{
(new Julia()).display();
}
public void display()
{
Panel p1,p2,p3;
CheckboxGroup cg;
f=new Frame("Input student");//实例化对象
f.setSize(480,350);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.setLayout(new GridLayout(1,2));
Is1=new List();
f.add(Is1);
p1=new Panel();
p1.setLayout(new GridLayout(7,1));
p3=new Panel();
p3.setLayout(new GridLayout(2,4));
f.add(p1);
tf1=new TextField("1");
tf2=new TextField("Name");
tf3=new TextField("FileName:null");
cg=new CheckboxGroup();
cb1=new Checkbox("男",cg,true);
cb2=new Checkbox("女",cg,false);
c1=new Choice();
c1.addItem("江苏省");
c1.addItem("浙江省");
c1.addItem("湖南省");
c1.addItemListener(this);
c2=new Choice();
c2.addItem("南京");
b1=new Button("第一页");
b1.addActionListener(this);
b2=new Button("前一页");
b2.addActionListener(this);
b3=new Button("下一页");
b3.addActionListener(this);
b4=new Button("最后一页");
b4.addActionListener(this);
b5=new Button("添加");
b5.addActionListener(this);
b6=new Button("删除");
b6.addActionListener(this);
b7=new Button("打开");
b7.addActionListener(this);
b8=new Button("保存");
b8.addActionListener(this);
p1.add(tf1);
p1.add(tf2);
p1.add(tf3);
p2=new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));//设置为左对齐的按行布局
p2.add(cb1);
p2.add(cb2);
p1.add(p2);
p1.add(c1);
p1.add(c2);
p1.add(p3);
p3.add(b1);
p3.add(b2);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
f.addWindowListener(this);
f.setVisible(true);
}
public void windowClosing(WindowEvent e)//单击窗口关闭按钮时产生该事件
{
System.exit(0);//结束程序运行,关闭窗口
}
public void save(File file)//保存
{
try
{
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(file));
String[] in=Is1.getItems();//左边列表框中的记录保存到字符串数组
for(int i=0;i<in.length;i++)
{
byte b[]=in[i].getBytes();//把字符转换为字节
out.write(b,0,b.length);//0~length b字节里的所有记录写入输出流
out.write(13);//回车
out.write(10);//换行
}
out.close();//关闭
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
public void actionPerformed(ActionEvent e)//实现Actionlistener接口中的方法,单击按钮时产生该事件
{
if(e.getSource()==b1) //First,获得产生该事件的对象
{
Is1.select(0); //选中第一条
}
if(e.getSource()==b2) //Prior
{
if(Is1.isIndexSelected(0))
Is1.select(Is1.getItemCount()-1);
else
Is1.select(Is1.getSelectedIndex()-1);
}
if(e.getSource()==b3) //Next
{
if(Is1.isIndexSelected(Is1.getItemCount()-1))
Is1.select(0);
else
Is1.select(Is1.getSelectedIndex()+1);
}
if (e.getSource()==b4) //Last
{
Is1.select(Is1.getItemCount()-1);
}
if(e.getSource()==b5) //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();
Is1.add(str);//添加列表选项
tf1.setText(""+(Integer.parseInt(tf1.getText())+1));//编号自动加1
}
if (e.getSource()==b6) //delete
{
Is1.remove(Is1.getSelectedItem());
}
if (e.getSource()==b7) //Open
{ Is1.removeAll();//在打开的同时自动删除以前记录
FileDialog fd_open=new FileDialog(f,"Open",FileDialog.LOAD);
fd_open.show();//显示对话框
if((fd_open.getDirectory()!=null)&&(fd_open.getFile()!=null))
{ tf3.setText(fd_open.getDirectory()+fd_open.getFile());
try
{ File file=new File(fd_open.getDirectory()+fd_open.getFile());
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);//把fr内容读入br中
String line;//字符串类的一个对象
while((line=br.readLine())!=null)
Is1.add(line+"\r\n");
fr.close();
br.close(); //关闭输入流
}
catch(IOException ioe)
{System.out.println(ioe);}
}
}
if (e.getSource()==b8) //Save
{
if((e.getSource()==b8)&&(file==null))
{
FileDialog fd_close=new FileDialog(f,"Save",FileDialog.SAVE);
if(file==null)
{
fd_close.setFile("LC.txt");
}
else
fd_close.setFile(file.getName());
fd_close.show();
if((fd_close.getDirectory()!=null)&&(fd_close.getFile()!=null))
{
tf3.setText(fd_close.getDirectory()+fd_close.getFile());
file=new File(fd_close.getDirectory(),fd_close.getFile());
save(file);
}
else
save(file);
}
}
}
public void itemStateChanged(ItemEvent e)
{
if(c1.getSelectedIndex()==0)//对选择框C1操作时触发
{
c2.removeAll();//清除选择框C2全部内容
c2.addItem("南京");//选择框C2添加内容
c2.addItem("苏州");
c2.addItem("无锡");
}
if(c1.getSelectedIndex()==1)
{
c2.removeAll();
c2.addItem("杭州");
c2.addItem("宁波");
c2.addItem("无锡");
}
if(c1.getSelectedIndex()==2)
{
c2.removeAll();
c2.addItem("长沙");
c2.addItem("永州");
c2.addItem("郴州" );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -