我有一个 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);
}
}
有什么想法吗?
最佳答案
您希望它什么时候出现?
您需要调用show()如果你想显示弹出菜单。
看这个example和 the one from oracle site .
顺便说一句 - 从你的问题看来 JDialog
关于java - JPopupMenu 没有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004720/