java图形界面怎样将文本文件里的数据输入到文本框和将文本框中的数据输入到文本文件 急求解在线等可加分
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java图形界面怎样将文本文件里的数据输入到文本框和将文本框中的数据输入到文本文件 急求解在线等可加分相关的知识,希望对你有一定的参考价值。
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();
query.query(jfb);这一行 你传了个null给query方法。这样肯定是不行的
必须实例化一个对象 new JFramebank...
还有
jTextField=jframBank.getBt1().getT1();
这地方 我不知道你的JFramBank是怎么写的 我的理解是你要把读出的数据赋给jtextFeild
那就不必传jframBank,只需要传this就行。query.query(this)
this.getT().setText(a);
这样设置肯定会一行覆盖一行的 所以你最终看到的是最后一行数据 参考技术A 就是一个文本文件的读写啊,用FileInPutStream
Java编程(基于图形界面)从文件中读出数据,显示在文本框中
Java编程
1.利用记事本创建一个文本文件,输入若干整数,保存。
2.编程实现(基于图形界面):
1)从文件中读出数据,显示在文本框中;
2)创建2个线程(要求实现Runnable接口
1.利用记事本创建一个文本文件,输入若干整数,保存。
2.编程实现(基于图形界面):
1)从文件中读出数据,显示在文本框中;
2)创建2个线程(要求实现Runnable接口),分别判断其中的素数和合数,并显示出来。
首先建一个文本文件,比如在D盘根目录下,名字为:int.txt,然后写一个Java类CalcNumber.java。
****************************************************************
int.txt里面的文本如下:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
****************************************************************
CalcNumber.java类代码如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
public class CalcNumber extends JFrame
JTextArea in;
JTextArea suText;
JTextArea heText;
public CalcNumber()
this.setTitle("计算素数和合数");
// 输入框
in = new JTextArea(4, 1);
in.setAutoscrolls(true);
JScrollPane inScroll = new JScrollPane(in);
in.setBorder(BorderFactory.createBevelBorder(1));
JPanel upper = new JPanel(new BorderLayout());
upper.add(inScroll);
// 按钮
JButton imp = new JButton("导入文本");
imp.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
try
BufferedReader br = new BufferedReader(new FileReader(
"d:\\\\int.txt"));
String s;
while ((s = br.readLine()) != null)
in.setText(in.getText() + "\\n" + s);
br.close();
catch (FileNotFoundException e1)
e1.printStackTrace();
catch (IOException e2)
e2.printStackTrace();
);
JButton start = new JButton("启动两个线程并计算");
start.addActionListener(new CalcActionListener());
JPanel buttonP = new JPanel();
buttonP.add(imp);
buttonP.add(start);
// 结果
JPanel result = new JPanel(new GridLayout(1, 2));
JPanel suPanel = new JPanel();
suPanel.setBorder(BorderFactory.createTitledBorder("以下为素数"));
JPanel hePanel = new JPanel();
hePanel.setBorder(BorderFactory.createTitledBorder("以下为合数"));
result.add(suPanel);
result.add(hePanel);
suText = new JTextArea(4, 25);
JScrollPane suScroll = new JScrollPane(suText);
suText.setBorder(BorderFactory.createBevelBorder(1));
heText = new JTextArea(4, 25);
JScrollPane heScroll = new JScrollPane(heText);
heText.setBorder(BorderFactory.createBevelBorder(1));
suPanel.add(suScroll);
hePanel.add(heScroll);
// 总
JPanel all = new JPanel(new GridLayout(2, 1, 2, 2));
all.setBorder(BorderFactory.createTitledBorder("计算素数和合数"));
all.add(upper);
all.add(result);
this.add(buttonP, BorderLayout.SOUTH);
this.add(all, BorderLayout.CENTER);
// this.setSize(400, 300);
this.pack();
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension screen = tool.getScreenSize();
this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
/ 2 - this.getHeight() / 2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class CalcActionListener implements ActionListener, Runnable
public void actionPerformed(ActionEvent e)
Thread suThread = new Thread(new CalcActionListener(), "su");
Thread heThread = new Thread(new CalcActionListener(), "he");
suThread.start();
heThread.start();
public void run()
String s = in.getText();
String[] arr = s.split("\\\\s");
StringBuffer su = new StringBuffer();
StringBuffer he = new StringBuffer();
for (int i = 0; i < arr.length; i++)
boolean flag = false;
if (!arr[i].equals("") && arr[i] != null)
int n = Integer.valueOf(arr[i]);
for (int j = 2; j < n; j++)
if (n % j == 0)
flag = true;
if (Thread.currentThread().getName().equals("su")
&& flag == false)
su = su.append(" " + n);
if (Thread.currentThread().getName().equals("he")
&& flag == true)
he = he.append(" " + n);
if (Thread.currentThread().getName().equals("su"))
suText.setText(new String(su));
else
heText.setText(new String(he));
public static void main(String[] args) throws Exception
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
CalcNumber c = new CalcNumber();
****************************************************************
运行结果如下:
参考技术A btn.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)
String content=jtainput.getText();
if(content.equals(""))
JOptionPane.showMessageDialog(null,"请输入");
jtaoutput.setText(jtaoutput.getText()+'\n'+content);
jtainput.setText("");
); 参考技术B import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test extends JFrame implements ActionListener,Runnable
JButton btn;
JTextArea text;
JScrollPane scroll;
Thread prime,composite;
File file;
String s1="素数有:",s2="合数有:";
public Test()
btn=new JButton("load file...");
btn.addActionListener(this);
text=new JTextArea();
text.setLineWrap(true);
scroll=new JScrollPane(text);
JPanel p=new JPanel(new BorderLayout());
p.add(btn,"South");
p.add(scroll,"Center");
setContentPane(p);
setSize(800,500);
setTitle("提取素数和合数");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
public static void main(String [] args)
new Test();
public void actionPerformed(ActionEvent e)
JFileChooser chooser=new JFileChooser();
chooser.showOpenDialog(this);
file=chooser.getSelectedFile();
writeToText();
(prime=new Thread(this,"prime")).start();
(composite=new Thread(this,"composite")).start();
public void run()
if(Thread.currentThread().getName().equals("prime"))
splitPrime();
if(Thread.currentThread().getName().equals("composite"))
splitComposite();
public void writeToText()
String s=null;
try
BufferedReader in=new BufferedReader(new FileReader(file));
text.setText("");
while((s=in.readLine())!=null)
text.append(s);
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException ee)
ee.printStackTrace();
public void splitComposite()
Scanner sc=new Scanner(text.getText());
for(int i=sc.nextInt();sc.hasNextInt();i=sc.nextInt())
int j=i/2;
for(int k=1;k<=j&&i!=2;k++)
if(k==1)
continue;
if(i%k==0)
s2+=String.valueOf(i)+" ";
break;
text.append("\n\n"+s2);
public void splitPrime()
Scanner sc=new Scanner(text.getText());
for(int i=sc.nextInt();sc.hasNextInt();i=sc.nextInt())
int j=i/2;
int count=0;
for(int k=1;k<=j&&i!=2;k++)
if(k==1)
count++;
continue;
if(i%k!=0)
count++;
if(k==j&&count==j)
s1+=String.valueOf(i)+" ";
if(i==2)
s1+=String.valueOf(i)+" ";
text.append("\n\n"+s1);
让我很郁闷的是,这个程序判断3的时候老是说不是素数,可是我口算了很多遍都是的啊,其它的要求都满足了!
以上是关于java图形界面怎样将文本文件里的数据输入到文本框和将文本框中的数据输入到文本文件 急求解在线等可加分的主要内容,如果未能解决你的问题,请参考以下文章
写一个java GUI界面,有个文本框可输入文本,按个按钮之后输出到文件中