java罕見事宜呼應辦法實例匯總。本站提示廣大學習愛好者:(java罕見事宜呼應辦法實例匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是java罕見事宜呼應辦法實例匯總正文
本文實例匯總了java中罕見的事宜呼應辦法,包含容器類監聽、監聽器類、AbstractAction、反射等。以便利年夜家參考。詳細辦法以下:
起首,在Java圖形用戶界面中,處置事宜時所必需的步調是:
1、創立接收呼應的組件(控件)
2、完成相干事宜監聽接口
3、注冊事宜源的舉措監聽器
4、事宜觸發時的事宜處置
響應的可以經由過程以下的集中方法來作失事件呼應。
1、容器類監聽
後果:單擊窗體中的三個按鈕,完成響應的響應時光。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//聲明 類時,添加“implements ActionListener”完成監聽的接口,假如要對多種監聽方法停止監聽,則用逗號距離開
// 好比“implements ActionListener,KeyListener”
class ButtonListener extends JFrame implements ActionListener{
JButton ok, cancel,exit; //創立接收呼應的組建,就是三個按鈕
public ButtonListener(String title){
super(title);
this.setLayout(new FlowLayout());
ok = new JButton("肯定");
cancel = new JButton("前往");
exit = new JButton("加入");
//上面三個語句 為按鈕分離 注冊監聽器
ok.addActionListener(this);
cancel.addActionListener(this);
exit.addActionListener(this);
getContentPane().add(ok);
getContentPane().add(cancel);
getContentPane().add(exit);
}
//完成 事宜觸發時的事宜處置
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok)
System.out.println("肯定");
if(e.getSource()==cancel)
System.out.println("前往");
if(e.getSource()==exit)
System.exit(0);;
}
public static void main(String args[]) {
ButtonListener pd=new ButtonListener("ActionEvent Demo");
pd.setSize(250,100);
pd.setVisible(true);
}
}
2、監聽類完成
後果:單擊窗體中的三個按鈕,完成響應的響應時光。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonListener1 extends JFrame { //這裡沒有完成監聽
JButton ok, cancel,exit;
public ButtonListener1(String title){
super(title);
this.setLayout(new FlowLayout());
ok = new JButton("肯定");
cancel = new JButton("前往");
exit = new JButton("加入");
ok.addActionListener(new MyListener());
cancel.addActionListener(new MyListener());;
exit.addActionListener(new MyListener());;
getContentPane().add(ok);
getContentPane().add(cancel);
getContentPane().add(exit);
}
public static void main(String args[]) {
ButtonListener pd=new ButtonListener("ActionEvent Demo");
pd.setSize(250,100);
pd.setVisible(true);
}
}
//監聽舉措事宜
class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="肯定")
System.out.println("肯定");
if(e.getActionCommand()=="前往")
System.out.println("前往");
if(e.getActionCommand()=="加入")
System.exit(0);;
}
}
3、應用 AbstractAction類完成監聽
後果:單擊菜單,作出呼應
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
//此類繼續AbstractAction,必需完成actionPerformed()辦法。
class AbstractEvent extends AbstractAction{
//private static final long serialVersionUID = 1L;
AbstractEvent(){
}
public void actionPerformed(ActionEvent e){
//彈出確認對話框
if (e.getActionCommand()=="open"){
JOptionPane.showMessageDialog(null, "翻開");
}else if (e.getActionCommand()=="close"){
JOptionPane.showMessageDialog(null, "封閉");
}else if (e.getActionCommand()=="run"){
JOptionPane.showMessageDialog(null, "運轉");
}else if (e.getActionCommand()=="stop"){
JOptionPane.showMessageDialog(null, "停滯");
}
}
}
public class TestAbstractEvent {
private static JMenuBar menubar;
private static JFrame frame;
//指定MenuEvent的詳細處置法式是AbstractEvent類完成的。
final Action MenuEvent=new AbstractEvent();
public TestAbstractEvent(){
frame=new JFrame("menu");
frame.getContentPane().setLayout(new BorderLayout());
menubar=new JMenuBar();
JMenu menuFile=new JMenu("file");
//實例化一個菜單項,並添加監聽openAction,
JMenuItem menuItemopen=new JMenuItem("open");
menuItemopen.addActionListener(MenuEvent);
JMenuItem menuItemclose=new JMenuItem("close");
menuItemclose.addActionListener(MenuEvent);
menuFile.add(menuItemopen);
menuFile.add(menuItemclose);
JMenu menuTool=new JMenu("tool");
JMenuItem menuItemrun=new JMenuItem("run");
menuItemrun.addActionListener(MenuEvent);
JMenuItem menuItemstop=new JMenuItem("stop");
menuItemstop.addActionListener(MenuEvent);
menuTool.add(menuItemrun);
menuTool.add(menuItemstop);
menubar.add(menuFile);
menubar.add(menuTool);
menubar.setVisible(true);
frame.add(menubar,BorderLayout.NORTH);
frame.setSize(400,200);
frame.setVisible(true);
}
public static void main(String[] args){
new TestAbstractEvent();
}
}
4、 AbstractAction類 + 反射 的辦法
後果:單擊對象欄的三個按鈕,經由過程按鈕的稱號,反射獲得 與按鈕稱號雷同的類完成呼應。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
class ViewAction extends AbstractAction{
private String ActionName="";
//private JFrame frame=null;
private Action action=null;
public ViewAction(){
}
public ViewAction(String ActionName){
this.ActionName=ActionName;
//this.frame=frame;
}
@Override
public void actionPerformed(ActionEvent e) {
Action action=getAction(this.ActionName);
action.execute();
}
private Action getAction(String ActionName){
try{
if (this.action==null){
Action action=(Action)Class.forName(ActionName).newInstance();
this.action=action;
}
return this.action;
}catch(Exception e){
return null;
}
}
}
public class TestAE extends JFrame {
public JToolBar bar=new JToolBar();
String buttonName[]={"b1","b2","b3"};
public TestAE(){
super("事宜");
for (int i=0;i<buttonName.length;i++){
ViewAction action=new ViewAction(buttonName[i]);
JButton button=new JButton(buttonName[i]);
button.addActionListener(action);
bar.add(button);
}
this.getContentPane().add(bar,BorderLayout.NORTH);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String [] args){
new TestAE();
}
}
interface Action{
void execute();
}
class b1 implements Action{
public void execute(){
JOptionPane.showMessageDialog(null, "單擊了 b1");
}
}
class b2 implements Action{
public void execute(){
JOptionPane.showMessageDialog(null, "單擊了 b2");
}
}
class b3 implements Action{
public void execute(){
JOptionPane.showMessageDialog(null, "單擊了 b3");
}
}
上述實例備有較為詳實的正文,應當不難懂得。願望本文所述實例對年夜家可以或許有所贊助。