-1

I've been making an installer for a few of my friends for adding mods to Minecraft. I removed all of my static variables and methods from my project(except the main method of course) and just added objects to the main method so I could call methods from other classes(without the methods in the other classes being static[Thanks to MadProgrammer for suggesting this to me :D]) And now when I try to call a boolean in my seperate class(FileHandling) it returns false no matter what, but if I print out the boolean in the main class it changes between true and false(like it should) Why is this and how can I fix it?

Below is my previous error, which is still occurring, but is not the 'main idea' of this post

I created a method(inside the class FileHandling) called fileChooserCheck. What it does is it gets the directory for installation defined by the user by a JFileChooser and sets that to modDir, or, if the user doesn't specify, it sets the installation directory to the default directory, which is the File 'defaultDir'. Whenever I run the method fileChooserCheck, it doesn't change the Path modDir, when I try to call it(modDir) it returns null.

How can I make it so that fileChooserCheck actually changes the Path modDir?

If you see anything in my code that you is sloppy and/or could/needs to be improved please tell me, I've only been programming with Swing for a few days, and Java for almost a week[I only know one other programming language[which is pretty basic)]

EDIT: Updated and slimmed down the code

Main Class

public class MainInstaller extends JFrame {

//File moddir;
Path modDir;
URI bioForums = URI.create("http://bio-mc.com/forum");
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

    //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(); //Changed what was previously 'moddir.toPath();' into what it is now b/c 'moddir' is a String type, not a File type

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

    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 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;
        }
    });

    modsPanel.add(modsel);
    modsPanel.add(invBox);
    modsPanel.add(mapBox);

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

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

public static void main(String[] args) throws IOException { //Main Method

    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();
    file.copyMods();

    frame.installButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(frame.invtweaks == 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);

                }
            }
        }});
}

}

FileHandling Class

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

Path modDir = mi.modDirPath;
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.modDirPath + "//InventoryTweaks-MC1.6.2-1.55-b56.jar");

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

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

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

}

4

2 回答 2

1

在您的主类中,您定义了 modDir,但您从未为其分配值。您确实有此行,但已将其注释掉:

//modDir = moddir.toPath();

另外我想警告你,有两个变量名称为 name,但不同的大小写是一个坏主意:

String moddir;
Path modDir;

更好的是:

String modDir
Path modDirPath
于 2013-07-24T20:36:41.630 回答
1

我在准确了解这里发生的事情时遇到了一点麻烦,但我有点担心您正在创建 MainInstaller 类的两个完全独立的实例:一个('frame')在 MainInstaller 的 run 方法中创建,第二个('mi') 被创建为 FileHandler 类定义中的一个字段。由于 MainInstaller 中的所有字段都不是静态的,因此通过在一个实例中更改布尔值并在另一个实例中检查相同的布尔值,可能会很容易交叉引用。

对此问题的快速测试可能是将某些字段(例如布尔值)指定为静态并适当地引用它们。但是,如果是我,我会强烈考虑将 MainInstaller 类重新定义为单例。我将通过尝试解释我的意思来故意暴露我的经验不足:

  • 在 MainInstaller 类定义中添加一个名为“instance”的 MainInstaller 类型的私有静态字段,将其初始化为 null。
  • 添加一个公共静态方法(没有参数),该方法返回一个名为“getinstance”的 MainInstaller 对象
  • 此方法应检查“实例”字段是否为空 - 如果是,请将其定义为 MainInstaller 的新实例。然后,只需返回“实例”。
  • 然后,任何时候您需要定义/访问 MainInstaller 对象,都可以通过“getInstance”方法来完成。

这里的优点是您仍然可以在必要时使用非静态字段和方法,但请确保您没有在您不期望它们的地方浮动多个实例,我愿意打赌您没有' t 打算创建 MainInstaller 的多个实例。

  • 我忘了提到,通常你也会将你的构造函数设为私有,只是为了确保没有其他类可以创建额外的实例,而是应该使用 getInstance 方法。
于 2013-07-25T18:01:26.107 回答