点击Jbutton时有没有办法显示菜单项列表?例如,如果我点击标有wrench
的Jbutton,它会显示一个包含2个选项的菜单项列表,例如this example。这是我的代码(用**
强调的文字是我想在点击时显示菜单项列表的按钮):
appletFrame = new JFrame(Settings.serverName);
Loader.webclient = false;
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
JPanel appletPanel = new JPanel(new BorderLayout());
appletFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/icon.png")));
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 655));
appletFrame.getContentPane().add(appletPanel, BorderLayout.CENTER);
appletFrame.pack();
appletFrame.setLocationRelativeTo(null);
appletFrame.setVisible(true);
JMenuBar jmenubar = new JMenuBar();
appletPanel.add(jmenubar, BorderLayout.PAGE_START);
layout = new FlowLayout();
**ImageIcon wrench = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/wrench.png")));**
ImageIcon key = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/key.png")));
JButton button1 = new JButton("Vote");
JButton button2 = new JButton("Item List");
JButton button3 = new JButton("Screenshot");
**JButton button4 = new JButton(wrench);**
JButton button5 = new JButton(key);
**button4.setBorder(null);
**button4.setBorderPainted(false);**
**button4.setContentAreaFilled(false);**
button5.setBorder(null);
button5.setBorderPainted(false);
button5.setContentAreaFilled(false);
jmenubar.setLayout(layout);
jmenubar.add(button1);
jmenubar.add(button2);
jmenubar.add(button3);
**jmenubar.add(button4);**
jmenubar.add(button5);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
**button4.addActionListener(this);**
button5.addActionListener(this);
答案 0 :(得分:0)
你需要SplitButton吗?然后是一些实现的包(包括Netbeans UI组件),你可能会喜欢它们。
如果您需要一个小包装,请查看以下链接: http://code.google.com/p/jsplitbutton/