Java GUI弹出菜单
我是JavaGUI的初学者,在设计弹出菜单时遇到了问题。我有一个JPanel,想有一个只有两个选项的弹出菜单。这是我的密码:Java GUI弹出菜单,java,swing,user-interface,jmenuitem,jpopupmenu,Java,Swing,User Interface,Jmenuitem,Jpopupmenu,我是JavaGUI的初学者,在设计弹出菜单时遇到了问题。我有一个JPanel,想有一个只有两个选项的弹出菜单。这是我的密码: ActionListener ordermethod = new ActionListener() { public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported
ActionListener ordermethod = new ActionListener() {
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
JPopupMenu popup = new JPopupMenu();
JMenuItem arr = new JMenuItem("first");
arr.addActionListener(ordermethod);
popup.add(arr);
arr = new JMenuItem("second");
arr.addActionListener(ordermethod);
popup.add(arr);
myPanel.add(new JLabel("Pattern ordering method:"), gbc);
myPanel.add(arr, gbc);
但问题是,当我运行代码时,我们会看到JLabel和一个显示“second”的框,当我单击“second”查找“first”时,会看到一长串错误。(这是错误的某些部分)
希望问题很清楚,你能帮助我。谢谢。尝试删除行
抛出新的UnsupportedOperationException(“尚未受支持”)//要更改生成的方法体,请从您的actionPerformed
method…中选择工具|模板。
,并将其替换为所需的应用程序逻辑
ActionListener ordermethod = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello");
}
};
JPopupMenu popup = new JPopupMenu();
JMenuItem arr = new JMenuItem("first");
arr.addActionListener(ordermethod);
popup.add(arr);
arr = new JMenuItem("second");
arr.addActionListener(ordermethod);
popup.add(arr);
myPanel.add(new JLabel("Pattern ordering method:"), gbc);
myPanel.add(arr, gbc);
谢谢你的建议。我这样做了,只要我点击“秒”,我就会看到“Hello”消息。所以,现在您需要替换
JOptionPane.showMessageDialog(null,“Hello”)代码>与您的应用程序逻辑,无论它可能是什么。。。
ActionListener ordermethod = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello");
}
};
JPopupMenu popup = new JPopupMenu();
JMenuItem arr = new JMenuItem("first");
arr.addActionListener(ordermethod);
popup.add(arr);
arr = new JMenuItem("second");
arr.addActionListener(ordermethod);
popup.add(arr);
myPanel.add(new JLabel("Pattern ordering method:"), gbc);
myPanel.add(arr, gbc);