WindowBuilder的GUI和应用程序逻辑交互java?

时间:2014-12-04 13:11:32

标签: java jframe jtextarea windowbuilder

我有一个MainWindow类,它有main方法,它的构造函数和initialize()方法。 initialize()方法有框架,Jbutton和最终的Jtextarea。 actionPerformed()在另一个处理ActionListener的类Data中。我想在Jtextfield中按下按钮后显示一些文本,Jtextfield只在MainWindow类的私有变量框架内。我没有提到应用程序逻辑,帮我与它交互和GUI ..谢谢!!!! !

MainWindow类:

    public class MainWindow {
    private JFrame frame;
    public Data data;

    public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 try {
                    MainWindow window = new MainWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                 }
             }
         });
    }

    public MainWindow() {
         this.data = new Data();
         initialize();
     }

     private void initialize(){

         frame = new JFrame();
         frame.setBounds(100, 100, 396, 469);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().setLayout(null);

         final JTextArea textarea = new JTextArea();
         textarea.setFont(new Font("Dialog", Font.PLAIN, 75));
         textarea.setTabSize(15);
         textarea.setBounds(12, 28, 370, 85);
         frame.getContentPane().add(textarea);

        JButton button7 = new JButton("7");
        button7.addActionListener(this.data);  // Data data class has the actionperformed() method
        button7.setActionCommand("7");
        button7.setBounds(12, 125, 65, 73);
        frame.getContentPane().add(button7);
        }
}

然后类数据是:

    public class Data implements ActionListener {

    public String s;

    public Data(){
        //constructor
    }

    public void actionPerformed(ActionEvent e) {
             // this will set string s with some string 
            // that has to be returned to be displayed
            // in the Jtextarea of the frame in MainWindow
  }

    public string returnString(){
             return s;
    }

我只想在MainWindow类中设置框架变量的JtextArea ..请帮助

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以通过getter或构造函数将TextArea的引用传递给ActionListener。我刚刚使用了getter

public class Data implements ActionListener {
public JTextArea textArea;

public Data() {
    //constructor
}

// here I used setter
public void setTextArea( JTextArea textArea ) {
    this.textArea = textArea;
}

public void actionPerformed(ActionEvent e) {
    // this will work, if you click the button 7
    textArea.setText("Any modification you want");
}
}

在textarea初始化

之后,在initialize()方法中添加此代码

this.data.setTextArea(textarea的);

希望它有所帮助。

<强>更新

public class MainWindow {
private JFrame frame;
public Data data;

final JTextArea textarea;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public class Data implements ActionListener {

public String s;
public MainWindow mainWindow;

public Data( MainWindow mainWindow ) {
    this.mainWindow = mainWindow;
    //constructor
}

public void actionPerformed(ActionEvent e) {
    s = "test";
    // this will set string s with some string
    // that has to be returned to be displayed
    // in the Jtextarea of the frame in MainWindow
    mainWindow.updateTextArea(s);
}

public String returnString() {
    return s;
}
}

这只是一个简单的实现。