sunmonve 2010-04-29 22:59
浏览 331
已采纳

swing弹出菜单显示多个图片的问题?



 swing中点击一个按钮,在按钮上方弹出一个菜单显示多个图片,提供选择操作,该如何实现:(
菜单的样式类似附件中的图片。

  • 写回答

1条回答 默认 最新

  • g_johnson_lee 2010-04-30 10:26
    关注

    没法传附件,直接上代码吧,你自己弄五张表情的gif图片放到工程/org/zergle/test/swing/faces/目录下,再弄一张face.gif放到/org/zergle/test/swing/icons/下作按钮的图标

    [code="java"]
    package org.zergle.test.swing;

    import javax.swing.Icon;
    import javax.swing.ImageIcon;

    public class FaceManager {
    private static final String FACE_PKG = "/org/zergle/test/swing/faces/";
    private static final String ICON_PKG = "/org/zergle/test/swing/icons/";
    private static final Icon[] FACES;

    static {
        FACES = new ImageIcon[5];
        for (int i = 0; i < FACES.length; i++) {
            FACES[i] = new ImageIcon(FaceManager.class.getResource(FACE_PKG + (i + 1) + ".gif"));
        }
    }
    
    private FaceManager() {}
    
    public static Icon[] getFaces() {
        return FACES;
    } 
    
    public static Icon getIcon(String name) {
        return new ImageIcon(FaceManager.class.getResource(ICON_PKG + name + ".gif"));
    }
    

    }
    [/code]
    [code="java"]
    package org.zergle.test.swing;

    import java.awt.FlowLayout;
    import javax.swing.Icon;
    import javax.swing.JLabel;
    import javax.swing.JPopupMenu;

    public class PopupFace extends JPopupMenu {
    private static final long serialVersionUID = -6732655684556273891L;
    private FlowLayout layout = new FlowLayout(FlowLayout.LEFT);

    public PopupFace() {
        layout.setVgap(0);
        layout.setHgap(0);
        this.setLayout(layout);
        this.setPopupSize(400, 300);
    }
    
    public void add(Icon icon) {
        JLabel face = new JLabel(icon);
        super.add(face);
    }
    

    }
    [/code]
    [code="java"]
    package org.zergle.test.swing;

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Icon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class QQLikeFrame extends JFrame {
    private static final long serialVersionUID = -3124258696363228133L;
    private JPanel pnlSouth = new JPanel();
    private JButton btnFace = new JButton(FaceManager.getIcon("face"));
    private PopupFace popupFace = new PopupFace();

    private JPanel container;
    
    public QQLikeFrame() {
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
        Icon[] faces = FaceManager.getFaces();
        for (int i = 0; i < faces.length; i++) {
            this.popupFace.add(faces[i]);
        }
        this.pnlSouth.add(this.btnFace);
        this.btnFace.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                popupFace.show(QQLikeFrame.this, 0, 0);
            }
        });
        this.container = (JPanel) this.getContentPane();
        this.container.add(pnlSouth, BorderLayout.SOUTH);
        this.setSize(600, 400);
        this.setVisible(true);
    }
    
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        new QQLikeFrame();
    }
    

    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试