Java JFrame架构中RootPane--GlassPane的应用
来源:互联网 发布:湖南软件技术学院 编辑:程序博客网 时间:2024/05/21 06:53
Java JFrame中从里到外由frame-layeredPane-contentPane-menuBar(optional)-glassPane构成。layered pane用处很大就不必说了。现在是其中glass pane的应用。
package com.han;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.Point;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.MouseEvent;import javax.swing.AbstractButton;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.SwingUtilities;import javax.swing.event.MouseInputListener;/** * It contains a check box that lets you set whether the glass pane is "visible" * — whether it can get events and paint itself onscreen. When the glass pane is * visible, it blocks all input events from reaching the components in the * content pane. It also paints a red dot in the place where it last detected a * mouse-pressed event. * * @author HAN * */public class GlassPane_1 {/** * Create and show GUI. For thread safety, this method should be invoked * from the event-dispatching thread. */private static void createAndShowGUI() {// Create and set up the window.JFrame frame = new JFrame("GlassPane Demo 1");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Start creating and adding components.JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");changeButton.setSelected(false);// Set up the content pane, where the "main" GUI lives.Container contentPane = frame.getContentPane();contentPane.setLayout(new FlowLayout());contentPane.add(changeButton);contentPane.add(new JButton("Button 1"));contentPane.add(new JButton("Button 2"));// Set up the menu bar, which appears above the content pane.JMenuBar menuBar = new JMenuBar();JMenu menu = new JMenu("Menu");menu.add(new JMenuItem("Do nothing"));menuBar.add(menu);frame.setJMenuBar(menuBar);// Set up the glass pane, which appears both above the menu bar and the// content pane, and is an item listener on the changeButton.MyGlassPane myGlassPane = new MyGlassPane(contentPane, changeButton);changeButton.addItemListener(myGlassPane);frame.setGlassPane(myGlassPane);// Show the window.frame.pack();frame.setVisible(true);}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub// Schedule a job for the event-dispatching thread:// creating and showing its application's GUI.SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubcreateAndShowGUI();}});}}/** * We have to provide our own glass pane so that it can paint itself. * * @author HAN * */@SuppressWarnings("serial")class MyGlassPane extends JComponent implements ItemListener {private Point point;// React to changeButton click.@Overridepublic void itemStateChanged(ItemEvent e) {// TODO Auto-generated method stubsetVisible(e.getStateChange() == ItemEvent.SELECTED);}@Overrideprotected void paintComponent(Graphics g) {if (point != null) {g.setColor(Color.RED);g.fillOval(point.x - 10, point.y - 10, 20, 20);}}public void setPoint(Point point) {this.point = point;}MyGlassPane(Container contentPane, AbstractButton changeButton) {// Should forward the input events to check box.CBListener listener = new CBListener(contentPane, changeButton, this);addMouseListener(listener);addMouseMotionListener(listener);}}/** * Listen for all events that our check box is likely to be interested in. * Redispatch them to the check box. * * @author HAN * */class CBListener implements MouseInputListener {private Container contentPane;private Component changeButton;private MyGlassPane glassPane;CBListener(Container contentPane, Component changeButton,MyGlassPane glassPane) {this.contentPane = contentPane;this.changeButton = changeButton;this.glassPane = glassPane;}@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubredispatchMouseEvent(e, false);}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stubredispatchMouseEvent(e, true);}@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stubredispatchMouseEvent(e, false);}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stubredispatchMouseEvent(e, false);}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stubredispatchMouseEvent(e, false);}@Overridepublic void mouseDragged(MouseEvent e) {// TODO Auto-generated method stubredispatchMouseEvent(e, false);}@Overridepublic void mouseMoved(MouseEvent e) {// TODO Auto-generated method stubredispatchMouseEvent(e, false);}// A basic implementation of redispatching events.private void redispatchMouseEvent(MouseEvent e, Boolean repaint) {Point glassPanePoint = e.getPoint();Point contentPanePoint = SwingUtilities.convertPoint(glassPane,glassPanePoint, contentPane);Component component = SwingUtilities.getDeepestComponentAt(contentPane,contentPanePoint.x, contentPanePoint.y);if (component != null && component.equals(changeButton)) {Point componentPoint = SwingUtilities.convertPoint(glassPane,glassPanePoint, component);changeButton.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), componentPoint.x,componentPoint.y, e.getClickCount(), e.isPopupTrigger()));} else {if (repaint) {glassPane.setPoint(glassPanePoint);glassPane.repaint();}}}}
It contains a check box that lets you set whether the glass pane is "visible" — whether it can get events and paint itself onscreen. When the glass pane is visible, it blocks all input events from reaching the components in the content pane. It also paints a red dot in the place where it last detected a mouse-pressed event.
- Java JFrame架构中RootPane--GlassPane的应用
- 关于研究Java中GlassPane的心得
- java pane 之GlassPane
- AsWing中JFrame的简单应用
- java---jframe带操控界面的应用
- java中JFrame类的简介
- Java中JFrame.getContentPane().add(pane)与JFrame.add(pane)的区别
- java中JFrame中函数removeAll的用法
- 【jframe】Java架构师之路
- java swing中jpanel跟jframe的区别
- java中JFrame和Frame的窗体关闭区别
- Java中Swing编程JFrame窗口的使用
- JFrame中setDefaultCloseOperation的使用方法
- Java JFrame常用的布局
- Java Jframe库的 EventQueue
- JFrame类setContentPane()和getContentPane()的应用
- JFrame类setContentPane()和getContentPane()的应用
- 用JFrame代替Frame的应用
- multimap的查找
- SQL时间段在另外一个时间段内是否存在
- 视频教程/软件892
- 小失落
- 成都旅游必去点531
- Java JFrame架构中RootPane--GlassPane的应用
- 今天就要远离一个在了三年半年的地方
- 深入理解JVM内幕:从基本结构到Java 7新特性
- Android 4.0 事件输入(Event Input)系统
- 网页swf素材
- Android系统--事件读取及分发
- 贪吃蛇---毁墙模式详解
- 网页下雨JS特效
- 使用VS2005/8/10开发——Sample