Java中的事件处理程序(使用网络bean)在C#中类似于From_Load是什么?
答案 0 :(得分:11)
如果你正在使用Swing的JFrame,请尝试使用addWindowListener(继承自java.awt.Window)
监听器的windowOpened方法看起来就像你想要的那样......
答案 1 :(得分:2)
这个简单的样本很有用。
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");
}
});
}
答案 2 :(得分:0)
虽然接受的答案基于底层 WindowListener
接口(这迫使您为您可以使用的每种类型的事件提供实现),但您可以在其中覆盖相应的事件的 WindowAdapter有兴趣。
public class MySwingJFrame extends JFrame {
public MySwingJFrame() {
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
// do something
}
});
}
}