2013-04-03 105 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