java 事件监听 - 控件
2016-10-16 03:08
260 查看
java 事件监听
//事件监听 //事件监听,写了一个小案例,点击按钮改变面板的颜色。 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Index extends JFrame implements ActionListener{ //设置面板 Wdmb wdmb = new Wdmb(); //设置按钮 JButton anniu1 = new JButton("黄色"); JButton anniu2 = new JButton("红色"); public static void main(String[] args) throws Exception{ //实例化当前类 Index index = new Index(); } //自动执行 public Index(){ //设置面板的背景色 wdmb.setBackground(Color.yellow); //添加到界面 this.add(anniu1,BorderLayout.NORTH); this.add(anniu2,BorderLayout.SOUTH); this.add(wdmb); //添加监听事件 anniu1.addActionListener(this); anniu1.setActionCommand("1"); //区别按钮识别码 anniu2.addActionListener(this); anniu2.setActionCommand("2"); //区别按钮识别码 this.setSize(500,500); this.setLocation(300,200); this.setTitle("绘图"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } //ActionListener 的抽象方法 //ActionEvent e 是固定的 public void actionPerformed(ActionEvent e){ //e.getActionCommand().equals("1") 判断“区别按钮识别码”是否相等 if(e.getActionCommand().equals("1")){ System.out.println("黄色按钮按下了"); wdmb.setBackground(Color.yellow); } if(e.getActionCommand().equals("2")){ System.out.println("红色按钮按下了"); wdmb.setBackground(Color.red); } } } //面板方法 class Wdmb extends JPanel{ //方法覆盖 //JPanel自带方法,下面格式是固定的 //paint 会在三种情况下自动被调用 //1、启动程序 2、窗口大小发生变化时 3、运行repaint函数时 public void paint(Graphics g){ //覆盖父类的方法 super.paint(g); } }
相关文章推荐
- java中给控件填加事件监听
- JAVA进阶8.4——事件处理(1)事件监听与控件事件
- 非专业码农 JAVA学习笔记 用户图形界面设计与实现-所有控件的监听事件
- Java图形界面事件监听处理之四种方法
- Atitit。监听键盘上下左右方向键事件java js jquery c#.net
- 【Java.NIO】SelectionKey,***监听的事件***
- JAVA事件处理,和事件监听机制
- java设计模式--事件监听器模式和观察者模式
- Android各种控件的事件监听
- android应用开发实践一:Activity与控件布局,事件监听之学习实践
- 监听获取程序退出事件(Linux、Windows、Java、C++)
- 手指对控件的按下,放了,背景颜色改变的监听事件
- Java键盘事件监听
- 简单的java图片绘制和鼠标事件监听
- java鼠标事件监听
- java事件监听机制中的事件、监听器接口、处理方法、事件源
- 关于flex中RichTextEditor控件,监听Ctrl+s事件和去除Ctrl+s按下时在RichTextEditor生成的不可知字符的问题
- 初涉java(事件处理与事件监听)
- Java监听键盘鼠标全局事件
- java(6)--事件监听机制