Moves an image of Duke around within a layered pane in response to mouse motion events. : 层面板 « Swing « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » Swing » 层面板 
14. 52. 4. Moves an image of Duke around within a layered pane in response to mouse motion events.
Moves an image of Duke around within a layered pane in response to mouse motion events.
/*
 *
 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class LayeredPaneDemo extends JPanel implements ActionListener, MouseMotionListener {
  private String[] layerStrings = "Yellow (0)""Magenta (1)""Cyan (2)""Red (3)""Green (4)" };

  private Color[] layerColors = Color.yellow, Color.magenta, Color.cyan, Color.red, Color.green };

  private JLayeredPane layeredPane;

  private JLabel dukeLabel;

  private JCheckBox onTop;

  private JComboBox layerList;

  // Action commands
  private static String ON_TOP_COMMAND = "ontop";

  private static String LAYER_COMMAND = "layer";

  // Adjustments to put Duke's toe at the cursor's tip.
  private static final int XFUDGE = 40;

  private static final int YFUDGE = 57;

  public LayeredPaneDemo() {
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

    // Create and load the duke icon.
    final ImageIcon icon = new ImageIcon("yourFile.gif");

    // Create and set up the layered pane.
    layeredPane = new JLayeredPane();
    layeredPane.setPreferredSize(new Dimension(300310));
    layeredPane.setBorder(BorderFactory.createTitledBorder("Move the Mouse to Move Duke"));
    layeredPane.addMouseMotionListener(this);

    // This is the origin of the first label added.
    Point origin = new Point(1020);

    // This is the offset for computing the origin for the next label.
    int offset = 35;

    // Add several overlapping, colored labels to the layered pane
    // using absolute positioning/sizing.
    for (int i = 0; i < layerStrings.length; i++) {
      JLabel label = createColoredLabel(layerStrings[i], layerColors[i], origin);
      layeredPane.add(label, new Integer(i));
      origin.x += offset;
      origin.y += offset;
    }

    // Create and add the Duke label to the layered pane.
    dukeLabel = new JLabel(icon);
    if (icon != null) {
      dukeLabel.setBounds(15225, icon.getIconWidth(), icon.getIconHeight());
    else {
      System.err.println("Duke icon not found; using black square instead.");
      dukeLabel.setBounds(152253030);
      dukeLabel.setOpaque(true);
      dukeLabel.setBackground(Color.BLACK);
    }
    layeredPane.add(dukeLabel, new Integer(2)0);

    // Add control pane and layered pane to this JPanel.
    add(Box.createRigidArea(new Dimension(010)));
    add(createControlPanel());
    add(Box.createRigidArea(new Dimension(010)));
    add(layeredPane);
  }

  private JLabel createColoredLabel(String text, Color color, Point origin) {
    JLabel label = new JLabel(text);
    label.setVerticalAlignment(JLabel.TOP);
    label.setHorizontalAlignment(JLabel.CENTER);
    label.setOpaque(true);
    label.setBackground(color);
    label.setForeground(Color.black);
    label.setBorder(BorderFactory.createLineBorder(Color.black));
    label.setBounds(origin.x, origin.y, 140140);
    return label;
  }

  // Create the control pane for the top of the frame.
  private JPanel createControlPanel() {
    onTop = new JCheckBox("Top Position in Layer");
    onTop.setSelected(true);
    onTop.setActionCommand(ON_TOP_COMMAND);
    onTop.addActionListener(this);

    layerList = new JComboBox(layerStrings);
    layerList.setSelectedIndex(2)// cyan layer
    layerList.setActionCommand(LAYER_COMMAND);
    layerList.addActionListener(this);

    JPanel controls = new JPanel();
    controls.add(layerList);
    controls.add(onTop);
    controls.setBorder(BorderFactory.createTitledBorder("Choose Duke's Layer and Position"));
    return controls;
  }

  // Make Duke follow the cursor.
  public void mouseMoved(MouseEvent e) {
    dukeLabel.setLocation(e.getX() - XFUDGE, e.getY() - YFUDGE);
  }

  public void mouseDragged(MouseEvent e) {
  // do nothing

  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();

    if (ON_TOP_COMMAND.equals(cmd)) {
      if (onTop.isSelected())
        layeredPane.moveToFront(dukeLabel);
      else
        layeredPane.moveToBack(dukeLabel);

    else if (LAYER_COMMAND.equals(cmd)) {
      int position = onTop.isSelected() 1;
      layeredPane.setLayer(dukeLabel, layerList.getSelectedIndex(), position);
    }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("LayeredPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    JComponent newContentPane = new LayeredPaneDemo();
    newContentPane.setOpaque(true)// content panes must be opaque
    frame.setContentPane(newContentPane);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
  }
}
14. 52. 层面板
14. 52. 1. JLayeredPane层常数
14. 52. 2. 在不同层次执行按钮
14. 52. 3. The JLayeredPane serves as the main component container of a JRootPane.
14. 52. 4. Moves an image of Duke around within a layered pane in response to mouse motion events.Moves an image of Duke around within a layered pane in response to mouse motion events.
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.