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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2432708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:34:21  来源:igfitidea点击:

What is Form load event handler in Java?

java

提问by Ahmad Farid

What is the event handler in Java (using net beans) that resembles the From_Load in C#?

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

采纳答案by Martin Milan

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 方法看起来像您想要的位置...

回答by Sajad NasiriNezhad

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");
        }
    });
}