我有一个Java小程序,它包含几个用户必须与之交互的弹出菜单。但是,JPopupMenu在添加时不会显示。这是我的代码:
public class Parser extends JApplet implements ActionListener {
private static final long serialVersionUID = 1L;
JPopupMenu deviceMenu;
JButton downloadButton;
Map <String, Object> deviceDict;
public void init () {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
}
catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
public void createGUI() {
try {
URL url = new URL("[URL]");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
String inputLine;
String xml = "";
while ((inputLine = in.readLine()) != null)
xml = xml + inputLine;
deviceDict = Plist.fromXml(xml);
System.out.print(deviceDict);
}
catch (XmlParseException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
setLayout(new FlowLayout());
setPreferredSize(new Dimension(480, 360));
setSize(480, 360);
Iterator <String> deviceIterator = deviceDict.keySet().iterator();
deviceMenu = new JPopupMenu("Test");
while (deviceIterator.hasNext()) {
JMenuItem item = new JMenuItem(deviceIterator.next());
deviceMenu.add(item);
}
add(deviceMenu);
}
}
任何想法为什么?
答案 0 :(得分:3)
您希望何时出现?
如果要显示弹出菜单,则需要调用show()
请参阅此example和the one from oracle site。
BTW - 从您的问题看来JDialog
答案 1 :(得分:0)
必须使用JComboBox而不是JPopupMenu