Javaのいくつかのイベントリスニング方法
ここ数日Javaのグラフィックインタフェースのプログラミングを見ていて、授業も本がないので、ほとんど勉強していないので、自分で積み木を作ってよく勉強したいと思っています.書き終わった後、傍受されていくつかの問題が出てきたが、幸い先輩が手伝ってくれてJavaのイベント駆動モデルについて基本的な理解を得たので、JavaでGUIをするのは本当に爽やかなことではないようだ.その後、自分でよくまとめて、次の4つのよく使われる傍受方法を考えました.
import javax.swing. * ; import java.awt.event. * ;//The first method//このメソッドには欠陥があり、本クラスが別のクラスに継承される場合、javaの単一継承メカニズムのため、このクラスはアダプタクラスに継承できません//このメソッドを使用する場合は、アダプタクラスのメソッド名を正しく書くことに注意する必要があります.そうしないと、jdkはpublicを誤報することなく、新しいメソッドとみなされます class JFrameDemo extends WindowAdapter { JFrame jfr; public JFrameDemo(String name) { jfr = new JFrame(name); jfr.setSize( 200 , 100 ); jfr.setVisible( true ); jfr.addWindowListener( this ); } public void windowClosing(WindowEvent e) { System.exit( 0 ); } public static void main(String[] args) { new JFrameDemo( "jframedemo "); } }//The second method public class JFrameDemo extends JFrame { public JFrameDemo(String name) { super (name); setSize( 200 , 100 ); setVisible( true ); addWindowListener( new WindowOP()); } public static void main(String[] args) { new JFrameDemo( "jramedemo "); } } class WindowOP implements WindowListener { public void windowClosed(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit( 0 ); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} }//The third method//通常、この方法は良い//インタフェースWindowListenerを実現する部分をclassに置く JFrameDemoでは、これで1つのクラスしか使用できません//この場合、構築方法にリスニングを追加するオブジェクトはthis自体、すなわちaddWindowListener(this);public class JFrameDemo extends JFrame implements WindowListener { public JFrameDemo(String name) { super (name); setSize( 200 , 100 ); setVisible( true ); addWindowListener( this ); } public void windowClosed(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit( 0 ); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} }//The forth method//内部クラスの方法でWindowListenerインタフェース、すなわちaddWindowListener(new)を実現 WindowListener() { public void windowCloing(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit( 0 ); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) //しかし、この方法は通常、小さなプログラムでしか使用できません.
import javax.swing.*;
import java.awt.event.*;
//The first method
// , , java ,
// , jdk
public class JFrameDemo extends WindowAdapter
{
JFrame jfr;
public JFrameDemo(String name)
{
jfr = new JFrame(name);
jfr.setSize(200,100);
jfr.setVisible(true);
jfr.addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public static void main(String[] args)
{
new JFrameDemo("jframedemo");
}
}
//The second method
public class JFrameDemo extends JFrame
{
public JFrameDemo(String name)
{
super(name);
setSize(200,100);
setVisible(true);
addWindowListener(new WindowOP());
}
public static void main(String[] args)
{
new JFrameDemo("jramedemo");
}
}
class WindowOP implements WindowListener
{
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
//The third method
// ,
// WindowListener class JFrameDemo ,
// this , addWindowListener(this);
public class JFrameDemo extends JFrame implements WindowListener
{
public JFrameDemo(String name)
{
super(name);
setSize(200,100);
setVisible(true);
addWindowListener(this);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
//The forth method
// WindowListener ,
addWindowListener(new WindowListener()
{
public void windowCloing(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
});
//
import javax.swing. * ; import java.awt.event. * ;//The first method//このメソッドには欠陥があり、本クラスが別のクラスに継承される場合、javaの単一継承メカニズムのため、このクラスはアダプタクラスに継承できません//このメソッドを使用する場合は、アダプタクラスのメソッド名を正しく書くことに注意する必要があります.そうしないと、jdkはpublicを誤報することなく、新しいメソッドとみなされます class JFrameDemo extends WindowAdapter { JFrame jfr; public JFrameDemo(String name) { jfr = new JFrame(name); jfr.setSize( 200 , 100 ); jfr.setVisible( true ); jfr.addWindowListener( this ); } public void windowClosing(WindowEvent e) { System.exit( 0 ); } public static void main(String[] args) { new JFrameDemo( "jframedemo "); } }//The second method public class JFrameDemo extends JFrame { public JFrameDemo(String name) { super (name); setSize( 200 , 100 ); setVisible( true ); addWindowListener( new WindowOP()); } public static void main(String[] args) { new JFrameDemo( "jramedemo "); } } class WindowOP implements WindowListener { public void windowClosed(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit( 0 ); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} }//The third method//通常、この方法は良い//インタフェースWindowListenerを実現する部分をclassに置く JFrameDemoでは、これで1つのクラスしか使用できません//この場合、構築方法にリスニングを追加するオブジェクトはthis自体、すなわちaddWindowListener(this);public class JFrameDemo extends JFrame implements WindowListener { public JFrameDemo(String name) { super (name); setSize( 200 , 100 ); setVisible( true ); addWindowListener( this ); } public void windowClosed(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit( 0 ); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} }//The forth method//内部クラスの方法でWindowListenerインタフェース、すなわちaddWindowListener(new)を実現 WindowListener() { public void windowCloing(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit( 0 ); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) //しかし、この方法は通常、小さなプログラムでしか使用できません.