java从文本框保存文件_关于JAVA中输入框内容的读入以及本地文件读取到文本框中操作(以老师发布作业及读取本地学生作业为例)...

class Teacher extends JDialog{  //教师跳转窗口

public static String s;  //获取文本框里的内容

public Teacher(HdIhomework frame){  //类似管理员窗口

super(frame,"教师登录窗口",true);

setLayout(null);

setBounds(200,200,1000,800);

Container container2=getContentPane();

JTextArea jt2=new JTextArea("教师登录成功!");

//显示在操作过程中教师文件中内容显示

JTextArea jt3=new JTextArea();

//显示读取的学生上传文件

JTextArea jt4=new JTextArea();

JButton jb7=new JButton("发布作业");  //老师发布作业按钮

jb7.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

//发布作业保存的地址

File file2=new File("e:/cr/;

//作业内容的保存

try

{

FileWriter out=new FileWriter(file2);

s=jt3.getText();

out.write(s);

out.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

}

});

JButton jb8=new JButton("发布答案");  //老师发布答案的按钮

jb8.addActionListener(new ActionListener(){

//该监听器包含了老师发布三种不同形式的答案

public void actionPerformed(ActionEvent e){

//老师发布选择题的答案

JButton jb12=new JButton("选择题");

jb12.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

//老师发布选择题答案的保存地址

File file2=new File("e:/cr/;

try

{

FileWriter out=new FileWriter(file2);

s=jt3.getText();

out.write(s);

out.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

}

});

//类似于选择题

JButton jb13=new JButton("简单数值题");

jb13.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

File file2=new File("e:/cr/;

try

{

FileWriter out=new FileWriter(file2);

s=jt3.getText();

out.write(s);

out.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

}

});

//类似于选择题

JButton jb14=new JButton("问答题");

jb14.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

File file2=new File("e:/cr/;

try

{

FileWriter out=new FileWriter(file2);

s=jt3.getText();

out.write(s);

out.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

}

});

jb12.setBounds(300, 50, 100, 25);

jb13.setBounds(300, 100, 100, 25);

jb14.setBounds(300, 150, 100, 25);

container2.add(jb12);

container2.add(jb13);

container2.add(jb14);

}

});

JButton jb9=new JButton("发布反馈");  //老师发布反馈按钮

jb9.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

File file3=new File("e:/cr/;

try

{

FileWriter out=new FileWriter(file3);

s=jt3.getText();

out.write(s);

out.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

}

});

JButton jb10=new JButton("自助批改");  //系统自动批改选择题与简单题按钮

jb10.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

JButton jb20=new JButton("选择");

//选择题的批改

jb20.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

String s1=null,s2 = null;  //返回读取自教师及学生的选择题答案

//读取文件操作

File file=new File("e:/cr/;

try

{

FileReader in=new FileReader(file);

char byt[]=new char[1024];

int len=in.read(byt);

jt3.setText(new String(byt,0,len));

s1=jt3.getText();

in.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

File file1=new File("e:/cr/;

try

{

FileReader in=new FileReader(file1);

char byt[]=new char[1024];

int len=in.read(byt);

jt4.setText(new String(byt,0,len));

s2=jt4.getText();

in.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

char[] c1=s1.toCharArray(),c2=s2.toCharArray();

double g;  //返回错误率

int count=0;  //计算错题数

//for循环判断两个字符串不同的字符并统计

for(int i=0;i

{

if(c1[i]!=c2[i])

{

count++;

}

}

g=count/s1.length();

System.out.println("错误率为"+g);

System.out.println("s1为"+s1);

System.out.println("s2为"+s2);

//在主界面左文本框中输入批改完成

jt3.setText("批改完成!");

//显示错误率

jt4.setText("错误率为"+g);

//将错误率保存在电脑中 方便学生查询

File file4=new File("e:/cr/;

try

{

FileWriter out=new FileWriter(file4);

s=jt4.getText();

out.write(s);

out.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

}

});

JButton jb21=new JButton("数值");  //类似于选择题的创建

jb21.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

String s1=null,s2 = null;  //返回读取自教师及学生的简单题答案

File file=new File("e:/cr/;

try

{

FileReader in=new FileReader(file);

char byt[]=new char[1024];

int len=in.read(byt);

jt3.setText(new String(byt,0,len));

s1=jt3.getText();

in.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

File file1=new File("e:/cr/;

try

{

FileReader in=new FileReader(file1);

char byt[]=new char[1024];

int len=in.read(byt);

jt4.setText(new String(byt,0,len));

s2=jt4.getText();

in.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

char[] c1=s1.toCharArray(),c2=s2.toCharArray();

double g;

int count=0;

for(int i=0;i

{

if(c1[i]!=c2[i])

{

count++;

}

}

g=count/s1.length();

System.out.println("错误率为"+g);

System.out.println("s1为"+s1);

System.out.println("s2为"+s2);

jt3.setText("批改完成");

jt4.setText("错误率为"+g);

File file4=new File("e:/cr/;

try

{

FileWriter out=new FileWriter(file4);

s=jt4.getText();

out.write(s);

out.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

}

});

jb20.setBounds(650, 20, 100, 25);

jb21.setBounds(650, 75, 100, 25);

container2.add(jb20);

container2.add(jb21);

//System.out.println("c2为"+c2);

//System.out.println("c1为"+c1);

}

});

JButton jb11=new JButton("手动批改");  //教师手动批改问答题

jb11.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

//学生作业的读取操作

File file=new File("e:/cr/;

try

{

FileReader in=new FileReader(file);

char byt[]=new char[1024];

int len=in.read(byt);

jt4.setText(new String(byt,0,len));

in.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

//在文本框中提示教师的操作

jt2.setText("请给出你的分数");

//创建完成与取消按钮 教师可以在选择完成后 将分数保存在电脑上让学生查询

JButton jb12=new JButton("完成");

jb12.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

File file=new File("e:/cr/;

try

{

FileWriter out=new FileWriter(file);

s=jt3.getText();

out.write(s);

out.close();

}

catch(Exception e1)

{

e1.printStackTrace();

}

}

});

JButton jb13=new JButton("取消");

jb12.setBounds(500, 550, 60, 50);

jb13.setBounds(600, 550, 60, 50);

container2.add(jb12);

container2.add(jb13);

}

});

JLabel jl1=new JLabel("教师内容显示");

JLabel jl2=new JLabel("学生内容显示");

jt2.setBounds(100, 200, 300, 300);

jt3.setBounds(500, 250, 150, 250);

jt4.setBounds(700, 250, 150, 250);

jb7.setBounds(100, 50, 100, 25);

jb8.setBounds(100, 100, 100, 25);

jb9.setBounds(100, 150, 100, 25);

jb10.setBounds(500, 50, 100, 25);

jb11.setBounds(500, 100, 100, 25);

jl1.setBounds(500, 200, 100, 50);

jl2.setBounds(700, 200, 100, 50);

container2.add(jt2);

container2.add(jt3);

container2.add(jt4);

container2.add(jb7);

container2.add(jb8);

container2.add(jb9);

container2.add(jl1);

container2.add(jl2);

container2.add(jb10);

container2.add(jb11);

setVisible(true);

}

}

代码运行显示

a4c26d1e5885305701be709a3d33442f.png

//后续有时间将读取发布步骤上传

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值