-3

我应该创建一个 gui 来读取文件并使用您输入的路径来执行此操作。我怎样才能从我给出的代码中完成这项工作。我需要从 javapath 变量中获取输入并将其用于File inFile = new File (getJavapath());

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.swing.*;


public class P_Supplemental_10 extends JFrame {

private JPanel javapanel = new JPanel();
private JButton javareader = new JButton("Click here to get file");
private JTextField javapath = new JTextField();
private JLabel javalabel = new JLabel("Enter file name here; ");
private JTextArea javacontents = new JTextArea();

P_Supplemental_10() {

    this.setLayout(new BorderLayout(5, 10));
    javapanel.setLayout(new GridLayout(1,2));
    javapanel.add(javalabel);
    javapanel.add(javapath);
    add(javapanel,BorderLayout.NORTH);
    add(javacontents,BorderLayout.CENTER);

    add(javareader,BorderLayout.SOUTH);
    javareader.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){

                        jbtReadFileActionPerformed(evt);
                    }
    });

}//end of constructor


    private void jbtReadFileActionPerformed(ActionEvent evt){
    try {
        File inFile = new File (getJavapath());

        Scanner input = new Scanner (inFile);
        String fileContents = "";
        while(input.hasNext()){
            fileContents += input.nextLine() + "\n";
        }
            getJavacontents().setText(fileContents);



    } catch (FileNotFoundException ex) {
        Logger.getLogger(P_Supplemental_10.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

public static void main(String[] args){
        P_Supplemental_10 frame = new P_Supplemental_10();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("P_Supplemental_10");
    frame.setSize(625, 60);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);






}//end of main

/**
 * @return the javapanel
 */
public JPanel getJavapanel() {
    return javapanel;
}

/**
 * @param javapanel the javapanel to set
 */
public void setJavapanel(JPanel javapanel) {
    this.javapanel = javapanel;
}

/**
 * @return the javareader
 */
public JButton getJavareader() {
    return javareader;
}

/**
 * @param javareader the javareader to set
 */
public void setJavareader(JButton javareader) {
    this.javareader = javareader;
}

/**
 * @return the javapath
 */
public JTextField getJavapath() {
    return javapath;
}

/**
 * @param javapath the javapath to set
 */
public void setJavapath(JTextField javapath) {
    this.javapath = javapath;
}

/**
 * @return the javalabel
 */
public JLabel getJavalabel() {
    return javalabel;
}

/**
 * @param javalabel the javalabel to set
 */
public void setJavalabel(JLabel javalabel) {
    this.javalabel = javalabel;
}

/**
 * @return the javacontents
 */
public JTextArea getJavacontents() {
    return javacontents;
}

/**
 * @param javacontents the javacontents to set
 */
public void setJavacontents(JTextArea javacontents) {
    this.javacontents = javacontents;
}

}// end of class
4

1 回答 1

0

作为getJavapath()回报,JTextField这必须是:

File inFile = new File(getJavapath().getText());

如果不需要JTextfield.

于 2012-11-05T22:39:32.007 回答