如何从类文件中发送对主类的调用?

时间:2014-11-19 15:18:16

标签: java class

您好我有1个主程序文件2个类文件。在主类文件中,我创建一个框架并控制它们如何交互。我的一个类文件需要与主类文件对话以更改第三个文件。我正在尝试找出该电话将如何发回。

主类文件:

public class MainFrame extends javax.swing.JFrame {

public MainFrame() {
    buttonPanel1 = new ButtonPanel();
    textPanel1 = new TextPanel();
    textPanel2 = new TextPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(buttonPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(textPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(textPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(buttonPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(textPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(textPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    pack();
}                     

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(() -> {
        new MainFrame().setVisible(true);
    });
}

public void copyText() {
    textPanel2.setText(textPanel1.getText());
}

private ButtonPanel buttonPanel1;
private TextPanel textPanel1;
private TextPanel textPanel2;           

}

尝试回调主文件的按钮类文件。更改第3个文件。

public class ButtonPanel extends javax.swing.JPanel {
    public ButtonPanel() {
        jButton1 = new javax.swing.JButton();
        jButton1.setText("Change");
        jButton1.addActionListener((java.awt.event.ActionEvent evt) -> {


//I would like to tell Main Frame to get TextPanel1 and put into TextPanel2
        MainFrame.copyText();

         });

       javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addComponent(jButton1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addComponent(jButton1))
        );
    }                                                          
    private javax.swing.JButton jButton1;             
}

正在改变:

public class TextPanel extends javax.swing.JPanel {
public TextPanel() {
    jTextField1 = new javax.swing.JTextField();
    jTextField1.setText("Hello");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }                    

    public String getText(){
        return jTextField1.getText();
    }
    public void setText(String str){
        jTextField1.setText(str);
    }                   
    private javax.swing.JTextField jTextField1;      
}

2 个答案:

答案 0 :(得分:0)

如果您将方法设为静态,这将有效,但您将无法直接访问该对象。

MainFrame.copyText();

否则,只需将对mainFrame的引用传递给ButtonPanel即可。例如:

public ButtonPanel(MainFrame mainFrame) {
    this.mainFrame = mainFrame;
...
}

在大型机上创建如下:

new ButtonPanel(this);

答案 1 :(得分:0)

查看MVC(模型 - 视图 - 控制器)范例以分离事物,并保持一些概述。

一个控制器管理所有内容:它保留数据模型和GUI 查看。事件,对模型的更改将传递给控制器​​,然后在视图上调用方法。

Controller类(如Application)只有一个实例,它将是一些通用类,其中包含Application.get().copyText()等方法并持有视图(MainFrame)和模型(数据,字符串) )。

public class Application { // The controller
    private static final Application instance = new Application();
    private MainFrame frame;

    public static Application get() {
        return instance;
    }

    public static void main(String[] args) {
        ...

在Application.copyText中,可能会调用MainFrame方法。有点乒乓球。但是将业务逻辑(控制流)保持在冗长的GUI代码之外。

其他技术是为按钮设置单独的Action类,扩展AbstractAction。