java图形界面怎样将文本文件里的数据输入到文本框和将文本框中的数据输入到文本文件 急求解在线等可加分
importjava.awt.HeadlessException;importjava.awt.event.ActionEvent;importjava.awt.even...
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.action.JframBank;
/**
* 查询界面
*
* @author Administrator
*
*/
public class BTN1 extends JFrame {
private JButton btn1, btn2;
private JTextField t1;
private JPanel b1 = new JPanel();
private JframBank jfb = null;
public BTN1(String arg0) throws HeadlessException {
super(arg0);
setSize(300, 200);
t1=new JTextField(15);
b1.setName("查询");
btn1 = new JButton("查询余额");
btn1.addActionListener(new B());
btn2 = new JButton("退出");
btn2.addActionListener(new B());
b1.add(btn1);
b1.add(btn2);
b1.add(t1);
setContentPane(b1);
this.setVisible(true);
}
public JTextField getT1() {
return t1;
}
class B implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
Object o = arg0.getSource();
if (o == btn1) {
Query query = new Query();
query.query(jfb);
}
if (o == btn2) {
jfb.setVisible(true);
}
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JTextField;
/**
* 查询
* @author Administrator
*
*/
public class Query {
private JframBank jframBank;
private JTextField jTextField;
public void query(JframBank jframBank){
this.jframBank=jframBank;
// FileReader fin=null;
try {
// fin=new FileReader("D:/1.txt");
BufferedReader Ia=new BufferedReader(new InputStreamReader(new FileInputStream("D:/1.txt")));
try {
String a=Ia.readLine();
Ia.close();
jTextField=jframBank.getBt1().getT1();
jTextField.setText(a);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 展开
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.action.JframBank;
/**
* 查询界面
*
* @author Administrator
*
*/
public class BTN1 extends JFrame {
private JButton btn1, btn2;
private JTextField t1;
private JPanel b1 = new JPanel();
private JframBank jfb = null;
public BTN1(String arg0) throws HeadlessException {
super(arg0);
setSize(300, 200);
t1=new JTextField(15);
b1.setName("查询");
btn1 = new JButton("查询余额");
btn1.addActionListener(new B());
btn2 = new JButton("退出");
btn2.addActionListener(new B());
b1.add(btn1);
b1.add(btn2);
b1.add(t1);
setContentPane(b1);
this.setVisible(true);
}
public JTextField getT1() {
return t1;
}
class B implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
Object o = arg0.getSource();
if (o == btn1) {
Query query = new Query();
query.query(jfb);
}
if (o == btn2) {
jfb.setVisible(true);
}
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JTextField;
/**
* 查询
* @author Administrator
*
*/
public class Query {
private JframBank jframBank;
private JTextField jTextField;
public void query(JframBank jframBank){
this.jframBank=jframBank;
// FileReader fin=null;
try {
// fin=new FileReader("D:/1.txt");
BufferedReader Ia=new BufferedReader(new InputStreamReader(new FileInputStream("D:/1.txt")));
try {
String a=Ia.readLine();
Ia.close();
jTextField=jframBank.getBt1().getT1();
jTextField.setText(a);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 展开
2个回答
展开全部
首先
query.query(jfb);这一行 你传了个null给query方法。这样肯定是不行的
必须实例化一个对象 new JFramebank...
还有
jTextField=jframBank.getBt1().getT1();
这地方 我不知道你的JFramBank是怎么写的 我的理解是你要把读出的数据赋给jtextFeild
那就不必传jframBank,只需要传this就行。query.query(this)
this.getT().setText(a);
这样设置肯定会一行覆盖一行的 所以你最终看到的是最后一行数据
query.query(jfb);这一行 你传了个null给query方法。这样肯定是不行的
必须实例化一个对象 new JFramebank...
还有
jTextField=jframBank.getBt1().getT1();
这地方 我不知道你的JFramBank是怎么写的 我的理解是你要把读出的数据赋给jtextFeild
那就不必传jframBank,只需要传this就行。query.query(this)
this.getT().setText(a);
这样设置肯定会一行覆盖一行的 所以你最终看到的是最后一行数据
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询