2013-04-03 45 views
3

訪問文本區域我將如何能夠從運行這個功能我的主要()來構建GUI,然後使用代碼從其他地方來處理按一下按鈕,並從文本字段檢索輸入?能夠從不同的類

package main; 

import javax.swing.*; 
import java.awt.*; 

public class Gui { 

public static void mainGUI() { 
    UIManager.put("swing.boldMetal", Boolean.FALSE); 
    java.net.URL imgApp = ClassLoader.getSystemResource("res/app.png"); 
    JFrame mainWin = new JFrame("jIRC"); 
    mainWin.setSize(1024, 720); 
    mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    mainWin.setIconImage(new ImageIcon(imgApp).getImage()); 

    Container panel = mainWin.getContentPane(); 
    panel.setLayout(null); 

    JTextArea inputBox = new JTextArea(); 
    inputBox.setSize(300, 100); 
    inputBox.setLocation(500, 250); 

    JButton sendButton = new JButton(); 
    sendButton.setText("Send"); 
    sendButton.setFont(new Font("Helvetica", Font.ITALIC, 16)); 
    sendButton.setSize(72, 32); 
    sendButton.setLocation(500, 500); 

    panel.add(inputBox); 
    panel.add(sendButton); 
    mainWin.setVisible(true); 
} 
} 

這裏是我的類主要功能:

public class Run{ 

public static void main(String[] args) { 

    main.Debug.startupDebug(); 
    main.Gui.mainGUI(); 
} 
} 

我將如何去把我的一些代碼在非靜態字段?

回答

4

你已經掌握了所有靜態方法,並且這不會讓你使用任何面向對象編程的力量。考慮創建符合OOP的類非靜態字段和方法以及公共getter和setter方法,這樣其他類可以影響此類的行爲。

編輯
您發佈的評論:

public class Run{ 
    public static void main(String[] args) { 
    main.Debug.startupDebug(); 
    main.Gui.mainGUI(); 
    } 
} 

但是,你需要做的,而不是什麼是一樣的東西:

public class Run{ 
    public static void main(String[] args) { 
    GUI gui = new GUI(); 
    Debug debug = new Debug(); 

    debug.setGui(gui); 
    gui.setDebug(debug); 

    gui.startGui(); 
    } 
} 

或類似的東西。再次避免使用靜態的東西。

+0

編輯成我的主要職務 – 2013-04-03 23:07:40

+0

@DrZarreh:請將此代碼爲編輯張貼到你的問題,因爲代碼不會在評論格式化好。 – 2013-04-03 23:08:23

+0

@DrZarreh:請參閱編輯。 – 2013-04-03 23:11:59