java編寫(xiě)顯示文本的應(yīng)用程序, 需要用到圖形界面GUI編程技術(shù).

創(chuàng)新互聯(lián)專(zhuān)業(yè)為企業(yè)提供監(jiān)利網(wǎng)站建設(shè)、監(jiān)利做網(wǎng)站、監(jiān)利網(wǎng)站設(shè)計(jì)、監(jiān)利網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、監(jiān)利企業(yè)網(wǎng)站模板建站服務(wù),十多年監(jiān)利做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
步驟一: 需要搭建一個(gè)整體的外觀(guān), ?調(diào)整布局和組件的位置.
主要需要使用的組件
JTextField 文本框組件: 用于當(dāng)做地址欄, 填寫(xiě)文件的路徑
JButton 按鈕, 用于響應(yīng)點(diǎn)擊事件,根據(jù)地址欄的地址讀取文件, 并且顯示到文本域里
JTextArea 文本域, 用于顯示文件里的字符串信息
JLabel ?標(biāo)簽, 用于顯示行數(shù)信息
布局: 使用邊界布局BorderLayout,可以快速按照上下左右中的位置快速布局.
步驟二: IO流,讀取文本文件信息,并統(tǒng)計(jì)行數(shù).
由于要按行讀取,方便計(jì)算文件的行數(shù), 所以使用Buffered 可以事半功倍.
核心代碼如下
StringBuffer?txtbuf=new?StringBuffer();//用于保存文本信息
int?lines=0;//用于計(jì)算行數(shù)
try?{
BufferedReader?br?=?new?BufferedReader(new?FileReader(fp));
String?hasRead=null;
while((hasRead=br.readLine())!=null){
txtbuf.append(hasRead+"\n");
lines++;
}
br.close();//IO流用完記得關(guān)閉
}?catch?(Exception?e)?{
//當(dāng)IO出現(xiàn)異常時(shí),要進(jìn)行提示
JOptionPane.showMessageDialog(this,?"文件讀取錯(cuò)誤,確認(rèn)文件存在,\n或者沒(méi)有被其他文件打開(kāi).","IO錯(cuò)誤",JOptionPane.ERROR_MESSAGE);;
}
步驟三:整合代碼
import?java.awt.*;
import?java.awt.event.*;
import?java.io.*;
import?javax.swing.*;
public?class?ShowTextFrame?extends?JFrame?implements?ActionListener{
JTextArea?jta;//該文本域用于顯示文本信息
JTextField?jtf;//該文本框,用于填寫(xiě)文件路徑
JLabel?jl;//該標(biāo)簽用于保存讀取到的行數(shù)
JButton?jbt;//按鈕
public?ShowTextFrame()?{
jtf?=?new?JTextField(18);
jbt?=?new?JButton("讀取并顯示");
jbt.addActionListener(this);
JPanel??jp1?=?new?JPanel();
jp1.add(jtf);
jp1.add(jbt);
add(jp1,BorderLayout.NORTH);
jta?=?new?JTextArea();
JScrollPane?jsp?=?new?JScrollPane(jta);//文本域添加到滾動(dòng)面板
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//垂直滾動(dòng)條一直顯示
add(jsp);
jl?=?new?JLabel("文件共有0行");
JPanel?jp2?=?new?JPanel();
jp2.add(jl);
add(jp2,BorderLayout.SOUTH);
setTitle("顯示文本");//窗口標(biāo)題
setSize(380,?320);
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public?void?actionPerformed(ActionEvent?e)?{
if(jbt==e.getSource()){
String?fp?=?jtf.getText().trim();
Info?info?=?getInfo(fp);
jta.setText(info.txt);//把文本信息顯示到文本域
jl.setText("文件共有"+info.lines+"行");//把行數(shù)顯示顯示到JLabel
}
}
public?Info?getInfo(String?fp){//通過(guò)文件路徑,獲取文件信息(字符串信息和行數(shù)信息)
StringBuffer?txtbuf=new?StringBuffer();
int?lines=0;
try?{
BufferedReader?br?=?new?BufferedReader(new?FileReader(fp));
String?hasRead=null;
while((hasRead=br.readLine())!=null){
txtbuf.append(hasRead+"\n");
lines++;
}
br.close();//IO流用完記得關(guān)閉
}?catch?(Exception?e)?{
//當(dāng)IO出現(xiàn)異常時(shí),要進(jìn)行提示
JOptionPane.showMessageDialog(this,?"文件讀取錯(cuò)誤,確認(rèn)文件存在,\n或者沒(méi)有被其他文件打開(kāi).","IO錯(cuò)誤",JOptionPane.ERROR_MESSAGE);;
}
return?new?Info(txtbuf.toString(),?lines);
}
public?static?void?main(String[]?args)?{
new?ShowTextFrame();//創(chuàng)建窗口實(shí)例
}
}
class?Info{//輔助類(lèi),?用于傳遞信息
String?txt;//文字信息
int?lines;//?行數(shù)信息
public?Info(String?txt,?int?lines)?{
this.txt?=?txt;
this.lines?=?lines;
}
}
運(yùn)行效果圖
在main.xml定義一個(gè)Button
Button
android:id = "@+id/myBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的詳細(xì)介紹"/
Activity的代碼
public class MainActivity extends Activity {
Button myButton;
String introduce = "內(nèi)容主題文本,可以用賦值(值傳遞還是地址傳遞)如:daXia.toString()";//介紹:你要傳的內(nèi)容
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.myBtn);
//點(diǎn)擊按鈕在當(dāng)前Activity(如:MainActivity.this)彈出一個(gè)新的AlertDialog對(duì)話(huà)框
myButton.setOnClickListener(new OnClickListener() {//導(dǎo)包:import android.view.View.OnClickListener;
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
//設(shè)置對(duì)話(huà)框內(nèi)容
.setTitle("我的詳細(xì)介紹")
.setMessage(introduce)
.setPositiveButton("確定", null)
.show();
}
});
}
}
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class H {
/**
* 功能:Java讀取txt文件的內(nèi)容
* 步驟:1:先獲得文件句柄
* 2:獲得文件句柄當(dāng)做是輸入一個(gè)字節(jié)碼流,需要對(duì)這個(gè)輸入流進(jìn)行讀取
* 3:讀取到輸入流后,需要讀取生成字節(jié)流
* 4:一行一行的輸出。readline()。
* 備注:需要考慮的是異常情況
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() file.exists()){ //判斷文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考慮到編碼格式
BufferedReader bufferedReader = new BufferedReader(read);//創(chuàng)建讀入的buffer
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){//按行輸出讀取的內(nèi)容
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("讀取文件內(nèi)容出錯(cuò)");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "L:\\Apache\\htdocs\\res\\read.txt";//文件路徑名稱(chēng)
readTxtFile(filePath);
}
}
復(fù)制粘貼自網(wǎng)上,添加了部分備注。。
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjavax.swing.JButton;importjavax.swing.JEditorPane;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassWindowTestextendsJFrameimplementsActionListener,KeyListener{privatestaticfinallongserialVersionUID=1L;/***主方法*/publicstaticvoidmain(String[]args){WindowTestwin=newWindowTest();}/***下面是具體實(shí)現(xiàn)*/JTextFieldtext;JButtonbutton;JEditorPanetextArea;publicWindowTest(){super("測(cè)試窗體");text=newJTextField(15);text.addKeyListener(this);JPanelp1=newJPanel();p1.add(newJLabel("輸入字符:"));p1.add(text);button=newJButton("清除");button.addActionListener(this);p1.add(button);p1.setBounds(5,5,220,100);textArea=newJEditorPane();textArea.setBounds(1,1,216,200);JPanelp2=newJPanel();p2.add(newJLabel("顯示字符:"));p2.add(textArea);p2.setBounds(5,115,340,220);JPanelp3=newJPanel();p3.add(p1);p3.add(p2);add(p3);setBounds(160,60,400,300);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@OverridepublicvoidkeyPressed(KeyEvente){}@OverridepublicvoidkeyReleased(KeyEvente){if(e.getKeyCode()==KeyEvent.VK_ENTER){textArea.setText("");}else{Stringstr=text.getText();textArea.setText(str);}}@OverridepublicvoidkeyTyped(KeyEvente){}@OverridepublicvoidactionPerformed(ActionEvente){text.setText("");textArea.setText("");}}
我編譯時(shí)報(bào)了
p185_6.java:50: 警告: catch 子句無(wú)法訪(fǎng)問(wèn)
catch(IOException e2){System.out.println("文件讀寫(xiě)錯(cuò)");}
改正后文件可以在頁(yè)面內(nèi)顯示,就是位置有點(diǎn)問(wèn)題。
可以把a(bǔ)dd(jta,BorderLayout.SOUTH);改成add(jta,BorderLayout.CENTER);
如果想使用滾動(dòng)條,可以把JTextArea放在JScrollPane里,然后再放在JFrame中。
add(jta,BorderLayout.SOUTH);改成add(new JScrollPane(jta),BorderLayout.CENTER);
當(dāng)前文章:java文本顯示內(nèi)容代碼 Java文本
轉(zhuǎn)載來(lái)源:http://chinadenli.net/article32/dodpdpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、外貿(mào)建站、響應(yīng)式網(wǎng)站、網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)