0

我一直在为我的一些朋友制作一个安装程序,用于向 Minecraft 添加模组。
我创建了一个名为 fileChooserCheck 的方法(在 FileHandling 类中)。它的作用是获取用户通过 JFileChooser 定义的安装目录并将其设置为 modDir,或者,如果用户未指定,则将安装目录设置为默认目录,即 File 'defaultDir' . 每当我运行方法 fileChooserCheck 时,它都不会更改 Path modDir,当我尝试调用它(modDir)时,它会返回 null。

我怎样才能使 fileChooserCheck 实际更改 Path modDir?另外,为什么我的安装失败了?(我刚刚完全删除了 modDir 并手动设置了路径,它仍然失败)

如果你在我的代码中看到任何你马虎和/或可以/需要改进的地方,请告诉我,我只用 Swing 编程了几天,而用 Java 编程了将近一周[我只知道另一种编程语言[这是非常基本的)]

主班

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;

import javax.swing.*;

public class MainInstaller extends JFrame {
    private final long serialVersionUID = 1L;

    String moddir;
    Path modDir;
    URI bioForums = URI.create("http://bio-mc.com/forum");
    URI thread = URI.create("http://google.com");
    String username = System.getProperty("user.name");
    String mcDir = "C://Users//" + username + "//AppData//Roaming//.minecraft";
    File defaultDir = new File("C://Users//" + username +          "//AppData//Roaming//.minecraft");
    Font consoleFont = new Font("", Font.PLAIN, 13 + 1/2);
    Font checkFont = new Font("", Font.PLAIN, 14 + 1/2);
    String[] minimapsArray = { "REI's Minimap", "Zan's Minimap" }; //An array of optifine versions, get manually later
    Object minimap;
    int yInterval = 22;
    boolean enabled = false;
    boolean optifine, invtweaks, armorhud, statuseffecthud, tabbychat, spc, chatbubbles, map = true;
    boolean fcLaunched = false;

    JPanel installPanel, modsPanel, consolePanel;
    Box box, box1;
    JLabel title;
    JLabel modsel;
    JLabel author;
    JTabbedPane tabbedpane = new JTabbedPane();
    JComboBox<?> minimaps; //The drop down menus to select the versions of optifine and invtweaks
    JButton installButton;
    JButton fileButton, forumButton, threadButton;
    JTextArea console;
    JTextField mcdir; 
    JFileChooser fc;

    public MainInstaller() { //MainInstaller Constructor
        super("BlackCrew ModPack Installer"); //Sets the title of the frame

        //Defines the title a bit more and adds it to the box
        box = Box.createVerticalBox();
        title = new JLabel("BlackCrew Modpack");
        title.setFont(new Font("Impact", Font.PLAIN, 38 + 1/2));
        title.setAlignmentX(CENTER_ALIGNMENT);
        box.add(title);

        //The First Tab(Installer)
        installPanel = new JPanel();
        installPanel.setLayout(null);
        Insets insets = installPanel.getInsets();
        installButton = new JButton("Install!");
        installButton.setFont(new Font("", Font.PLAIN, 20));
        Dimension size = installButton.getPreferredSize();

        mcdir = new JTextField();
        mcdir.setText("C:/Users/" + username + "/AppData/Roaming/.minecraft");
        mcdir.setFont(new Font("", Font.PLAIN, 13));
        size = mcdir.getPreferredSize();
        mcdir.setBounds(139 + insets.left, 80 + insets.top, size.width + 5,    size.height);
        mcdir.setEditable(false);

        fileButton = new JButton("...");
        size = fileButton.getPreferredSize();
        fileButton.setBounds(mcdir.getX() + 280, mcdir.getY() - 1 + insets.top, size.width, size.height + 3);   
        fileButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                fcLaunched = true;
                //Create the FileChooser and define it a bit    
                if (fc == null) {
                        fc = new JFileChooser("C://Users//" + username + "//AppData//Roaming//.minecraft");
                            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    }

                //Show the file Chooser
                int returnVal = fc.showOpenDialog(fileButton);

                fc.setSelectedFile(defaultDir);

                //modDir = moddir.toPath();

                //Process the results
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    mcdir.setText(fc.getSelectedFile().getPath());
                } 
            }
        });

        forumButton = new JButton("<html><font color=green>BioCraft</font> <font  color=black>Forums</font></html>");
        forumButton.setFont(new Font("", Font.PLAIN, 13));
        size = forumButton.getPreferredSize();
        forumButton.setBounds(1 - 1/2 + insets.left, 10 + insets.top, size.width, size.height);
        forumButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Desktop.getDesktop().browse(bioForums);
                } catch (IOException e1) {
                    e1.printStackTrace();
                    System.out.println("Failed to Launch the BioCraft Forums");
                }
            }
        });

        threadButton = new JButton("<html><font color=blue>Installer</font> <font color=black>Thread</font></html>");
        threadButton.setFont(new Font("", Font.PLAIN, 13));
        size = threadButton.getPreferredSize();
        threadButton.setBounds(459 + insets.left, forumButton.getY() + insets.top, size.width, size.height);
        threadButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Desktop.getDesktop().browse(thread);
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("Failed to Launch the Installer Thread");
                }
            }
        });

        author = new JLabel("<html><font color=black>Developed by</font> <font color=red>n1ghtk1n9</font></html>");
        author.setFont(new Font("", Font.PLAIN, 13));
        author.setForeground(Color.RED);
        author.setBounds(235 + insets.left, 1 + insets.top, size.width + 20, size.height);

        installButton.setBounds(139 + insets.left, mcdir.getY() + 50 + insets.top, mcdir.getWidth() + fileButton.getWidth(), size.height + 40);

        installPanel.add(author);
        installPanel.add(forumButton);
        installPanel.add(threadButton);
        installPanel.add(installButton);
        installPanel.add(mcdir);
        installPanel.add(fileButton);

        //The Second Tab(Mod Selection)
        modsPanel = new JPanel();
        modsPanel.setLayout(null);
        insets = modsPanel.getInsets();
        JLabel modsel = new JLabel("Select the mods you installed");
        modsel.setFont(new Font("", Font.BOLD, 15));
        size = modsel.getPreferredSize();
        modsel.setBounds(5 + insets.left, 2 + insets.top, size.width + 15, size.height); 

        //Adds all the mod checkboxes
        JCheckBox optiBox = new JCheckBox("Optifine");
        optiBox.setFont(checkFont);
        size = optiBox.getPreferredSize();
        optiBox.setBounds(2 + 1/2 + insets.left, 2 + yInterval + insets.top, size.width, size.height); //Y = 23
        optiBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                optifine = !optifine;
            }
        });

        JCheckBox invBox = new JCheckBox("InvTweaks");
        invBox.setFont(checkFont);
        size = invBox.getPreferredSize();
        invBox.setBounds(2 + 1/2 + insets.left, 2 + (yInterval*2) + 1/2 + insets.top, size.width, size.height); //Y = 44
        invBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                invtweaks = !invtweaks;
            }
        });

        JCheckBox armorhudBox = new JCheckBox("Armor HUD"); //Also automatically installs bspkrs-core
        armorhudBox.setFont(checkFont);
        size = armorhudBox.getPreferredSize();
        armorhudBox.setBounds(2 + 1/2 + insets.left, 2 + (yInterval*3) + insets.top, size.width, size.height); //Y = 65
        armorhudBox.setToolTipText("Also installs 'bskprs core'");
        armorhudBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                armorhud = !armorhud;
            }
        });

        JCheckBox statusBox = new JCheckBox("Status Effect HUD"); //Also automatically installs bspkrs-core
        statusBox.setFont(checkFont);
        size = statusBox.getPreferredSize();
        statusBox.setBounds(2 + 1/2 + insets.left, 2 + (yInterval*4) + insets.top, size.width, size.height); //Y = 86
        statusBox.setToolTipText("Also installs 'bskprs core'");
        statusBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statuseffecthud = !statuseffecthud;
            }
        });

        JCheckBox tabbyBox = new JCheckBox("TabbyChat");
        tabbyBox.setFont(checkFont);
        size = tabbyBox.getPreferredSize();
        tabbyBox.setBounds(2 + 1/2 + insets.left, 2 + (yInterval*5) + insets.top, size.width, size.height); //Y = 108
        tabbyBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tabbychat = !tabbychat;

            }
        });

        JCheckBox spcBox = new JCheckBox("SinglePlayerCommands");
        spcBox.setFont(checkFont);
        size = spcBox.getPreferredSize();
        spcBox.setBounds(2 + 1/2 + insets.left, 2 + (yInterval*6) + insets.top, size.width, size.height);
        spcBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                spc = !spc;

            }
        });

        JCheckBox mapBox = new JCheckBox("MiniMap");
        mapBox.setFont(checkFont);
        size = mapBox.getPreferredSize();
        mapBox.setBounds(2 + 1/2 + insets.left, 2 + (yInterval*7) + insets.top, size.width, size.height);
        //map = mapBox.isSelected();
        mapBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enabled = !enabled;
                minimaps.setEnabled(enabled);
                map = !map;

            }
        });

        JCheckBox bubbleBox = new JCheckBox("Chat Bubbles");
        bubbleBox.setFont(checkFont);
        size = bubbleBox.getPreferredSize();
        bubbleBox.setBounds(2 + 1/2 + insets.left, 2 + (yInterval*8) + insets.top, size.width, size.height);
        bubbleBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                chatbubbles = !chatbubbles;

            }
        });

        minimaps = new JComboBox<Object>(minimapsArray);
        minimaps.setFont(checkFont);
        size = minimaps.getPreferredSize();
        minimaps.setBounds(83 + insets.left, 7 - 31/32 - 1/32 + (yInterval*7) + insets.top, size.width, size.height - 6 - 1/2);
        minimaps.setEnabled(false);
        minimap = minimaps.getSelectedItem();

        //The Third Tab(Console)
        consolePanel = new JPanel();
        consolePanel.setLayout(null);
        console = new JTextArea("Launched the BlackCrew ModPack Installer!", 600, 400);
        console.setFont(new Font("", Font.PLAIN, 13 + 1/2));
        console.setBounds(0, 0, 600, 400);
        console.setEditable(false);
        console.setWrapStyleWord(true);
        console.setLineWrap(true);

        consolePanel.add(console);
        console.setCaretPosition(console.getDocument().getLength());

        modsPanel.add(modsel);
        modsPanel.add(optiBox);
        modsPanel.add(invBox);
        modsPanel.add(armorhudBox);
        modsPanel.add(statusBox);
        modsPanel.add(tabbyBox);
        modsPanel.add(mapBox);
        modsPanel.add(spcBox);
        modsPanel.add(bubbleBox);
        modsPanel.add(minimaps);

        tabbedpane.addTab("Installer", installPanel);
        tabbedpane.addTab("Mod Selection", modsPanel);
        tabbedpane.addTab("Console", consolePanel);

        //Add everything to the frame
        add(box, BorderLayout.NORTH);
        add(tabbedpane);
    }

    public static void main(String[] args) throws IOException { //Main Method
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        } catch(UnsupportedLookAndFeelException e) {
            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } catch (InstantiationException e) {
            e.printStackTrace();

        } catch (IllegalAccessException e) {
            e.printStackTrace();

        }

        final MainInstaller frame = new MainInstaller();
        Insets insets = frame.getInsets();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);

        final FileHandling file = new FileHandling();
        file.createBaseFolder();

        frame.installButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(frame.optifine == false && frame.invtweaks == false && frame.armorhud == false && frame.chatbubbles == false && frame.spc == false && frame.statuseffecthud == false) {
                    JOptionPane.showMessageDialog(frame, "No mods are enabled");
                } else {
                    // (?) Install mods here?
                    try {
                        file.copyMods();
                        System.out.println("Installing Mods");
                        System.out.println("Source: " + file.invSource);
                        System.out.println("Target:" + file.invTarget);

                    } catch (IOException e1) {
                        e1.printStackTrace();
                        System.out.println("Error!" + e);

                    }
                }
            }});
    }
}

文件处理类

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.nio.file.*;
import java.nio.file.StandardCopyOption.*;

public class FileHandling {
    MainInstaller mi = new MainInstaller();

    Path modDir = mi.modDir;
    Path source = Paths.get("C://Users//Gannon//Desktop//Java//workspace//ModPack Installer//test.txt");
    Path target = Paths.get("C://Users");

    Path invSource = Paths.get("mods//invtweaks.jar");
    Path invTarget = Paths.get(mi.modDir + "//InventoryTweaks-MC1.6.2-1.55-b56.jar");

    public void createBaseFolder() {
        try {
            System.out.println(mi.moddir);

        } catch(Exception e) {
            System.out.println("Folder Creation Failed!"); // (?)Remove and   replace with in-program error

        }
    }

    public void fileChooserCheck() {
        if(mi.fcLaunched == false) {
            mi.modDir = mi.defaultDir.toPath();
            System.out.println("Unchanged: " + mi.modDir);
        } else if(mi.fcLaunched == true) {
            mi.modDir = mi.fc.getSelectedFile().toPath();
            System.out.println("Changed: " + mi.modDir);
        }
    }

    public void copyMods() throws IOException {
        fileChooserCheck();
        try {
            if(mi.invtweaks == true) {
                Files.copy(invSource, invTarget);
                System.out.println("Trying to copy!");
            }

        } catch (IOException e) {
            System.out.println("The Copying Failed!" + e);
        }
    }
}
4

1 回答 1

0

您在路径中使用了两个正斜杠 ( // ):

("C://Users//Gannon//Desktop//Java//workspace//ModPack Installer//test.txt");

使用单个正斜杠或使用两个反斜杠,因为您需要转义那些 ( \\ )。
我希望这有帮助。

于 2014-05-31T06:15:10.180 回答