📄 dale.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class dale implements ActionListener,ItemListener
{
TextField tf1,tf2,tf3,tf5;
Checkbox cb1,cb2;
Choice c1;
Label t1=new Label("NO:");
Label t2=new Label("NAME:");
Label t3=new Label("当前时间:");
Label t4=new Label("选中的记录:");
Label t5=new Label("JAVA课程设计-信息输入",(Label.CENTER));
Label t6=new Label("制作人: 01级 计4班 23号 袁芳",(Label.CENTER));
Label t7=new Label("指导老师: 傅卓军",(Label.CENTER));
List ls1,ls2;
Button b1,b2,b3,b4,b5,b6,b7,b8;
FileDialog fd;
File file1 = null;
Frame f;
int k=0;
static TextField tf4;
public void display()
{
Panel p1,p2,p3,p4,p5,p6,p7,p8,p9;
CheckboxGroup cg;
f = new Frame("java课程设计-信息输入");
f.setSize(650,400);
f.setLocation(250,170);
f.setBackground(Color.lightGray);
f.setLayout(new GridLayout(1,3));
ls1 = new List(); //创建列表框
ls1.addItemListener(this);
f.add(ls1); //占据窗口左半部分
p1 = new Panel();
p1.setLayout(new GridLayout(7,1)); //网格布局,7行1列
f.add(p1); //占据窗口中间部分
tf1 = new TextField ("1",25); //创建文本行
tf2 = new TextField ("",25);
tf3 = new TextField ("Filename:null",10);
tf4 = new TextField ("",25);
tf5 = new TextField (26);
cg = new CheckboxGroup(); //创建复选框组
cb1 = new Checkbox("male",cg,true); //创建单选按钮
cb2 = new Checkbox("female",cg,false);
c1=new Choice();
c1.addItem("浙江");
c1.addItem("湖南");
c1.addItem("江西");
c1.addItem("江苏");
c1.addItemListener(this); //注册选择框事件监听程序
ls2 = new List(5,false);
ls2.add("温州");
ls2.add("杭州");
ls2.add("宁波");
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.setForeground(Color.red); //设置按钮颜色
// b2.setForeground(Color.red);
b3.setForeground(Color.red);
// b4.setForeground(Color.red);
b5.setForeground(Color.red);
// b6.setForeground(Color.red);
b7.setForeground(Color.red);
// b8.setForeground(Color.red);
t1.setForeground(Color.blue); //设置标签字体颜色
t2.setForeground(Color.blue);
t3.setForeground(Color.blue);
t4.setForeground(Color.blue);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this); //为按钮注册监听监听程序
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
p4 = new Panel();
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
p4.add(t1);
p4.add(tf1);
p5 = new Panel();
p5.setLayout(new FlowLayout(FlowLayout.LEFT));
p5.add(t2);
p5.add(tf2);
p1.add(p4);
p1.add(p5);
p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(cb1);
p2.add(cb2);
p1.add(p2);
p1.add(c1);
p1.add(ls2);
p1.add(tf3);
tf3.setEditable(false);
tf3.setFont(new Font("Dialog",0,12));
p3 = new Panel();
p3.setLayout(new GridLayout(2,4));
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);
p6 = new Panel();
p6.setLayout(new GridLayout(3,1));
p7 = new Panel();
p7.setLayout(new FlowLayout(FlowLayout.LEFT));
p7.add(t3);
p7.add(tf4);
p6.add(p7);
p8 = new Panel();
p8.setLayout(new FlowLayout(FlowLayout.LEFT));
p8.add(t4);
p8.add(tf5);
p6.add(p8);
p9 = new Panel();
p9.setLayout(new GridLayout(3,1));
p9.add(t5);
p9.add(t6);
p9.add(t7);
p6.add(p9);
f.add(p6);
Date time=new Date();
String today=time.toLocaleString();
int s,m,h;
s=time.getSeconds(); //获取当前系统时间方法
m=time.getMinutes();
h=time.getHours();
tf4.setEditable(false);
Thread1 d=new Thread1(); //创建线程对象
d.start(); //启动线程执行
f.addWindowListener(new WinClose()); //注册窗口监听程序
f.setVisible(true);
}
public class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String arg[])
{
(new dale()).display();
}
public static class Thread1 extends Thread//显示当前系统时间
{
public Thread1(){}
public void run() //覆盖run方法的线程体
{
int i=1;
String str1;
while(i==1) //每秒刷新一次
{
try
{
Date today=new Date();
SimpleDateFormat sdf;
sdf=new SimpleDateFormat("MM月dd日 a hh:mm:ss");
str1=sdf.format(today);
tf4.setText(str1);
sleep(1000); //睡眠一秒钟
}
catch(InterruptedException e)
{ System.out.println(e);}
}
}
}
public void actionPerformed(ActionEvent e) //监听按钮事件
{
if(e.getSource()==b1) //单击FIRST按钮
{
ls1.select(0);
}
if(e.getSource()==b2) //单击PRIOR按钮
{
if(ls1.isIndexSelected(0))
ls1.select(ls1.getItemCount()-1);
else
ls1.select(ls1.getSelectedIndex()-1);
}
if(e.getSource()==b3) //单击NEXT按钮
{
if(ls1.isIndexSelected(ls1.getItemCount()-1))
ls1.select(0);
else
ls1.select(ls1.getSelectedIndex()+1);
}
if(e.getSource()==b4) //单击LAST按钮
{
ls1.select(ls1.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+" "+ls2.getSelectedItem();
str = str+" "+tf4.getText();
ls1.add(str); //添加列表框选项
tf1.setText(""+(Integer.parseInt(tf1.getText())+1));//编号自加1
}
if(e.getSource()==b6) //单击DELETE按钮
{
int i,j,k;
k=ls1.getItemCount();
if(k>0)
{i=ls1.getSelectedIndex();
ls1.remove(i);
j=ls1.getItemCount();
ls1.select(j-1);
}
}
if(e.getSource()==b7) //单击OPEN按钮
{ int m;
ls1.removeAll();
fd = new FileDialog(f,"Open",FileDialog.LOAD);
fd.setVisible(true);
if((fd.getDirectory()!=null)&&(fd.getFile()!=null))
{
try
{
tf3.setText(fd.getDirectory()+fd.getFile());
file1 = new File(fd.getDirectory(),fd.getFile());
FileInputStream fin=new FileInputStream(file1);
ObjectInputStream in=new ObjectInputStream(fin);
m=6000;
for(int i=0;i<m;i++)
ls1.add((String)in.readObject()); //读对象并显示
in.close();
int k;
k=ls1.getItemCount();
ls1.select(k-1);
in.close();
}
catch(FileNotFoundException fe){}
catch(IOException ioe){}
catch(ClassNotFoundException ioe){}
tf1.setText(""+(ls1.getItemCount()+1));
}
}
if(e.getSource()==b8) //单击SAVE按钮
{
if ((e.getSource()==b8)&&(file1==null))
{
fd = new FileDialog(f,"Save",FileDialog.SAVE);
if (file1==null)
fd.setFile("");
else
fd.setFile(file1.getName());
fd.setVisible(true);
if ((fd.getDirectory()!=null) && (fd.getFile()!=null))
{
tf3.setText(fd.getDirectory()+fd.getFile());
file1 = new File(fd.getDirectory(),fd.getFile());
save(file1);
}
}
else
save(file1);
}
}
public void save(File file1)
{
int m;
try
{
FileOutputStream fr = new FileOutputStream(file1);
ObjectOutputStream out= new ObjectOutputStream(fr);
m=ls1.getItemCount(); //列表框中记录条数
for(int i=0;i<m;i++)
out.writeObject(ls1.getItem(i)); //写入对象
out.close(); //关闭对象流
}
catch(IOException ioe)
{System.out.println(ioe);}
}
public void itemStateChanged(ItemEvent e)
{
if(c1.getSelectedIndex()==0) //选中浙江
{
ls2.removeAll();
ls2.add("温州");
ls2.add("杭州");
ls2.add("宁波");
}
if(c1.getSelectedIndex()==1) //选中湖南
{
ls2.removeAll();
ls2.add("长沙");
ls2.add("株洲");
ls2.add("湘潭");
ls2.add("常德");
}
if(c1.getSelectedIndex()==2) //选中江西
{
ls2.removeAll();
ls2.add("南昌");
ls2.add("景德镇");
ls2.add("南平");
}
if(c1.getSelectedIndex()==3) //选中江苏
{
ls2.removeAll();
ls2.add("南京");
ls2.add("苏州");
ls2.add("无锡");
}
if(e.getSource()==ls1)
{
String str2;
str2=ls1.getSelectedItem();
tf5.setText(str2);
b6.setEnabled(true);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -