10例子14.txt

来自「这是一本java基础教程 对新手上路有很大帮助」· 文本 代码 · 共 44 行

TXT
44
字号
import javax.swing.*; 
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Example10_14{
    public static void main(String args[]){
        new ReadFileWindow();
    }
}
class ReadFileWindow extends JFrame implements ItemListener{
    JComboBox list;
    JTextArea showText;
    ReadFileWindow(){
        showText=new JTextArea(12,12);  
        list=new JComboBox();
        list.addItem("Example10_1.java");
        list.addItem("Example10_2.java");
        add(list,BorderLayout.NORTH);
        add(new JScrollPane(showText));
        validate();
        list.addItemListener(this);
        setBounds(120,120,500,370);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    } 
    public void itemStateChanged(ItemEvent e){
        String fileName=(list.getSelectedItem()).toString();
        File readFile=new File(fileName);
        showText.setText(null);
        try{   FileReader inOne=new FileReader(readFile);
              BufferedReader inTwo= new BufferedReader(inOne);
              String s=null;
              int i=0;
              while((s=inTwo.readLine())!=null)
                   showText.append("\n"+s);
              inOne.close();
              inTwo.close();
         }
         catch(IOException ex){
              showText.setText(ex.toString());   
         }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?