2013-06-04 28 views
2

因此,我一直致力於完成基本佈局的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; 
} 
} 

回答

2

化妝數組作爲私有靜態..對OOP的封裝的緣故,創建一個靜態的吸氣

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

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

那麼你可以通過PlayerShow.getRobbieArray()訪問它

+0

謝謝!我從來沒有想過那個笑 – user2445983

1

一種可能性是將其聲明爲靜態。

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

然後,您可以從任何地方訪問它,如PlayerShow.robbieArray

這是假設你只想要有一個實例PlayerShowrobbieArray

否則,您可以將它傳遞給任何需要作爲其構造函數中的參數或通過setter方法訪問它的類。

+0

所以。換句話說,如果我聲明robbieArray作爲公共靜態在我的主體之上,然後填充它並在我的主體中設置其中的東西。當我去訪問它讓我們可以說框架內,我應該能夠說System.out.println(robbieArray [0] .getRole());並且不會發生運行時錯誤? *只是試了一下,靜態沒有改變。嗯,定居者的方法是什麼? – user2445983

+0

我不認爲它適當的成員作爲靜態,如果多數民衆贊成在沒有必要..它應該是靜態的getter(方法)是隻讀的..正在靜態成員意味着它可以訪問和編輯的任何東西.. –

+0

@ user2445983您需要在我的答案 - 「System.out.println(PlayerShow.robbieArray [0] .getRole());'中顯示類名前綴。 –

相關問題