import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import com.incors.plaf.alloy.*;
/**
* Swing开发的帮助类,包括了一些实用方法
*/
public abstract class SwingUtil {
/**
* UI中使用的默认字体
*/
private static final javax.swing.plaf.FontUIResource font =
new javax.swing.plaf.FontUIResource("", Font.PLAIN, 12);
public static void setAlloyLookAndFeel() {
try {
com.incors.plaf.alloy.AlloyLookAndFeel.
setProperty("alloy.isLookAndFeelFrameDecoration", "true");
String sn = null;
sn = "v#ej_technologies#uwbjzx#e6pck8";
com.incors.plaf.alloy.AlloyLookAndFeel.setProperty(
"alloy.licenseCode", sn);
AlloyFontTheme fontTheme = new AlloyFontTheme() {
public javax.swing.plaf.FontUIResource getControlTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getMenuTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getSubTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getSystemTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getUserTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getWindowTitleFont() {
return font;
}
};
com.incors.plaf.alloy.AlloyTheme theme = null;
//theme = new com.incors.plaf.alloy.themes.bedouin.BedouinTheme();
theme = new com.incors.plaf.alloy.themes.glass.GlassTheme(fontTheme);
//theme = new com.incors.plaf.alloy.themes.acid.AcidTheme();
javax.swing.LookAndFeel alloyLnF = new com.incors.plaf.alloy.
AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置SwingUI,使用略微好看的中文字体
*/
public static void setTheme() {
try {
javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme
(new javax.swing.plaf.metal.DefaultMetalTheme() {
public javax.swing.plaf.FontUIResource getControlTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getMenuTextFont() {
return font;
}
});
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Exception e) {
}
}
/**
* 初始化一个窗口的宽度和高度,并使其居中显示
* @param window 窗口,可以为JWindow JFrame或是JDialog
* @param w 宽度
* @param h 高度
*/
public static void initSizeAndLocation(Window window, int w, int h) {
window.setSize(w, h);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation(size.width / 2 - w / 2, size.height / 2 - h / 2);
}
/**
* 设置一个窗口为最大显示
* @param window 窗口,可以为JWindow JFrame或是JDialog
* @deprecated setFullScreeen
*/
public static void setMaxSize(Window window) {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
window.setSize(size.width, size.height - 50);
window.setLocation(0, 0);
}
/**
* 等待一个窗口消失,阻塞当前线程
* @param w 窗口,可以为JWindow JFrame或是JDialog
*/
public static void waitForWindow(Window w) {
try {
while (true) {
if (w.isShowing() == false) {
return;
} else {
Thread.sleep(500L);
}
}
} catch (InterruptedException ex) {
}
}
/**
* 设置某个Frame不能关闭
* @param frame Frame
*/
public static void setFrameCannotClose(JFrame frame) {
//frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
}
/**
* 设置某个对话框不能关闭
* @param dialog Dialog
*/
public static void setDialogCannotClose(JDialog dialog) {
//dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.
INFORMATION_DIALOG);
}
/**
* 设置某个Frame始终在最前
* @param frame Frame
*/
public static void setFrameAlwaysFocus(final JFrame frame) {
new javax.swing.Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//frame.toFront();
}
}).start();
}
/**
* 完全展开一个JTree
* @param tree JTree
*/
public static void expandTree(JTree tree) {
TreeNode root = (TreeNode) tree.getModel().getRoot();
expandAll(tree, new TreePath(root), true);
}
/**
* 完全展开或关闭一个树,用于递规执行
* @param tree JTree
* @param parent 父节点
* @param expand 为true则表示展开树,否则为关闭整棵树
*/
private static void expandAll(JTree tree, TreePath parent, boolean expand) {
// Traverse children
TreeNode node = (TreeNode) parent.getLastPathComponent();
if (node.getChildCount() >= 0) {
for (Enumeration e = node.children(); e.hasMoreElements(); ) {
TreeNode n = (TreeNode) e.nextElement();
TreePath path = parent.pathByAddingChild(n);
expandAll(tree, path, expand);
}
}
// Expansion or collapse must be done bottom-up
if (expand) {
tree.expandPath(parent);
} else {
tree.collapsePath(parent);
}
}
public static JPanel createLabelPanel(JLabel label) {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(Box.createGlue());
Box box = Box.createHorizontalBox();
box.add(label);
p.add(box);
p.add(Box.createGlue());
return p;
}
public static JPanel createLabelPanel(String label) {
return createLabelPanel(new JLabel(label));
}
public static void setFullScreen(Frame frame) {
GraphicsDevice myDevice =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice();
myDevice.setFullScreenWindow(frame);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import com.incors.plaf.alloy.*;
/**
* Swing开发的帮助类,包括了一些实用方法
*/
public abstract class SwingUtil {
/**
* UI中使用的默认字体
*/
private static final javax.swing.plaf.FontUIResource font =
new javax.swing.plaf.FontUIResource("", Font.PLAIN, 12);
public static void setAlloyLookAndFeel() {
try {
com.incors.plaf.alloy.AlloyLookAndFeel.
setProperty("alloy.isLookAndFeelFrameDecoration", "true");
String sn = null;
sn = "v#ej_technologies#uwbjzx#e6pck8";
com.incors.plaf.alloy.AlloyLookAndFeel.setProperty(
"alloy.licenseCode", sn);
AlloyFontTheme fontTheme = new AlloyFontTheme() {
public javax.swing.plaf.FontUIResource getControlTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getMenuTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getSubTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getSystemTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getUserTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getWindowTitleFont() {
return font;
}
};
com.incors.plaf.alloy.AlloyTheme theme = null;
//theme = new com.incors.plaf.alloy.themes.bedouin.BedouinTheme();
theme = new com.incors.plaf.alloy.themes.glass.GlassTheme(fontTheme);
//theme = new com.incors.plaf.alloy.themes.acid.AcidTheme();
javax.swing.LookAndFeel alloyLnF = new com.incors.plaf.alloy.
AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置SwingUI,使用略微好看的中文字体
*/
public static void setTheme() {
try {
javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme
(new javax.swing.plaf.metal.DefaultMetalTheme() {
public javax.swing.plaf.FontUIResource getControlTextFont() {
return font;
}
public javax.swing.plaf.FontUIResource getMenuTextFont() {
return font;
}
});
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Exception e) {
}
}
/**
* 初始化一个窗口的宽度和高度,并使其居中显示
* @param window 窗口,可以为JWindow JFrame或是JDialog
* @param w 宽度
* @param h 高度
*/
public static void initSizeAndLocation(Window window, int w, int h) {
window.setSize(w, h);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation(size.width / 2 - w / 2, size.height / 2 - h / 2);
}
/**
* 设置一个窗口为最大显示
* @param window 窗口,可以为JWindow JFrame或是JDialog
* @deprecated setFullScreeen
*/
public static void setMaxSize(Window window) {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
window.setSize(size.width, size.height - 50);
window.setLocation(0, 0);
}
/**
* 等待一个窗口消失,阻塞当前线程
* @param w 窗口,可以为JWindow JFrame或是JDialog
*/
public static void waitForWindow(Window w) {
try {
while (true) {
if (w.isShowing() == false) {
return;
} else {
Thread.sleep(500L);
}
}
} catch (InterruptedException ex) {
}
}
/**
* 设置某个Frame不能关闭
* @param frame Frame
*/
public static void setFrameCannotClose(JFrame frame) {
//frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
}
/**
* 设置某个对话框不能关闭
* @param dialog Dialog
*/
public static void setDialogCannotClose(JDialog dialog) {
//dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.
INFORMATION_DIALOG);
}
/**
* 设置某个Frame始终在最前
* @param frame Frame
*/
public static void setFrameAlwaysFocus(final JFrame frame) {
new javax.swing.Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//frame.toFront();
}
}).start();
}
/**
* 完全展开一个JTree
* @param tree JTree
*/
public static void expandTree(JTree tree) {
TreeNode root = (TreeNode) tree.getModel().getRoot();
expandAll(tree, new TreePath(root), true);
}
/**
* 完全展开或关闭一个树,用于递规执行
* @param tree JTree
* @param parent 父节点
* @param expand 为true则表示展开树,否则为关闭整棵树
*/
private static void expandAll(JTree tree, TreePath parent, boolean expand) {
// Traverse children
TreeNode node = (TreeNode) parent.getLastPathComponent();
if (node.getChildCount() >= 0) {
for (Enumeration e = node.children(); e.hasMoreElements(); ) {
TreeNode n = (TreeNode) e.nextElement();
TreePath path = parent.pathByAddingChild(n);
expandAll(tree, path, expand);
}
}
// Expansion or collapse must be done bottom-up
if (expand) {
tree.expandPath(parent);
} else {
tree.collapsePath(parent);
}
}
public static JPanel createLabelPanel(JLabel label) {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(Box.createGlue());
Box box = Box.createHorizontalBox();
box.add(label);
p.add(box);
p.add(Box.createGlue());
return p;
}
public static JPanel createLabelPanel(String label) {
return createLabelPanel(new JLabel(label));
}
public static void setFullScreen(Frame frame) {
GraphicsDevice myDevice =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice();
myDevice.setFullScreenWindow(frame);
}
}