当您单击 Jbutton 时,有没有办法显示菜单项列表?例如,如果我单击标有 的 Jbutton wrench
,它会显示一个带有 2 个选项的菜单项列表,就像这个示例一样。这是我的代码(强调的文本**
是我想要在单击时显示菜单项列表的按钮):
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);