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

📄 jpopupbutton.java

📁 一个KTV管理系统
💻 JAVA
字号:
package view.mainframe.systemmaintenance;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.Serializable;

import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPopupMenu;
import javax.swing.border.AbstractBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.basic.BasicButtonUI;


import com.sun.java.swing.plaf.windows.WindowsButtonUI;
import   java.awt.Point;  
/**
 * <p>Title: OpenSwing</p>
 * <p>Description:弹出式菜单按钮 </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
 * @version 1.0
 */

public class JPopupButton
    extends JComboBox
    implements Serializable {
    /**
     * 普通按钮
     */
    public static final int TYPE_NORMAL = 0;

    /**
     * 两个按钮组合成的弹出菜单按钮
     */
    public static final int TYPE_WITH_RIGHT_TOGGLE = 1;

    /**
     * 按钮类型
     */
    private int style = -1;

    /**
     * 合成按钮左按钮的事件
     */
    private int actionIndex = -1;

    /**
     * 弹出菜单
     */
    private JPopupMenu popup = null;

    /**
     * 是否需要更新
     */
    private boolean mustRefresh = false;

    /**
     * 左按钮
     */
    private JButton bttLeft;

    /**
     * 右按钮
     */
    private JButton bttRight;

    /**
     * 按钮事件
     */
    private PopupButtonListener listener = new PopupButtonListener();

//    javax.swing.plaf.BorderUIResource$EmptyBorderUIResource emptyBorder = new
//        javax.swing.plaf.BorderUIResource$EmptyBorderUIResource(new Insets(0, 0,
//        0, 0));

    /**
     * <p>Title: OpenSwing</p>
     * <p>Description: 鼠标移入时的边界</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
     * @version 1.0
     */
    public class UpBorder
        extends AbstractBorder {
        int thickness = 1;
        public void paintBorder(Component c, Graphics g, int x, int y,
                                int width, int height) {
            g.setColor(Color.white);
            g.drawLine(0, 0, width - 1, 0);
            g.drawLine(0, 0, 0, height - 1);
            g.setColor(Color.gray);
            g.drawLine(width - 1, 0, width - 1, height);
            g.drawLine(0, height - 1, width, height - 1);
        }

        public Insets getBorderInsets(Component c) {
            return new Insets(thickness, thickness, thickness, thickness);
        }
    }

    /**
     * <p>Title: OpenSwing</p>
     * <p>Description: 鼠标按下时的边界</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
     * @version 1.0
     */
    public class DownBorder
        extends AbstractBorder {
        int thickness = 1;
        public void paintBorder(Component c, Graphics g, int x, int y,
                                int width, int height) {
            g.setColor(Color.gray);
            g.drawLine(0, 0, width - 1, 0);
            g.drawLine(0, 0, 0, height - 1);
            g.setColor(Color.white);
            g.drawLine(width - 1, 0, width - 1, height);
            g.drawLine(0, height - 1, width, height - 1);
        }

        public Insets getBorderInsets(Component c) {
            return new Insets(thickness, thickness, thickness, thickness);
        }
    }

    /**
     * <p>Title: OpenSwing</p>
     * <p>Description: 按钮各种动作时边界变化以及事件处理</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
     * @version 1.0
     */
    private class PopupButtonListener
        implements MouseListener, PopupMenuListener {
        UpBorder upBorder = new UpBorder();
        DownBorder downBorder = new DownBorder();
        public void mouseClicked(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
            if (!JPopupButton.this.isEnabled()) {
                return;
            }
            if (e.getSource() == bttLeft) {
                bttLeft.setBorder(downBorder);
                bttRight.setBorder(downBorder);
                if (getStyle() == TYPE_NORMAL) {
                    JPopupButton.this.showPopupMenu();
                }
                else if (actionIndex != -1
                         && actionIndex < popup.getSubElements().length) {
                    AbstractButton btt = (AbstractButton) (popup.getSubElements()
                        [actionIndex].getComponent());
                    btt.doClick();
                }
            }
            else {
                bttLeft.setBorder(upBorder);
                bttRight.setBorder(downBorder);
                bttRight.setSelected(true);
                JPopupButton.this.showPopupMenu();
            }
        }

        public void mouseReleased(MouseEvent e) {
            bttRight.setSelected(false);
        }

        public void mouseEntered(MouseEvent e) {
            if (!JPopupButton.this.isEnabled()) {
                return;
            }
            if (!JPopupButton.this.popup.isShowing()) {
                bttLeft.setBorder(upBorder);
                bttRight.setBorder(upBorder);
            }
        }

        public void mouseExited(MouseEvent e) {
            if (!JPopupButton.this.isEnabled()) {
                return;
            }

            if (!JPopupButton.this.popup.isShowing()) {
                bttLeft.setBorder(null);
                bttRight.setBorder(null);
            }
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            if (!JPopupButton.this.isEnabled()) {
                return;
            }

            bttLeft.setBorder(null);
            bttRight.setBorder(null);
        }

        public void popupMenuCanceled(PopupMenuEvent e) {
        }
    }

    public JPopupButton() {
        this(TYPE_NORMAL);
    }

    public JPopupButton(int style) {
        this(style, null);
    }

    public JPopupButton(int style, String text) {
        this(style, text, null);
    }

    public JPopupButton(int style, String text, Icon icon) {
        this(style, text, icon,  new JPopupMenu());
    }

    /**
     * @param style int 按钮类型
     * @param text String 显示文字
     * @param icon Icon 显示图标
     * @param popup JPopupMenu 弹出菜单
     */
    public JPopupButton(int style, String text, Icon icon, JPopupMenu popup) {
        createButtons();
        setText(text);
        setIcon(icon);
        setPopup(popup);
        setStyle(style);
    }

    /**
     * 显示菜单
     */
    protected void showPopupMenu() {
        if (popup == null) { 
          return;
        }
        popup.show(this, 0, this.getHeight());
    }
    

    protected void createButtons() {
        if (bttLeft == null) {
            bttLeft = new JButton(){
                public void setUI(ButtonUI ui) {
                    if(ui instanceof WindowsButtonUI){
                       ui = new BasicButtonUI();
                    }
                    super.setUI(ui);
                }
            };
            bttLeft.setMargin(new Insets(0, 0, 0, 0));
        }
        if (bttRight == null) {
            bttRight = new JButton() {
                public void setUI(ButtonUI ui) {
                    if(ui instanceof WindowsButtonUI){
                       ui = new BasicButtonUI();
                    }
                    super.setUI(ui);
                }
                public void paint(Graphics g) {
                    super.paint(g);
                    Polygon p = new Polygon();
                    int w = getWidth();
                    int y = (getHeight() - 4) / 2;
                    int x = (w - 6) / 2;
                    if (isSelected()) {
                        x += 1;
                    }
                    p.addPoint(x, y);
                    p.addPoint(x + 3, y + 3);
                    p.addPoint(x + 6, y);
                    g.fillPolygon(p);
                    g.drawPolygon(p);
                }
            };
            bttRight.setUI(new BasicButtonUI());
            bttLeft.setMargin(new Insets(0, 0, 0, 0));
        }
    }

    /**
     * 更新组件布局
     */
    protected void refreshUI() {
        if (!mustRefresh) {
            return;
        }
        super.removeAll();
        this.setBorder(null);
        this.setLayout(new BorderLayout());
        this.add(bttLeft, BorderLayout.WEST);
        if (style == TYPE_WITH_RIGHT_TOGGLE) {
            this.add(bttRight, BorderLayout.EAST);
        }
        bttLeft.setFocusable(false);
        bttLeft.setBorder(null);
        bttLeft.addMouseListener(listener);
        bttRight.setFocusable(false);
        bttRight.setPreferredSize(new Dimension(13, 1));
        bttRight.setBorder(null);
        bttRight.addMouseListener(listener);
        this.doLayout();
    }

    /**
     * 设置按钮类型
     * @param style int
     */
    public void setStyle(int style) {
        if (this.style != style) {
            mustRefresh = true;
        }
        this.style = style;
        refreshUI();
    }

    /**
     * 取得按钮类型
     * @return int
     */
    public int getStyle() {
        return style;
    }

    /**
     * 设置显示文字
     * @param text String
     */
    public void setText(String text) {
        bttLeft.setText(text);
    }

    /**
     * 取得显示文字
     * @return String
     */
    public String getText() {
        return bttLeft.getText();
    }

    /**
     * 设置灰化状态
     * @param enabled boolean
     */
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        bttLeft.setEnabled(enabled);
        bttRight.setEnabled(enabled);
    }


    /**
     * 设置弹出式菜单
     * @param pop JPopupMenu
     */
    public void setPopup(JPopupMenu pop) {
        if (this.popup != null) {
            popup.removePopupMenuListener(listener);
        }
        this.popup = pop;
        popup.removePopupMenuListener(listener);
        popup.addPopupMenuListener(listener);
        
    }


    /**
     * 设置显示图标
     * @param icon Icon
     */
    public void setIcon(Icon icon) {
        bttLeft.setIcon(icon);
    }

    /**
     * 取得显示图标
     * @return Icon
     */
    public Icon getIcon() {
        return bttLeft.getIcon();
    }

    public void updateUI() {
      super.updateUI();
      bttLeft = null;
      bttRight = null;
      createButtons();
      setText(getText());
      setIcon(getIcon());

      showPopupMenu();
      

      setStyle(getStyle());

    }

    /**
     * 测试代码
     * @param args String[]
     */
//    public static void main(String[] args) {
//        JFrame frame = OpenSwingUtil.createDemoFrame("JPopupButton Demo");
//        class DemoAction
//            extends AbstractAction {
//            public DemoAction(String name) {
//                super.putValue(Action.NAME,name);
//            }
//
//            public void actionPerformed(ActionEvent e) {
////                JOptionPane.showMessageDialog( (Component) e.getSource(),
////                                              super.getValue(Action.NAME));
//                String ename = super.getValue(Action.NAME).toString();
//                if(ename.equals("在线帮助")){
//                  System.out.println("a");
//                }else if(ename.equals("开发日记")){
//                  System.out.println("b");
//                }else if(ename.equals("关于软件")){
//                  System.out.println("c");
//                }
//            }
//        }
//
//        JPopupMenu popup = new JPopupMenu("PopupMenu");
//        popup.add(new DemoAction("在线帮助"));
//        popup.add(new DemoAction("开发日记"));
//        popup.add(new DemoAction("关于软件"));
//        
//        JPopupButton btt1 = new JPopupButton(
//            JPopupButton.TYPE_NORMAL, "Button1", null,
//            popup);
//        
//        btt1.setPreferredSize(new Dimension(60, 25));
//        btt1.setSize(new Dimension(60, 25));
//        btt1.setMaximumSize(new Dimension(60, 25));
//        frame.setLayout(null);
//        frame.add(btt1);
//      
//        frame.setVisible(true);
//    }

}

⌨️ 快捷键说明

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