【问题标题】:How to execute a method of a class from another class?如何从另一个类执行一个类的方法?
【发布时间】:2013-11-14 00:40:15
【问题描述】:

我是 Java Swing 开发的新手,遇到以下问题:

我有这个包含main() 方法的GUI 类:

package com.test.login3;

import org.jdesktop.application.SingleFrameApplication;


public class GUI extends SingleFrameApplication {

    public static void main(String[] args) {
        launch(GUI.class, args);
    }

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        System.out.println("GUIBis ---> startUp()");
        MainFrame mainFrame = new MainFrame();
        mainFrame.setVisible(true);

    }

    @Override
    protected void shutdown() {
        System.out.println("Entered into GUI ---> shutdown()"); 
    }

}

正如你在这个类中看到的,main() 方法只是简单地执行这个操作:

launch(GUI.class, args);

阅读官方文档:launch doc

创建指定应用程序子类的实例,设置 ApplicationContext 应用属性,然后调用新的 应用程序的启动方法。启动方法通常称为 从应用程序的主。 applicationClass 构造函数和 启动方法在事件调度线程上运行。

所以startup() 方法被执行并创建并显示一个新的MainFrame 对象

2) 这是 MainFrame 代码(它扩展了经典的 Swing JFrame):

package com.test.login3;

import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.test.login.LoginFrame;

import net.miginfocom.swing.MigLayout;


public class MainFrame extends JFrame {

    private static final int FIXED_WIDTH = 1000;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 620);

    public MainFrame() {
        super();

        setPreferredSize(INITAL_SIZE);
        setResizable(false);

        setTitle("My Application");
        setLayout(new MigLayout("fill"));

        JButton logOutButton = new JButton("LogOut");

        logOutButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                // Execute when button is pressed:
                System.out.println("You clicked the button");

            }
        });   

        add(logOutButton);

        pack();
        setLocationRelativeTo(null);        // Center the window

    }


}

我的问题是这样的:

正如您在GUI.java 类中所见,定义了shutdown() 方法(即在SingleFrameApplication 抽象类中定义)。这是此方法的文档:shutdown doc

阅读文档:

保存以 mainFrame 为根的组件层次结构的会话状态。

当用户点击在MainFrame 类中声明的JButton 时,我希望执行shutdown() 方法(即在GUI 类中声明)。

您有实现此行为的解决方案吗?

谢谢

安德烈亚

【问题讨论】:

  • 您首先需要 MainFrame 来真正了解您的 GUI 对象。为此,您可能希望将 GUI 对象(“t​​his”)作为参数传递给 MainFrame 的构造函数,然后您可以将其保存到 MainFrame 中的字段。然后,您可以使用事件侦听器或其他技术来调用 MainFrame 的 GUI 实例上的关闭方法。

标签: java swing user-interface


【解决方案1】:

您可以使用 PropertyChanges。让 GUI 实现 PropertyChangeListener。然后让 MainFrame 在单击按钮时触发属性更改。在 GUI 中,会捕获此属性更改并执行关闭命令。请参阅this example 了解更多信息。

类似:

在类 GUI 中:

public class GUI extends SingleFrameApplication implements PropertyChangeListener {

...

MainFrame mainFrame = new MainFrame();
mainFrame.addPropertyChangeListener(this);

...

@Override
public void propertyChange(PropertyChangeEvent arg0) {
    if (arg0.getPropertyName().equals("buttonClicked")) {
        shutdown();
    }
}

然后在MainFrame中

       public void actionPerformed(ActionEvent e)
        {
            // Execute when button is pressed:
            System.out.println("You clicked the button");
            firePropertyChange("buttonClicked", false, true);
        }

【讨论】:

  • mmm 我在我的 GUI 类中添加了一个 PropertyChangeListener,当用户单击按钮时,我该怎么做才能触发属性更改?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 2014-02-07
相关资源
最近更新 更多