以前写的,方便swing使用的子程序集,顺带散分

dyhml dyhml 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
/*
* Created on 2005-2-19
*/
package net.csdn.dyhml.util;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.Enumeration;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
/**
* 提供对AWT,Swing有用的静态方法
*
* @author <a href="http:\\blog.csdn.net\dyhml">VirusCamp</a>
*/
public class SwingUtil {
/**
* 由此类中方法使用的默认 java.awt.Toolkit
*/
public static final Toolkit toolkit = Toolkit.getDefaultToolkit();
/**
* 将window全屏化,对Window,Frame,Dialog,JWindow,JFrame,JDialog有效
* @param window 全屏化的window
* @see Window,Frame,Dialog,JWindow,JFrame,JDialog
*/
public static void setSizeFullOfScreen(Window window) {
Dimension screenSize = toolkit.getScreenSize();
window.setBounds(0, 0, screenSize.width, screenSize.height);
}
/**
* 将window设置在屏幕中间,对Window,Frame,Dialog,JWindow,JFrame,JDialog有效
* @param window 设置在屏幕中间的window
* @see Window,Frame,Dialog,JWindow,JFrame,JDialog
*/
public static void setToScreenCenter(Window window) {
Dimension screenSize = toolkit.getScreenSize();
Dimension windowSize = window.getSize();
if (windowSize.height > screenSize.height)
windowSize.height = screenSize.height;
if (windowSize.width > screenSize.width)
windowSize.width = screenSize.width;
window.setLocation((screenSize.width - windowSize.width) / 2,
(screenSize.height - windowSize.height) / 2);
}
/**
* 设置全局字体
* @param font 全局字体
*/
public static void initGlobalFontSetting(Font font) {
FontUIResource fontRes = new FontUIResource(font);
for (Enumeration keys = UIManager.getDefaults().keys(); keys
.hasMoreElements();) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource)
UIManager.put(key, fontRes);
}
}
/**
* 在Component上设置光标(Cursor)
* @param c 在此Component上设置光标(Cursor)
* @param image 设置的光标(Cursor)图案
*/
public static void setCursor(Component c, Image image) {
Cursor cursor = toolkit
.createCustomCursor(image, new Point(0, 0), null);
c.setCursor(cursor);
}
/**
* 设置全局LookAndFeel,此后建立的Swing部件使用此LookAndFeel
* @param lafstr 设置的LookAndFeel
* @return 0:设置成功,1:设置失败
*/
public static int setGlobalLookAndFeel(String lafstr) {
try {
UIManager.setLookAndFeel(lafstr);
return 0;
} catch (Exception e) {
return 1;
}
}

/**
* 此后建立的JFrame,JDialog是否使用LookAndFeel的窗口装饰(标题栏,边框等)
*/
private static boolean isUndecorated=false;

/**
* 返回此后建立的JFrame,JDialog是否使用LookAndFeel的窗口装饰(标题栏,边框等)
* @return 此后建立的JFrame,JDialog是否使用LookAndFeel的窗口装饰(标题栏,边框等)
*/
public static boolean isUndecorated() {
return isUndecorated;
}

/**
* 设置此后建立的JFrame,JDialog是否使用LookAndFeel的窗口装饰(标题栏,边框等)
* @param 是否此后建立的JFrame,JDialog使用LookAndFeel的窗口装饰(标题栏,边框等)
*/
public static void setUndecorated(boolean undecorated) {
LookAndFeel laf = UIManager.getLookAndFeel();
SwingUtil.isUndecorated = undecorated;
JFrame.setDefaultLookAndFeelDecorated(undecorated
&& laf.getSupportsWindowDecorations());
JDialog.setDefaultLookAndFeelDecorated(undecorated
&& laf.getSupportsWindowDecorations());
}

/**
* 设置JFrame是否使用LookAndFeel的窗口装饰(标题栏,边框等)
* @param 是否使用LookAndFeel的窗口装饰(标题栏,边框等)
*/
public static void setUndecorated(JFrame frame,boolean undecorated){
if(frame.isDisplayable()) frame.dispose();
frame.setUndecorated(undecorated&&UIManager.getLookAndFeel().getSupportsWindowDecorations());
JRootPane jRootPane=frame.getRootPane();
jRootPane.setWindowDecorationStyle(jRootPane.getWindowDecorationStyle());
if(frame.isDisplayable()) frame.show();
}

/**
* 设置JDialog是否使用LookAndFeel的窗口装饰(标题栏,边框等)
* @param 是否使用LookAndFeel的窗口装饰(标题栏,边框等)
*/
public static void setUndecorated(JDialog dialog,boolean undecorated){
if(dialog.isDisplayable()) dialog.dispose();
dialog.setUndecorated(undecorated&&UIManager.getLookAndFeel().getSupportsWindowDecorations());
JRootPane jRootPane=dialog.getRootPane();
jRootPane.setWindowDecorationStyle(jRootPane.getWindowDecorationStyle());
if(dialog.isDisplayable()) dialog.show();
}
/**
* 更新本程序建立的所有JFrame的LookAndFeel
*/
public static void updateUI(){
Frame[] frames=Frame.getFrames();
for(int i=0;i<frames.length;i++)
updateUI(frames[i]);
}
/**
* 更新Component的LookAndFeel
* @param target 操作对象
*/
public static void updateUI(Component target){
if(target instanceof JFrame)
updateUI((JFrame)target,isUndecorated);
if(target instanceof JDialog)
updateUI((JDialog)target,isUndecorated);
SwingUtilities.updateComponentTreeUI(target);
}
/**
* 更新JFrame的LookAndFeel,改变JFrame的边框,标题
* @param frame 操作对象
* @param undecorated 是否使用LookAndFeel的窗口装饰(标题栏,边框等)
*/
public static void updateUI(JFrame frame,boolean undecorated){
setUndecorated(frame,undecorated);
SwingUtilities.updateComponentTreeUI(frame);
}

/**
* 更新JDialog的LookAndFeel,改变JDialog的边框,标题
* @param dialog 操作对象
* @param undecorated 是否使用LookAndFeel的窗口装饰(标题栏,边框等)
*/
public static void updateUI(JDialog dialog,boolean undecorated){
setUndecorated(dialog,undecorated);
SwingUtilities.updateComponentTreeUI(dialog);
}
}
...全文

相关推荐

使用java的swing写的抽奖程序

这个程序是我学习java swing时写的,可以直接运行在windows和linux中,Award.jar文件中有源代码。在国家要求全国都要使用正版软件的号召下,我决定以后写的软件可以直接在各种操作系统平台上运行,经过长期研究各种程序设计语言,感觉java是首选,最主要的原因是java的一次编绎,到处运行的特性。而且通过JDBC访问各种数据库,在各种平台上都使用相同的一个数据库驱动jar文件,连数据库的客户端都不需要安装。

song_dong_sheng

Swing写的上传文件

Swing写的一个上传小程序

小莲子09

Eclipse下写Swing的一个例子

外网下的,还是可以的。对于初学者有用。其实,swing的GUI方式和C#感觉差不太多。英文的,不过很容易理解

gaohaidong20050016

使用java编写swing配合BeautyEye写的例子源码(官方JAR和截图)

使用java编写swing配合BeautyEye写的例子源码(官方JAR和截图) 前几年,因对Swing的外观定制有过几次深入实践,因而今年初有了利用NinePatch技术为Swing彻底改头换面一次的想法。   起先没想完全开发一套LNF,因为明知工作量很大(包括编码,更为困难的是美感与视觉效果的确定),原本只想在Windows LNF的基础上深入改进,以便站在前人的肩膀上快速收工。因熟门熟路,定制起来非常顺手,随着改造的深入,完全定制一套LNF的条件自然成形。   经众多基础ui素材的积累和借鉴,以及效果精细调整,各组件外观逐渐达到预想的效果,也就萌发了要将此项目分享于互联上,供需要的人使用。   过程中也难免存在些遗憾——有些更好的想法没有付诸实施,原因在于LNF代码的结构很多时候并不像Swing本身一样优秀(更多时候是LNF创造者们有所保留,因为LNF代码难保一劳永逸),且存在不少未公开API的调用,基于兼容性和未来维护难度的考虑,采取了中庸之道。   如有喜欢者将甚感欣慰,不喜者也权当技术交流,千万勿喷。你可将其无偿用于测试、学习、甚至商业用途。但禁止在未经授权的情况下用于商业用途等,还请尊重知识产权。

业余草

java Swing写的一个流星雨

java swing程序,大神勿喷。灵感来源于以前的一次流星雨。所以就自己写了一个。很粗糙。

skyline how far
评论 21
收藏 已收藏
分享
返回顶部
dyhml
Java SE 62,620 成员 307,251 内容