代码之家  ›  专栏  ›  技术社区  ›  Didi1995

在swing java中传递文件路径

  •  0
  • Didi1995  · 技术社区  · 3 年前

    我编写了一个简短的程序,获取文件的路径,然后对其执行一些操作(交换行、交换字,然后将新文本保存在同一个文本文件中)。它是有效的,它走了这条路,我在那里没有任何问题。然后,我尝试创建一个简单的Java Swing接口,并尝试使用JTextField传递文件路径。这给了我一个错误:

    文件名、目录名或卷标语法不正确

    这是我在程序中使用的代码:

    import java.io.*;
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class Fns {
    
        //Indexes the words and lines in the file by adding them to a hashmap of String arrays
        public HashMap<Integer,String[]> Separator(String file) throws IOException {
    
                FileReader fileReader =
                        new FileReader(file);
    
                BufferedReader bufferedReader =
                        new BufferedReader(fileReader);
    
                List<String> lines = new ArrayList<String>();
                String line;
                while((line=bufferedReader.readLine()) != null){
                    lines.add(line);
                }
                String[] data = lines.toArray(new String[]{});
    
                String singleData[];
                HashMap<Integer,String[]> map=new HashMap<Integer, String[]>();
                for(int i=0;i< data.length;i++){
                    singleData=data[i].split(" ");
                        map.put(i,singleData);
                }
    
            return map;
        }
    
        //Swaps Lines
        public HashMap<Integer,String[]>lineChanger(int i, int j, HashMap<Integer,String[]> map){
            String[] temp= map.get(j);
            map.put(j, map.get(i));
            map.put(i,temp);
            return map;
        }
    
        //Swaps Words
        public HashMap<Integer,String[]> wordChanger(int i,int j,int k,int l,HashMap<Integer,String[]> map){
            String[] temp1=map.get(i);
            String[] temp2=map.get(k);
            String temp11=temp1[j];
            String temp22=temp2[l];
            temp1[j]=temp22;
            temp2[l]=temp11;
            map.put(i,temp1);
            map.put(k,temp2);
            return map;
        }
    
        //updates the file with the applied changes
        public void fileChanger(HashMap<Integer,String[]> map,String file) throws IOException {
            PrintWriter deleter=new PrintWriter(file);
            deleter.print("");
            deleter.close();
            FileWriter fw=new FileWriter(file,true);
            BufferedWriter bw=new BufferedWriter(fw);
            PrintWriter pw=new PrintWriter(bw);
    
            for(Integer key: map.keySet()){
                String[] values=map.get(key);
                pw.println(Arrays.stream(values).collect(Collectors.joining(" ")));
                pw.flush();
            }
    
                pw.close();
                bw.close();
                fw.close();
            }
        }
    

    这是我在界面上使用的代码:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.util.HashMap;
    
    public class GUI extends Fns implements ActionListener {
        //Buttons,Labels and Textfields
        static JButton Word_Swap=new JButton("Word Swap");
        static JButton Line_Swap=new JButton("Line Swap");
        static JButton Save=new JButton("Save");
        static JTextField FileField=new JTextField(15);
        static JTextField L1LS=new JTextField(15);
        static JTextField L2LS=new JTextField(15);
        static JTextField L1WS=new JTextField(15);
        static JTextField L2WS=new JTextField(15);
        static JTextField W1=new JTextField(15);
        static JTextField W2=new JTextField(15);
    
        public GUI(JPanel panel,JFrame frame,GridBagConstraints gbc){
            //panel and frame config
            panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
            panel.setLayout(new GridBagLayout());
            frame.add(panel,BorderLayout.CENTER);
            frame.setSize(100,100);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("File Processing Application");
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            GridBagConstraints gbc=new GridBagConstraints();
            JPanel panel=new JPanel();
            JFrame frame=new JFrame();
            GUI gui=new GUI(panel,frame,gbc);
    
            gui.addLabel("Open File ", panel,1,1,1,1,gbc,0,0 );
            gui.addTextField( panel,1,1,1,1,gbc,1,0 , gui.FileField);
            gui.addLabel("Line Swap:", panel,20,2,2,2,gbc,0,1 );
    
            gui.addLabel("Line 1", panel,20,0,1,1,gbc,0,2 );
            gui.addTextField( panel,20,0,1,1,gbc,1,2, gui.L1LS );
    
            gui.addLabel("Line 2", panel,20,0,1,1,gbc,0,3 );
            gui.addTextField( panel,20,0,1,1,gbc,1,3, gui.L2LS );
    
            gui.addButton( panel,40,4,1,1,gbc,1,4, gui.Line_Swap);
    
            gui.addLabel("Word Swap:", panel,20,2,2,2,gbc,0,5 );
    
            gui.addLabel("Line 1", panel,20,0,1,1,gbc,0,6 );
            gui.addTextField(panel,20,0,1,5,gbc,1,6 , gui.L1WS);
    
            gui.addLabel("Word 1", panel,20,0,1,20,gbc,2,6 );
            gui.addTextField(panel,20,0,1,1,gbc,3,6, gui.L2WS );
    
            gui.addLabel("Line 2", panel,20,0,1,1,gbc,0,7 );
            gui.addTextField( panel,20,0,1,1,gbc,1,7, gui.W1 );
    
            gui.addLabel("Word 2", panel,20,0,1,20,gbc,2,7 );
            gui.addTextField( panel,20,0,1,1,gbc,3,7 ,gui.W2);
    
            gui.addButton( panel,40,4,1,1,gbc,1,8, gui.Word_Swap);
    
            gui.addButton( panel,80,0,1,0,gbc,0,9, gui.Save);
    
            Line_Swap.addActionListener(
                    new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            Fns fns =new Fns();
                            try{
                                HashMap<Integer,String[]> map= fns.Separator(FileField.getText());
                                int i=Integer.parseInt(L1LS.getText());
                                int j=Integer.parseInt(L2LS.getText());
                                HashMap<Integer,String[]> map1=fns.lineChanger(i,j,map);
                                fns.fileChanger(map1,FileField.getText());
                            }
                            catch(IOException exception){
                                System.out.println(exception.getMessage());
                            }
                        }
                    }
            );
        }
    
        //Methods for coordinates setup, and jpannel configuration
        private void addButton(  JPanel panel, int top, int left,int bottom,int right,GridBagConstraints gbc,int x,int y,JButton button) {
            Insets i = new Insets(top, left, bottom, right);
            gbc.insets = i;
            gbc.gridx = x;
            gbc.gridy = y;
            panel.add(button, gbc);
        }
    
        private void addLabel( String name, JPanel panel, int top, int left,int bottom,int right,GridBagConstraints gbc,int x,int y) {
            Insets i = new Insets(top, left, bottom, right);
            gbc.insets = i;
            gbc.gridx = x;
            gbc.gridy = y;
            panel.add(new JLabel(name) , gbc);
        }
    
        private void addTextField(  JPanel panel, int top, int left,int bottom,int right,GridBagConstraints gbc,int x,int y,JTextField textField) {
            Insets i = new Insets(top, left, bottom, right);
            gbc.insets = i;
            gbc.gridx = x;
            gbc.gridy = y;
            panel.add(textField, gbc);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
        }
    
    }
    
    0 回复  |  直到 3 年前