简体   繁体   English

Java 中的表单加载事件处理程序是什么?

[英]What is Form load event handler in Java?

什么是 Java(使用网络 bean)中类似于 C# 中的 From_Load 的事件处理程序?

If you're using Swing's JFrame, try using addWindowListener (inherited from java.awt.Window)如果您使用的是 Swing 的 JFrame,请尝试使用 addWindowListener(从 java.awt.Window 继承)

The listener's windowOpened method looks like where you want to be...侦听器的 windowOpened 方法看起来像您想要的位置...

This simple sample is useful.这个简单的示例很有用。

public static void main(String[] args) {

    JFrame fa = new JFrame();
    fa.setBounds(100, 100, 400, 200);
    fa.setVisible(true);
    fa.addWindowListener(new WindowListener() {

        @Override
        public void windowOpened(WindowEvent e) {
            JOptionPane.showMessageDialog(fa, "windowOpened");
        }

        @Override
        public void windowClosing(WindowEvent e) {
            JOptionPane.showMessageDialog(fa, "windowClosing");
        }

        @Override
        public void windowClosed(WindowEvent e) {
            JOptionPane.showMessageDialog(fa, "windowClosed");
        }

        @Override
        public void windowIconified(WindowEvent e) {
            JOptionPane.showMessageDialog(fa, "windowIconified");
        }

        @Override
        public void windowDeiconified(WindowEvent e) {
            JOptionPane.showMessageDialog(fa, "windowDeiconified");
        }

        @Override
        public void windowActivated(WindowEvent e) {
        //                JOptionPane.showMessageDialog(fa, "windowActivated");
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
        //                JOptionPane.showMessageDialog(fa, "windowDeactivated");
        }
    });
}

Whilst the accepted answer is based on the underlying WindowListener interface (which forces you to provide an implementation for each type of event you can use) the WindowAdapter where you can override the appropriate event you are interested in.虽然接受的答案基于底层WindowListener接口(它强制您为您可以使用的每种类型的事件提供一个实现) WindowAdapter ,您可以在其中覆盖您感兴趣的适当事件。

public class MySwingJFrame extends JFrame {
    public MySwingJFrame() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                // do something
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM