java 添加方法_Java添加事件的四种方式

1 /**

2 * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器3 *4 *@authorcodebrother5 */

6 class EventListener1 extends JFrame implementsActionListener {7 privateJButton btBlue, btDialog;8

9 publicEventListener1() {10 setTitle("Java GUI 事件监听处理");11 setBounds(100, 100, 500, 350);12 setLayout(newFlowLayout());13 btBlue = new JButton("蓝色");14 btDialog = new JButton("弹窗");15

16 //将按钮添加事件监听器

17 btBlue.addActionListener(this);18 btDialog.addActionListener(this);19

20 add(btBlue);21 add(btDialog);22

23 setVisible(true);24 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);25 }26 //***************************事件处理***************************

27 @Override28 public voidactionPerformed(ActionEvent e) {29 if (e.getSource() ==btBlue) {30 Container c =getContentPane();31 c.setBackground(Color.BLUE);32 }33 else if (e.getSource() ==btDialog) {34 JDialog dialog = newJDialog();35 dialog.setBounds(300, 200, 400, 300);36 dialog.setVisible(true);37 }38 }39

40 }41

42 /**

43 * Java事件监听处理——内部类处理44 *45 *@authorcodebrother46 */

47

48 class EventListener3 extendsJFrame {49 privateJButton btBlue, btDialog;50

51 //构造方法

52 publicEventListener3() {53 setTitle("Java GUI 事件监听处理");54 setBounds(100, 100, 500, 350);55 setLayout(newFlowLayout());56 btBlue = new JButton("蓝色");57 btDialog = new JButton("弹窗");58 //添加事件监听器对象(面向对象思想)

59 btBlue.addActionListener(newColorEventListener());60 btDialog.addActionListener(newDialogEventListener());61

62 add(btBlue);63 add(btDialog);64 setVisible(true);65 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);66 }67 //内部类ColorEventListener,实现ActionListener接口

68 class ColorEventListener implementsActionListener {69 @Override70 public voidactionPerformed(ActionEvent e) {71 Container c =getContentPane();72 c.setBackground(Color.BLUE);73 }74 }75 //内部类DialogEventListener,实现ActionListener接口

76 class DialogEventListener implementsActionListener {77 @Override78 public voidactionPerformed(ActionEvent e) {79 JDialog dialog = newJDialog();80 dialog.setBounds(300, 200, 400, 300);81 dialog.setVisible(true);82 }83 }84

85 }86

87

88

89 /**

90 * Java事件监听处理——匿名内部类处理91 *92 *@authorcodebrother93 */

94 class EventListener2 extendsJFrame {95 privateJButton btBlue, btDialog;96

97 publicEventListener2() {98 setTitle("Java GUI 事件监听处理");99 setBounds(100, 100, 500, 350);100 setLayout(newFlowLayout());101

102 btBlue = new JButton("蓝色");103 btDialog = new JButton("弹窗");104

105 //添加事件监听器(此处即为匿名类)

106 btBlue.addActionListener(newActionListener() {107 //事件处理

108 @Override109 public voidactionPerformed(ActionEvent e) {110 Container c =getContentPane();111 c.setBackground(Color.BLUE);112 }113 });114

115 //并添加事件监听器

116 btDialog.addActionListener(newActionListener() {117 @Override118 public voidactionPerformed(ActionEvent e) {119 JDialog dialog = newJDialog();120 dialog.setBounds(300, 200, 400, 300);121 dialog.setVisible(true);122 }123 });124

125 add(btBlue);126 add(btDialog);127 setVisible(true);128 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);129 }130

131 }132

133 /**

134 * Java事件监听处理——外部类处理135 *136 *@authorcodebrother137 */

138 class EventListener4 extendsJFrame {139 privateJButton btBlue, btDialog;140

141 publicEventListener4() {142 setTitle("Java GUI 事件监听处理");143 setBounds(100, 100, 500, 350);144 setLayout(newFlowLayout());145 btBlue = new JButton("蓝色");146 btDialog = new JButton("弹窗");147 //将按钮添加事件监听器

148 btBlue.addActionListener(new ColorEventListener(this));149 btDialog.addActionListener(newDialogEventListener());150

151 add(btBlue);152 add(btDialog);153 setVisible(true);154 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);155 }156

157 }158 //外部类ColorEventListener,实现ActionListener接口

159 class ColorEventListener implementsActionListener {160 privateEventListener4 el;161 ColorEventListener(EventListener4 el) {162 this.el =el;163 }164 @Override165 public voidactionPerformed(ActionEvent e) {166 Container c =el.getContentPane();167 c.setBackground(Color.BLUE);168 }169 }170 //外部类DialogEventListener,实现ActionListener接口

171 class DialogEventListener implementsActionListener {172 @Override173 public voidactionPerformed(ActionEvent e) {174 JDialog dialog = newJDialog();175 dialog.setBounds(300, 200, 400, 300);176 dialog.setVisible(true);177 }178 }179

180 public classActionListenerTest181 {182 public static voidmain(String args[])183 {184 newEventListener2();185 }186 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值