2

So I've been working on a GUI that has the basic layout completed. I have the logic all mapped out but the issue I am having is figuring out how to set a filled array to a public variable with all of the String and ints placed in it remaining intact, and not having it kept blank. By no means am I a pro with programming so please be patient if the answer is very obvious. I will post the window that I am trying to get the filled array in a public variable.

the public array 'robbieArray' works soley in the main to output any values from it, but past that I cannot use it in frame. I would just like a solution so that I can access it in frame and other classes I decide to make.

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.beans.*;



public class PlayerShow extends JFrame
{

public JFrame playerShowFrame;

public final int CLASSSIZE1 = 2;//subject to change

 //PlayerInput player = new PlayerInput();

public PlayerInput [] robbieArray = new PlayerInput [CLASSSIZE1];



public void main(String[] args) throws IOException, FileNotFoundException
{
  //    PlayerInput player = new PlayerInput();
Scanner inputRobbieChamps = new Scanner (new FileReader("robbie_mccarthy_champs.txt"));

for (int x = 0; x<CLASSSIZE1;x++)
{
    String champName = inputRobbieChamps.next();
    String role = inputRobbieChamps.next();
    int tier = inputRobbieChamps.nextInt();

    robbieArray[x] = new PlayerInput (champName, role, tier);
System.out.println("The champ name is " + robbieArray[x].getChampName() +     "with a role of " + robbieArray[x].getRole() + " and a tier value of " + robbieArray[x].getTier());   
}
//  PlayerInput [] robbieArray = new PlayerInput[CLASSSIZE1]; 



/*  for (int x = 0; x<CLASSSIZE1; x++)
    {
        robbieArray [x] = new PlayerInput();
    }

    for (int x = 0; x<CLASSSIZE1; x++)
    {
        robbieArray[x].setChampName(inputRobbieChamps.next());
        robbieArray[x].setRole(inputRobbieChamps.next());
        robbieArray[x].setTier(inputRobbieChamps.nextInt());
    }*/
    inputRobbieChamps.close();


}


public PlayerShow ()
{
    frame();
    playerShowFrame.setVisible(false);
}

public void frame()
{
    playerShowFrame = new JFrame();
    playerShowFrame.setVisible(true);
    //playerShowFrame.setSize(900,600);
    playerShowFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    getPlayerShowFrame().setBounds(100, 300, 900, 600);
    playerShowFrame.setLayout(new GridLayout(5,8));

    JLabel titleL = new JLabel ("Robbie McCarthy's data:", SwingConstants.CENTER);

    JLabel blank1L = new JLabel (" ", SwingConstants.CENTER);

    JLabel blank2L = new JLabel (" ", SwingConstants.CENTER);

    JLabel champNameL = new JLabel("Champion Name", SwingConstants.CENTER);

    JLabel roleL = new JLabel ("Role", SwingConstants.CENTER);

    JLabel tierL = new JLabel ("Tier", SwingConstants.CENTER);

    JTextArea hugeTF = new JTextArea ();

    hugeTF.setColumns(6);
    hugeTF.setRows(3);

//      for (int x = 0; x<CLASSSIZE1; x++)
//      {
    System.out.println(robbieArray[0].getRole()); // This is where I am checking to see if i get a filled public array

//      }

    hugeTF.setEditable(false);  

    playerShowFrame = getPlayerShowFrame();

    playerShowFrame.add(titleL);

    playerShowFrame.add(blank1L);

    playerShowFrame.add(blank2L);

    playerShowFrame.add(champNameL);

    playerShowFrame.add(roleL);

    playerShowFrame.add(tierL);

    playerShowFrame.add(hugeTF);

}




public JFrame getPlayerShowFrame() {
    return playerShowFrame;
}

public void setPlayerShowFrame(JFrame playerShowFrame) {
        this.playerShowFrame = playerShowFrame;
}
}
4

2 回答 2

2

make array as private static .. for the sake of encapsulation of OOP, create a static getter

private static PlayerInput [] robbieArray = new PlayerInput [CLASSSIZE1];

public static PlayerInput [] getRobbieArray(){
return robbieArray;
}

then you can access it by PlayerShow.getRobbieArray()

于 2013-06-04T21:46:03.557 回答
1

One possibility would be to declare it as a static.

public static PlayerInput [] robbieArray = new PlayerInput [CLASSSIZE1];

Then you can access it from anywhere as PlayerShow.robbieArray.

That's assuming you only ever want to have one instance of PlayerShow and robbieArray.

Otherwise you could pass it to any classes that need to access it as a parameter in their constructor, or via a setter method.

于 2013-06-04T21:42:01.297 回答