swing中点击一个按钮,在按钮上方弹出一个菜单显示多个图片,提供选择操作,该如何实现:(
菜单的样式类似附件中的图片。
swing弹出菜单显示多个图片的问题?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
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]本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报