7 8 9 图形程序设计 事件处理 swing用户界面组件

7 图形程序设计

7.1 Swing概述抽象窗口工具箱,AWT,“一次编写,随处运行”,“一次编写,随处调试”。

Swing,丰富便捷的用户界面元素集合;底层平台依赖少;不同平台的一致感。

Metal, Nimbus, Java观感。

一种新技术,JavaFX。

7.2 创建框架

JFrame。

事件分派线程。

EventQueue.invokeLater(new Runnable() {
 public void run() {
 statement; }
});

关闭框架时的响应动作。

7.3 框架定位

setLocation
setBounds
setTitle
setResizable
setIconImage

7.4 在组建中显示信息

7.5 处理2d图形

7.6 使用颜色

7.7 文本使用特殊字体

7.8 显示图像

8 事件处理

Java AWT事件模型的工作机制。

8.1 事件处理基础

Java设计环境折衷了vb和原始c的事件处理方式。事件委托模型。

事件源,事件对象,事件监听器。

Java 中,所有事件对象都派生于java.util.EventObject类。

不同的事件源可以产生不同类别的事件。

AWT事件处理概要

  • 监听器对象是一个实现了特定监听器接口的类的实例。
  • 事件源是一个能够注册监听器对象并发送事件对象的对象。
  • 当事件发生时,事件源把事件对象传递给所有注册的监听器。
  • 监听器对象利用事件对象中的信息决定如何对事件作出响应。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonFrame extends JFrame {
    private JPanel buttonPanel;
    private static final int D_W = 300;
    private static final int D_H = 200;

    public ButtonFrame() {
        setSize(D_W, D_H);

        JButton yellowButton = new JButton("Yellow");
        JButton blueButton = new JButton("Blue");
        JButton redButton = new JButton("Red");

        buttonPanel = new JPanel();

        buttonPanel.add(yellowButton);
        buttonPanel.add(blueButton);
        buttonPanel.add(redButton);

        add(buttonPanel);

        ColorAction yellowAction = new ColorAction(Color.YELLOW);
        ColorAction blueAction = new ColorAction(Color.BLUE);
        ColorAction redAction = new ColorAction(Color.RED);

        yellowButton.addActionListener(yellowAction);
        blueButton.addActionListener(blueAction);
        redButton.addActionListener(redAction);
    }

    private class ColorAction implements ActionListener {
        private Color backgroundColor;

        public ColorAction(Color color) {
            backgroundColor = color;
        }

        public void actionPerformed(ActionEvent event) {
            buttonPanel.setBackground(backgroundColor);
        }
    }
}
View Code

8.1.2 建议使用内部类

8.1.3 创建一个包含方法调用的监听器

8.1.4 改变观感,实例

8.1.5 适配器类

每个含有多个方法的AWT监听器接口都配有一个适配器类,这个类实现了接口中所有的方法,但是每个方法都没有做任何事情。通过继承适配器类,就可以调用多个方法中的任何一个或多个。

8.2 动作

action接口,封装命令,并连接到多个事件源。

一个是封装了下列内容的对象:

  • 命令的说明
  • 执行命令所需要的参数

8.3 鼠标事件

如果用户使用鼠标画图,就需要捕获鼠标移动点击和拖动事件。

8.4 AWT事件继承层次

AWT将事件分为低级事件和语义事件。

常见语义事件类:

  • ActionEvent
  • AdjustEvent
  • ItemEvent

常见低级事件类

  • KeyEvent
  • MouseEvent
  • FocusEvent
  • WindowEvent

不同的接口监听这些事件

包含多个方法的监听器接口,都配有一个适配器类。

9 Swing用户界面组件

9.1 Swing和MVC设计模式

9.2 布局管理概述

9.2.1 边框布局

9.2.2 网格布局

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A panel with calculator buttons and a result display.
 */
public class CalculatorPanel extends JPanel
{
   private JButton display;
   private JPanel panel;
   private double result;
   private String lastCommand;
   private boolean start;

   public CalculatorPanel()
   {
      setLayout(new BorderLayout());

      result = 0;
      lastCommand = "=";
      start = true;

      // add the display

      display = new JButton("0");
      display.setEnabled(false);
      add(display, BorderLayout.NORTH);

      ActionListener insert = new InsertAction();
      ActionListener command = new CommandAction();

      // add the buttons in a 4 x 4 grid

      panel = new JPanel();
      panel.setLayout(new GridLayout(4, 4));

      addButton("7", insert);
      addButton("8", insert);
      addButton("9", insert);
      addButton("/", command);

      addButton("4", insert);
      addButton("5", insert);
      addButton("6", insert);
      addButton("*", command);

      addButton("1", insert);
      addButton("2", insert);
      addButton("3", insert);
      addButton("-", command);

      addButton("0", insert);
      addButton(".", insert);
      addButton("=", command);
      addButton("+", command);

      add(panel, BorderLayout.CENTER);
   }

   /**
    * Adds a button to the center panel.
    * @param label the button label
    * @param listener the button listener
    */
   private void addButton(String label, ActionListener listener)
   {
      JButton button = new JButton(label);
      button.addActionListener(listener);
      panel.add(button);
   }

   /**
    * This action inserts the button action string to the end of the display text.
    */
   private class InsertAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         String input = event.getActionCommand();
         if (start)
         {
            display.setText("");
            start = false;
         }
         display.setText(display.getText() + input);
      }
   }

   /**
    * This action executes the command that the button action string denotes.
    */
   private class CommandAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         String command = event.getActionCommand();

         if (start)
         {
            if (command.equals("-"))
            {
               display.setText(command);
               start = false;
            }
            else lastCommand = command;
         }
         else
         {
            calculate(Double.parseDouble(display.getText()));
            lastCommand = command;
            start = true;
         }
      }
   }

   /**
    * Carries out the pending calculation.
    * @param x the value to be accumulated with the prior result.
    */
   public void calculate(double x)
   {
      if (lastCommand.equals("+")) result += x;
      else if (lastCommand.equals("-")) result -= x;
      else if (lastCommand.equals("*")) result *= x;
      else if (lastCommand.equals("/")) result /= x;
      else if (lastCommand.equals("=")) result = x;
      display.setText("" + result);
   }
}
View Code

9.3 文本输入

9.3.1 文本域

9.3.2 标签和标签组件

9.3.3 密码域

9.3.4 文本区

9.4 选择组件

9.5 菜单

9.6 复杂的布局管理

9.7 对话框

猜你喜欢

转载自www.cnblogs.com/ayor/p/12825293.html