我無法訪問JFrame中的幾個組件以使用它們的setText(「...」)方法。我的主要是在一個單獨的班級,因爲實際的程序有很多窗口需要同時管理。從外部類訪問JFrame中的組件
public GameWindow() {
initialize();
gameFrame.setVisible(true);
}
private void initialize() {
gameFrame = new JFrame();
JTextPane gameTextPane = new JTextPane(); // text pane to contain all game text
gameTextPanel.add(gameTextPane);
這是我的主:
public class GameMain {
public static GameWindow gW = new GameWindow();
//I have tried using the code below with various numbers, but the "setText()" method is never available
gW.getGameFrame().getContentPane().getComponent(x);
}
我試圖從獨立的類設置的這個文本,但我不能訪問該組件。 最後,最終的代碼應該是這個樣子:
public static void main(String[] args) {
// make the GUI and initilize
changeTheText();
}
public static void changeTheText() {
[CODE TO ACCESS TEXTFIELD].setText("Hello World");
}
我都試過,我發現周圍尋找許多不同的方法,但我實在不明白其中的任何,和他們都不仍然讓我訪問我需要的方法。
默認有getsetters或約構造有一些變化,爲更好地幫助越早發佈的[SSCCE(HTTP:/ /sscce.org/),簡短,可運行,可編譯,僅僅是關於JFrame – mKorbel
*「實際的程序有很多窗口」*參見[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/ a/9554657/418556) –