![]() |
#2
GrayJerry2015-12-22 09:43
只有本站会员才能查看附件,请 登录 ,这里没改,而且你想实现的应该是,点击确定后,文本框的内容写入denglu.txt文件中,所以File file1 =new File("D:\\denglu.txt");及以后的代码应放在surelistener类里所有的代码如下: package test; import java.awt.*; import java.awt.event.*; import *; public class Test { TextField loginField = new TextField(10); TextField passworldField = new TextField(10); TextField homeField = new TextField(10); Button sure = new Button("确定"); Button clear = new Button("清除");// 这些东西放在denglu()方法里面,有错,所以我拿出来了 public static String loginT, passwordT, homeT; public void denglu() { Frame f = new Frame(); f.setBounds(150, 150, 200, 200); f.setVisible(true); f.setLayout(new GridLayout(4, 2, 4, 4)); Label log_in = new Label(" 用户名"); Label password_ = new Label(" 密码 "); Label home_ = new Label(" 地址 "); sure.addActionListener(new sureListener()); clear.addActionListener(new clearListener()); f.add(log_in); f.add(loginField); f.add(password_); f.add(passworldField); f.add(home_); f.add(homeField); f.add(sure); f.add(clear); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(0); } }); } class sureListener implements ActionListener { public void actionPerformed(ActionEvent e1) { if (e1.getSource() == sure) { loginT = loginField.getText(); passwordT = passworldField.getText(); homeT = homeField.getText(); File file1 = new File("D:\\denglu.txt"); try { file1.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { FileWriter fw = new FileWriter("D:\\denglu.txt"); fw.write(loginT); fw.write(passwordT); fw.write(homeT); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } class clearListener implements ActionListener { public void actionPerformed(ActionEvent e2) { } } public static void main(String[] args) { Test a = new Test(); a.denglu(); } } |
程序是为了将文本框中的内容写到一个txt文件中,我运行过了,
txt可以创建,但是不能写入东西
大家如果认为我的程序有编的不好的地方,都可以说说,一定虚心接受
package test;
import java.awt.*;
import java.awt.event.*;
import *;
public class Test
{
TextField loginField = new TextField(10);
TextField passworldField=new TextField(10);
TextField homeField=new TextField(10);
Button sure = new Button("确定");
Button clear = new Button("清除");//这些东西放在denglu()方法里面,有错,所以我拿出来了
public static String loginT,passwordT,homeT;
public void denglu()
{
Frame f = new Frame();
f.setBounds(150, 150, 200, 200);
f.setVisible(true );
f.setLayout(new GridLayout(4,2,4,4));
Label log_in = new Label(" 用户名");
Label password_ =new Label(" 密码 ");
Label home_ = new Label(" 地址 ");
sure.addActionListener(new sureListener());
clear.addActionListener(new clearListener());
f.add(log_in);
f.add(loginField);
f.add(password_);
f.add(passworldField);
f.add(home_);
f.add(homeField);
f.add(sure);
f.add(clear);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent arg0)
{
System.exit(0);
}
});
}
class sureListener implements ActionListener
{
public void actionPerformed(ActionEvent e1)
{
if(e1.getSource()==sure)
{
loginT=loginField.getText();
passwordT=loginField.getText();
homeT=loginField.getText();
}
}
}
class clearListener implements ActionListener
{
public void actionPerformed(ActionEvent e2)
{
}
}
public static void main(String[] args)
{
Test a =new Test();
a.denglu();
File file1 =new File("D:\\denglu.txt");
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter fw = new FileWriter("D:\\denglu.txt");
fw.write(loginT);
fw.write(passwordT);
fw.write(homeT);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}