我是Java GUI的初学者,设计弹出菜单时遇到问题。我有一个JPanel,想要一个只有两个选项的弹出菜单。这是我的代码:
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”时,会得到一长串错误。 (这是错误的一部分)
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at main.Main$1.actionPerformed(Main.java:53)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
希望问题很清楚,你可以帮助我。感谢。
答案 0 :(得分:3)
尝试从throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
方法中删除行actionPerformed
...并将其替换为您所需的应用程序逻辑
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);