⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guiutils.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:GUIUtils.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfdraw.gui;


import java.awt.Component;
import java.awt.Frame;

import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;

/**
 *  GUI Const class is a utilities library for gui.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
public class GUIUtils{

	//Recursivly get a comp's parent Frame.
	// @param comp A specified comp used to find its parent Frame.
	// @return The Frame
	static public Frame getFrame(Component comp){
		Component c	=getParent(comp,".Frame");
		if (c!=null)
			return (Frame)c;
		else
			return null;
	}

	//Recursivly get a comp's parent JFrame.
	// @param comp A specified comp used to find its parent JFrame.
	// @return The JFrame
	static public JFrame getJFrame(Component comp){
		Component c	=getParent(comp,".JFrame");
		if (c!=null)
			return (JFrame)c;
		else
			return null;
	}


	//Recursivly get a comp's parent with specified parent component type
	// @param comp A specified comp used to find its parent
	// @param compType A string comp type, it's actually the comp's class name.
	// @return The parent comp
	static public Component getParent(Component comp, String compType ){
		Component  resultingComponent = null;
		while (comp != null && resultingComponent == null)
        	{
        		String className	=comp.getClass().getName();
          		if (className.endsWith(compType))
                        	resultingComponent = comp;
        		else  comp = comp.getParent();
        	}

		return resultingComponent;
	}


    /**
     *  Set a button's background
     *
     *  For windows UI, we can easily change the background color of a JButton,
     *  But we can do nothing while chaning the background color of a JButton under MAC UIs,
     *  So we try to make a ImageIcon here to instead of directly changing the background.
     *
     *  @param button A button to be changed color
     *  @param color  New color
     */
    public static void setButtonBackground(JButton button, Color color){
                if (button==null || color==null)
                	return;

		//If you do not use MAC, you can simply use one line code below to change button's background
		//button.setBackground(color);                	
                	
		int size	=20;
		        
        	// Create a buffered image to fill with the new color
        	BufferedImage bufferedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    
        	// Create a graphics contents on the buffered image
        	Graphics2D g2 = bufferedImage.createGraphics();
        	
        	//fill with new color
        	g2.setColor(color);
        	g2.fillRect(0, 0, size, size);
   
        	// Graphics context no longer needed so dispose it
        	g2.dispose();

		// generate a new image icon        	
        	ImageIcon icon	=new ImageIcon(bufferedImage);
        	
        	//replace the button image to a new image.
        	button.setIcon(icon);
    }
    
   
   //a collection count
   private static int m_collCount	=0;
   
   /**Clear garbage memory, call gc method*/
   public static void garbageCollection(){ 
   	m_collCount++;
   	if (m_collCount>10){
   		m_collCount=0;
		//System.out.println(Runtime.getRuntime().freeMemory());
		System.gc();
		//System.out.println(Runtime.getRuntime().freeMemory());
	}
   }
    	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -