zoukankan      html  css  js  c++  java
  • 20110610上午java考试复数题

    一道大题
    (1) 在程序界面由上至下依次设按钮A、文本A、按钮B、文本B、按钮C
    (2) 按按钮A后,读取外部文件"input.txt",将其显示在文本区A中
    (3) 按按钮B后,找出文本A中的各英文单词(注意剔除其中的标点),将其按照字典序降序排序后在文本B中逐行输出
    (4) 按按钮C后,将文本区B中的内容写入外部文件"output.txt"

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;

    public class Exam {

        private JButton btnA;
        private JTextArea txtAreaA;
        private JScrollPane jspA;
        private JButton btnB;
        private JTextArea txtAreaB;
        private JScrollPane jspB;
        private JButton btnC;

        public Exam(){
            JFrame frame = new JFrame("2011.06.10 Exam");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setSize(400, 300);
            
            JPanel pan = new JPanel();
            BoxLayout b = new BoxLayout(pan, BoxLayout.Y_AXIS);
            pan.setLayout(b);

            btnA = new JButton("Button A");
            btnA.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    FileInputStream fis = null;
                    InputStreamReader isr = null;
                    try{
                        String path = System.getProperty("user.dir");
                        path = path+"/input.txt";
                        //String path = "c://input.txt";
                        File file = new File(path);
                        fis = new FileInputStream(file);
                        isr = new InputStreamReader(fis);
                        StringBuilder textA = new StringBuilder();
                        int v ;
                        while(true){
                            v = isr.read();
                            if(v==-1){
                                break;
                            }
                            textA.append((char)v);
                        }
                        txtAreaA.setText(textA.toString());
                    }catch (Exception ee) {
                        ee.printStackTrace();
                    }finally{
                        try{
                            if(null!=isr)isr.close();
                        }catch(Exception ee){
                            ee.printStackTrace();
                        }
                        try{
                            if(null!=fis)fis.close();
                        }catch(Exception ee){
                            ee.printStackTrace();
                        }
                    }
                }
            });
            txtAreaA = new JTextArea();
            jspA = new JScrollPane();
            jspA.add(txtAreaA);
            jspA.setViewportView(txtAreaA);
            btnB = new JButton("Button B");
            btnB.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String text = txtAreaA.getText();
                    String strArr[] = text.split("[^\\w]");
                    List<String> strList = new ArrayList<String>();
                    String s;
                    for(int i=0;i<strArr.length;i++){
                        s = strArr[i];
                        if(!"".equals(s)){
                            strList.add(s.replace("\n", ""));
                        }
                    }
                    Collections.sort(strList);
                    StringBuilder strText = new StringBuilder();
                    for(int j=strList.size()-1;j>=0;j--){
                        strText.append(strList.get(j)).append("\n");
                    }
                    txtAreaB.setText(strText.toString());
                }
            });
            txtAreaB = new JTextArea();
            jspB = new JScrollPane();
            jspB.add(txtAreaB);
            jspB.setViewportView(txtAreaB);
            btnC = new JButton("Button C");
            btnC.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String textB = txtAreaB.getText();
                    textB = textB.replace("\n", "\r");
                    FileOutputStream fos = null;
                    OutputStreamWriter osw = null;
                    try{
                        String path = System.getProperty("user.dir");
                        path = path+"/output.txt";
                        //String path = "c://output.txt";
                        File file = new File(path);
                        fos = new FileOutputStream(file);
                        osw = new OutputStreamWriter(fos);
                        osw.write(textB);
                    }catch (Exception ee) {
                        ee.printStackTrace();
                    }finally{
                        try{
                            if(null!=osw)osw.close();
                        }catch(Exception ee){
                            ee.printStackTrace();
                        }
                        try{
                            if(null!=fos)fos.close();
                        }catch(Exception ee){
                            ee.printStackTrace();
                        }
                    }
                    JOptionPane.showMessageDialog(null,"write output.txt over");
                }
            });
            pan.add(btnA);
            pan.add(jspA);
            pan.add(btnB);
            pan.add(jspB);
            pan.add(btnC);

            frame.add(pan);
            frame.setVisible(true);
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
            new Exam();
        }

    }

  • 相关阅读:
    第 3 章(类型、值和变量)(3.1~ 3.5)
    第七章(插件的使用和写法)(7.6 编写 jQuery 插件)
    第七章(插件的使用和写法)(7.4 jQuery UI 插件 7.5 管理Cookie的插件 --- Cookie)
    第七章(插件的使用和写法)(7.3 动态绑定事件插件 ----- livequery)(未完成)
    第七章(插件的使用和写法)(7.2 jQuery 表单插件 ----- Form)
    第七章(插件的使用和写法)(7.1 jQuery 表单验证插件 ----- Validation)
    第六章(jQuery 与 Ajax 的应用)(6.8 基于 jQuery 的 Ajax 聊天室程序)(未完成)
    第六章(jQuery 与 Ajax 的应用)(6.6 序列化元素 6.7 jQuery 中的 Ajax 事件)
    Python爬虫实战八之利用Selenium抓取淘宝匿名旺旺
    Python爬虫实战七之计算大学本学期绩点
  • 原文地址:https://www.cnblogs.com/SunDexu/p/2767127.html
Copyright © 2011-2022 走看看