frame

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);
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值