请问:
       如何用swing实现以下功能:单击按钮,打开一个指定文本文件.

解决方案 »

  1.   

    请大家帮帮忙吧.以下是我在NetBeans 中设计的GUI的源代码,想实现上面这个功能,求助./*
     * FirstGUI.java
     *
     * Created on 2008年2月15日, 下午2:22
     */package myFirstGUI;
    import javax.swing.*;
    /**
     *
     * @author  Administrator
     */
    public class FirstGUI extends javax.swing.JFrame {
        
        /** Creates new form FirstGUI */
        public FirstGUI() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {        jTextField1 = new javax.swing.JTextField();
            jTextField2 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jTextField1.setFont(new java.awt.Font("宋体", 0, 24));
            jTextField1.setText(" 点滴日语智能记忆");        jTextField2.setFont(new java.awt.Font("宋体", 0, 18));
            jTextField2.setText("  请选择词库:");        jButton1.setText("中日交流标准日本语初级上册");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });        jButton2.setText("中日交流标准日本语初级下册");        jButton3.setText("中日交流标准日本语中级上册");        jButton4.setText("中日交流标准日本语中级下册");        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(163, 163, 163)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(125, Short.MAX_VALUE))
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(46, 46, 46)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 132, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(66, 66, 66)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jButton1)
                        .add(jButton4)
                        .add(jButton3)
                        .add(jButton2))
                    .add(79, 79, 79))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 46, Short.MAX_VALUE)
                    .add(jButton1)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(jButton2)
                            .add(18, 18, 18)
                            .add(jButton3)
                            .add(18, 18, 18)
                            .add(jButton4))
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(88, 88, 88))
            );        pack();
        }// </editor-fold>    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            
            
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new FirstGUI().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration
        
    }
      

  2.   

    在按钮事件中读取文本,给你个文本文件读写的小例子:import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;public class TestMain { /**
     * 构造方法
     */
    public TestMain() {
    try {
    File file = new File("C:\\test.txt");
    BufferedWriter write = new BufferedWriter(new FileWriter(file, true));
    for(int i = 0; i < 10; i++){
    write.write("写入行数:" + i + "\n");
    write.flush();
    }
    write.close();

    BufferedReader read = new BufferedReader(new FileReader(file));
    String temp = "";
    while((temp = read.readLine()) != null){
    System.out.println(temp);
    }
    read.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    new TestMain();
    }}