JPopupMenu 没有出现?
我有一个 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);
}
}
有什么想法吗?
I have a Java applet that will consist of several popup menus that the user will have to interact with. However, the JPopupMenu won't show up when added. Here is my code:
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);
}
}
Any ideas why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您希望它什么时候出现?
您需要调用 show() 如果你想显示弹出菜单。
请参阅此示例和来自 Oracle 站点的一个。
顺便说一句 - 从你的问题来看,似乎 JDialog
When do you want it to show up?
You need to call show() if you want to display the popup menu.
See this example and the one from oracle site.
BTW - from your question it seems JDialog
必须使用 JComboBox 而不是 JPopupMenu
Had to use a JComboBox instead of JPopupMenu