为什么我不能将变量从一个.java文件传递给另一个?

时间:2014-01-14 23:29:45

标签: java class user-interface netbeans

我的代码中有一些奇怪的事情发生。我正在制作一个Connect Four游戏,但是它可以让你拥有最多8个团队,每个团队有4个玩家。有三个单独的GUI类文件,每个文件都有自己的布局等。但是,尽管我付出了最大努力,但我无法将SettingsGUI文件中的变量传递给TeamSettingsGUI文件。奇怪的是,我是如何初始化变量,或者让getXXX方法返回一定数量并且它工作正常。但由于某种原因,我无法传递变量的值。

我现在处理的两个变量是numTeams和playersPerTeam。如果我能理解如何使其中一个工作,我想我可以让其他人工作。我确信这很简单,但我很难过。 这是int ital GUI,SettingsGUI:

package connectfouradvanced;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SettingsGUI extends JPanel implements ActionListener {

JLabel saveLabel = new JLabel("Saved. Please exit this GUI.");
JButton save = new JButton("Save");

    JLabel numTeamsLabel = new JLabel("Chose the number of teams.");
    Choice numTeamsChoice = new Choice();

    JLabel boardDimensionsLabel = new JLabel("Chose the board dimensions.");
    Choice boardDimensionsChoice = new Choice();

    JLabel numPlayersPerTeamLabel = new JLabel("Chose the number of players per team.");
    Choice numPlayersPerTeamChoice = new Choice();

    JLabel powerupsLabel = new JLabel("Powerups On/Off?");
    JCheckBox powerups = new JCheckBox();

    JLabel standardLabel = new JLabel("Standard Setup?");
    JCheckBox standardSettings = new JCheckBox();

    int numTeams, boardRows, boardColumns, playersPerTeam; 
    //if you were to put numTeams = 4, then 4 would be passed to TeamSettingsGUI
    boolean powerupsSelected = false, standardSelected;

public SettingsGUI() {
    add(saveLabel);
    saveLabel.setVisible(false);

    add(save);
    save.addActionListener(this);

    add(numTeamsLabel);
    add(numTeamsChoice);
    numTeamsChoice.add("2");
    numTeamsChoice.add("3");
    numTeamsChoice.add("4");
    numTeamsChoice.add("5");
    numTeamsChoice.add("6");
    numTeamsChoice.add("7");
    numTeamsChoice.add("8");

    add(boardDimensionsLabel);
    add(boardDimensionsChoice);
    boardDimensionsChoice.add("7x6");
    boardDimensionsChoice.add("14x12");
    boardDimensionsChoice.add("21x18");
    boardDimensionsChoice.add("28x24");
    boardDimensionsChoice.add("35x30");
    boardDimensionsChoice.add("42x36");
    boardDimensionsChoice.add("49x42");
    boardDimensionsChoice.add("56x48");
    boardDimensionsChoice.add("63x54");
    boardDimensionsChoice.add("70x60");

    add(numPlayersPerTeamLabel);
    add(numPlayersPerTeamChoice);
    numPlayersPerTeamChoice.add("1");
    numPlayersPerTeamChoice.add("2");
    numPlayersPerTeamChoice.add("3");
    numPlayersPerTeamChoice.add("4");

    add(powerupsLabel);
    add(powerups);
    powerups.setSelected(true);

    add(standardLabel);
    add(standardSettings);
}

@Override
protected void paintComponent(Graphics g) { 
    super.paintComponent(g);

    saveLabel.setLocation(150, 100);
    save.setLocation(315, 90);

    numTeamsLabel.setLocation(0, 0);
    numTeamsChoice.setLocation(170, 0);
    numTeamsChoice.setSize(40, 15);

    boardDimensionsLabel.setLocation(0, 25);
    boardDimensionsChoice.setLocation(170, 25);
    boardDimensionsChoice.setSize(80, 15);

    numPlayersPerTeamLabel.setLocation(0, 50);
    numPlayersPerTeamChoice.setLocation(225, 50);
    numPlayersPerTeamChoice.setSize(40, 15);

    powerupsLabel.setLocation(0, 75);
    powerups.setLocation(110, 75);

    standardLabel.setLocation(0, 100);
    standardSettings.setLocation(110, 100);
}

public void saveSettings() {
    switch(numTeamsChoice.getSelectedIndex()) {
        case 0: numTeams = 2; break;
        case 1: numTeams = 3; break;
        case 2: numTeams = 4; break;
        case 3: numTeams = 5; break;
        case 4: numTeams = 6; break;
        case 5: numTeams = 7; break;
        case 6: numTeams = 8; break;
    }

    switch(boardDimensionsChoice.getSelectedIndex()) {
        case 0: boardColumns = 7; boardRows = 6; break;
        case 1: boardColumns = 14; boardRows = 12; break;
        case 2: boardColumns = 21; boardRows = 18; break;
        case 3: boardColumns = 28; boardRows = 24; break;
        case 4: boardColumns = 35; boardRows = 30; break;
        case 5: boardColumns = 42; boardRows = 36; break;
        case 6: boardColumns = 49; boardRows = 42; break;
        case 7: boardColumns = 56; boardRows = 48; break;
        case 8: boardColumns = 63; boardRows = 54; break;
        case 9: boardColumns = 70; boardRows = 60; break;
    }

    switch(numPlayersPerTeamChoice.getSelectedIndex()) {
        case 0: playersPerTeam = 1; break;
        case 1: playersPerTeam = 2; break;
        case 2: playersPerTeam = 3; break;
        case 3: playersPerTeam = 4; break;
    }

    if(powerups.isSelected() == true) {
        powerupsSelected = true;
    }

    if(standardSettings.isSelected() == true) {
        standardSelected = true;
        powerupsSelected = false;
        numTeams = 2;
        boardColumns = 7;
        boardRows = 6;
        playersPerTeam = 1;
    }

    saveLabel.setVisible(true);

    System.out.println("Using standard settings? " + standardSelected);
    System.out.println("Powerups enabled? " + powerupsSelected);
    System.out.println(numTeams + " " + boardColumns + " " + boardRows + " " + playersPerTeam);
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(save)) {
        saveSettings();
    }
}

public int getBoardColumns() {
    return boardColumns;
}

public int getBoardRows() {
    return boardRows;
}

public int getNumTeams() {
    return numTeams;
    //again, put return 4; here and it will return 4 to TeamSettingsGUI
}

public int getPlayersPerTeam(){
    return playersPerTeam;
}

public boolean getPowerupsEnabled() {
    return powerupsSelected;
}

public boolean getStandardEnabled() {
    return standardSelected;
}

}

现在请记住,在初始化numTeams和playersPerTeam的地方,它可以完美地传递给TeamSettingsGUI。我有一种感觉,在saveSettings()中它没有保存变量,它只是暂时的。如果是,那么我不知道如何解决它。我在切换机箱中尝试了像this.numPlayers这样的东西。

这是TeamSettingsGUI:

package connectfouradvanced;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class TeamSettingsGUI extends JPanel implements ActionListener {

    SettingsGUI settings = new SettingsGUI();
    int numTeams = settings.getNumTeams();
    int playersPerTeam = settings.getPlayersPerTeam();

    JButton saveTeam = new JButton();

    Color[] teamColors = new Color[numTeams];
    String[][] playerNames = new String[numTeams][playersPerTeam];
    char[][] playerSymbols = new char[numTeams][playersPerTeam];

    Choice[] colors = new Choice[numTeams];
    String[] colorList = {"Red", "Yellow", "Orange", "Green", "Black", "Blue", "Cyan",     "Magenta"};

    JCheckBox[] ready = new JCheckBox[numTeams];
    JLabel[] teamNumbers = new JLabel[numTeams];
    JTextField[][] playerNamesInput = new JTextField[numTeams][playersPerTeam];
    JTextField[][] playerSymbolsInput = new JTextField[numTeams][playersPerTeam];

public TeamSettingsGUI() {
    add(saveTeam);
    saveTeam.addActionListener(this);

    for(int i = 0; i < teamNumbers.length; i++) {
        teamNumbers[i] = new JLabel();
        teamNumbers[i].setText("Team " + (i + 1));
        add(teamNumbers[i]);
    }

    for(int i = 0; i < colors.length; i++) {
        colors[i] = new Choice();
        colors[i].add("Red");
        colors[i].add("Yellow");
        colors[i].add("Orange");
        colors[i].add("Green");
        colors[i].add("Black");
        colors[i].add("Blue");
        colors[i].add("Cyan");
        colors[i].add("Magenta");
        colors[i].select(colorList[i]);
        add(colors[i]);
    }

    for(int i = 0; i < playerNamesInput.length; i++) {
        for(int k = 0; k < playerNamesInput[i].length; k++) {
            playerNamesInput[i][k] = new JTextField();
            playerNamesInput[i][k].setText("Team " + (i+1) + " player " + (k+1) + " name: ");
            add(playerNamesInput[i][k]);
        }
    }

    for(int i = 0; i < playerSymbolsInput.length; i++) {
        for(int k = 0; k < playerSymbolsInput[i].length; k++) {
            playerSymbolsInput[i][k] = new JTextField();
            playerSymbolsInput[i][k].setText("Team " + (i+1) + " player " + (k+1) + " symbol: ");
            add(playerSymbolsInput[i][k]);
        }
    }

    for(int i = 0; i < ready.length; i++) {
        ready[i] = new JCheckBox();
        ready[i].setText("Ready?");
        add(ready[i]);
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(saveTeam)) {

    }
}

@Override
protected void paintComponent(Graphics g) { 
    super.paintComponent(g);
    System.out.println(numTeams + " " + playersPerTeam);
}

注意,TeamSettingsGUI尚未完成。在我解决这个问题之前,我无法继续调整事物。 对不起,如果我的代码很脏或者什么,但我只是想弄清楚发生了什么。

忘记包含主可执行类,还有另一个缺少的BoardGUI类,现在是空的。

    package connectfouradvanced;
    import javax.swing.*;
    public class ConnectFourAdvanced {

    public static void main(String[] args) {
    SettingsGUI s = new SettingsGUI();
    BoardGUI b = new BoardGUI();
    TeamSettingsGUI t = new TeamSettingsGUI();

    JFrame fs = new JFrame();
    JFrame fb = new JFrame();
    JFrame ft = new JFrame();
    fs.add(s);
    fb.add(b);
    ft.add(t);

    fs.setVisible(true);
    fs.setResizable(false);
    fs.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    fs.setLocationRelativeTo(null);
    fs.setSize(400, 150);
    fs.setTitle("Settings");

    while(fs.isVisible() == true) {
        ft.setVisible(false);
    }

    ft.setVisible(true);
    ft.setResizable(false);
    ft.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    ft.setLocationRelativeTo(null);
    ft.setSize(1200, 500);
    ft.setTitle("Team Settings");

    while(ft.isVisible() == true || fs.isVisible() == true) {
        fb.setVisible(false);
    }

    fb.setVisible(true);
    fb.setResizable(false);
    fb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fb.setLocation(100, 100);
    fb.setSize(1000, 1000);
    fb.setTitle("Connect Four");
}   

}

我想补充一点,我只有16岁,虽然我在java上并不那么糟糕,但GUI和对象对我来说仍然是个新手。因此,如果我的代码笨重或混乱,或者我正在做一些对其他人来说似乎很简单的非常愚蠢的事情,那是因为我是新手。这是我自己开始的第一个大型GUI项目。

1 个答案:

答案 0 :(得分:1)

您遇到问题的原因是在TeamSettings中,您正在创建一个新的SettingsGUI实例。

想想买一辆新车。如果你有一辆车,然后把所有的东西放进去,然后买了另一辆完全一样的车,新车会不会有你的所有东西?不,至少我希望不是,如果确实让我知道,我们可以一起赚钱...... :)

以下是有问题的一行:

SettingsGUI settings = new SettingsGUI();

我建议你使用Singleton模式来完成你正在做的工作。将您的设置放在那里,然后您可以在任何地方引用它们。有更复杂的选择,但对于你正在做的工作水平,我认为单身人士是你最好的选择。