11.1 事件处理基础
来源:互联网 发布:15年网络流行关键词 编辑:程序博客网 时间:2024/06/06 01:46
1 按钮例子
import java.awt.*;import javax.swing.*;/** * @version 1.34 2015-06-12 * @author Cay Horstmann */public class ButtonTest{ public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new ButtonFrame(); frame.setTitle("ButtonTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); }}import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * A frame with a button panel */public class ButtonFrame extends JFrame{ private JPanel buttonPanel; private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public ButtonFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // create buttons JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); buttonPanel = new JPanel(); // add buttons to panel buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton); // add panel to frame add(buttonPanel); // create button actions ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); // associate actions with buttons yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } /** * An action listener that sets the panel's background color. */ private class ColorAction implements ActionListener { private Color backgroundColor; public ColorAction(Color c) { backgroundColor = c; } public void actionPerformed(ActionEvent event) { buttonPanel.setBackground(backgroundColor); } }}
2 可以用Lambda表达式
3 例子改变外观
import java.awt.*;import javax.swing.*;/** * @version 1.32 2015-06-12 * @author Cay Horstmann */public class PlafTest{ public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new PlafFrame(); frame.setTitle("PlafTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); }}import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.UIManager;/** * A frame with a button panel for changing look-and-feel */public class PlafFrame extends JFrame{ private JPanel buttonPanel; public PlafFrame() { buttonPanel = new JPanel(); UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels(); for (UIManager.LookAndFeelInfo info : infos) makeButton(info.getName(), info.getClassName()); add(buttonPanel); pack(); } /** * Makes a button to change the pluggable look-and-feel. * @param name the button name * @param className the name of the look-and-feel class */ private void makeButton(String name, String className) { // add button to panel JButton button = new JButton(name); buttonPanel.add(button); // set button action button.addActionListener(event -> { // button action: switch to the new look-and-feel try { UIManager.setLookAndFeel(className); SwingUtilities.updateComponentTreeUI(this); pack(); } catch (Exception e) { e.printStackTrace(); } }); }}
4 适配器类
就是夹杂在监听器接口和事件之间的类。
0 0
- 11.1 事件处理基础
- UI基础-事件处理
- AWT事件处理基础
- Java基础--事件处理机制
- ActionListener接口--事件处理基础
- jQuery基础三事件处理
- Vue基础(事件处理)
- javascript基础:事件2事件处理函数
- 事件处理基础 处理按钮点击事件实例 Java核心技术
- java swing基础与事件处理
- 安卓基础学习_Android事件处理
- 03js基础 js事件处理
- java swing基础和事件处理
- 【基础】Cocos2d-x 浅谈事件处理机制
- java的事件处理机制基础
- javascript基础-常见控件事件处理
- 【js基础】JS事件处理程序
- JointJS零基础入门02-事件处理
- mysql 5.7.16解压版安装过程
- nodejs 表格分页工具类
- 彻底了解指针数组,数组指针,以及函数指针,以及堆中的分配规则
- 11 事件处理
- 浅谈泛型
- 11.1 事件处理基础
- 《Python核心编程》第13章 习题
- MapReduce--Shuffle过程
- 11.2 动作
- Beego初探
- 第14周实践 成绩处理函数版
- 深度优先,广度优先的学习(待续)
- 11.3 鼠标事件
- [ JoyfulPuTTY ] PuTTY开源代码分析.1 --- 全局配置