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