【问题标题】:Passing on a filled array onto a public variable so it can be used in other classes将填充的数组传递给公共变量,以便可以在其他类中使用
【发布时间】:2013-05-31 10:21:28
【问题描述】:

所以我一直在开发一个已经完成基本布局的 GUI。我已经把所有的逻辑都映射出来了,但是我遇到的问题是弄清楚如何将一个填充的数组设置为一个公共变量,其中所有的字符串和整数都保持不变,而不是让它保持空白。我绝不是编程专家,所以如果答案很明显,请耐心等待。我将发布我试图在公共变量中获取填充数组的窗口。

公共数组 'robbieArray' 主要单独工作以从中输出任何值,但过去我无法在框架中使用它。我想要一个解决方案,以便我可以在我决定制作的框架和其他类中访问它。

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

【问题讨论】:

    标签: java arrays class user-interface


    【解决方案1】:

    将数组设为私有静态......为了封装OOP,创建一个静态getter

    private static PlayerInput [] robbieArray = new PlayerInput [CLASSSIZE1];
    
    public static PlayerInput [] getRobbieArray(){
    return robbieArray;
    }
    

    然后你可以通过 PlayerShow.getRobbieArray() 访问它

    【讨论】:

      【解决方案2】:

      一种可能性是将其声明为静态。

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

      然后您可以在任何地方以PlayerShow.robbieArray 访问它。

      假设您只想拥有PlayerShowrobbieArray 的一个实例。

      否则,您可以将它作为构造函数中的参数或通过 setter 方法传递给任何需要访问它的类。

      【讨论】:

      • 所以换句话说,如果我将 robbieArray 声明为我的 main 上方的 public static,然后填充它并在我的 main 中设置内容。当我去访问它时,可以在框架内说它我应该能够说类似 System.out.println(robbieArray[0].getRole());并且不会发生运行时错误? *刚刚尝试过,静态没有任何改变。嗯,什么是定居者方法?
      • 如果没有必要,我认为将成员设置为静态是不合适的。它应该是静态的,因为 getter (方法)是只读的。作为静态成员意味着它可以是访问和编辑任何东西..
      • @user2445983 您需要在其前面加上我在回答中显示的类名 - System.out.println(PlayerShow.robbieArray[0].getRole());
      猜你喜欢
      • 2016-04-16
      • 2012-11-01
      • 2013-06-03
      • 2011-07-09
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2014-02-28
      • 2012-08-17
      相关资源
      最近更新 更多