Java编程(基于图形界面)从文件中读出数据,显示在文本框中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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怎么将查询到的数据库的内容显示到java图形界面上?
java怎么将查询到的数据库的内容显示到java图形界面上?
用Python shelve将canvas图形图像保存为文件及从文件读出图形图像重新显示的方法