简体   繁体   English

如何使用Java制作弹出窗口

[英]how to make a pop-up window using java

I wanted a button to show a pop-up window, I tried using the 我想要一个按钮来显示一个弹出窗口,我尝试使用

JOptionPane.showMessageDialog(null,"");

But I can't put my desired object such as the Table and List. 但是我不能放我想要的对象,例如表和列表。 Is it possible? 可能吗?

Start by taking a closer look at the JOptionPane JavaDocs , showMessageDialog clearly states that it accepts a Object as the message parameter 首先仔细看一下JOptionPane JavaDocsshowMessageDialog明确声明它接受Object作为消息参数。

One of the nice features of this, is if the Object is Component , it will be added to the dialog. 不错的功能之一是,如果ObjectComponent ,它将被添加到对话框中。

For example: JOptionPane displaying HTML problems in Java and How do i make the output come in different columns? 例如: JOptionPane在Java中显示HTML问题,以及如何使输出进入不同的列?

This is really poorly documented in the Java Docs , because all that tells you is that the "message" parameter is an Object, which can be anything - but does not go into specifics about what happens with different types of objects that may warrant special case handling. 这在Java Docs中确实没有很好地记录,因为所有告诉您的是“ message”参数是一个对象,可以是任何对象-但没有详细说明可能需要特殊情况的不同类型的对象会发生什么情况处理。

As far as I have gathered from experimenting with it, the "message" can be a subtype of Component - then it will just place the component in the message area of the dialog box as-is, like: 据我从试验中获得的信息,“消息”可以是Component的子类型-然后它将仅将组件原样放置在对话框的消息区域中,例如:

JOptionPane.showMessageDialog(null, new JCheckBox("I'm a checkbox!"));

Otherwise, it will just call the toString method on the object, converting it into a string, which it then will just wrap in a label and place that in the dialog as the message. 否则,它将仅在对象上调用toString方法,将其转换为字符串,然后将其包装在标签中并将其作为消息放置在对话框中。

But you can also pass in an Array of Objects, in which case it will just place each element in a separate row in the message area: 但是,您也可以传入对象数组 ,在这种情况下,它将仅将每个元素放在消息区域的单独行中:

JOptionPane.showMessageDialog(null, new Object[] {
    new JCheckBox("check"),
    new JRadioButton("radio"),
    "plain text"});

There might be other special cases, but I haven't found them yet. 可能还有其他特殊情况,但我还没有找到它们。

That said, if what you want to display is a subclass of Component (or JComponent), just passing it in as the message parameter should work. 就是说,如果要显示的是Component(或JComponent)的子类,则只需将其作为message参数传递即可。 If it doesn't, you might want to edit your question to describe whatever problems you are encountering in more detail, perhaps also providing some sample code. 如果不是这样,您可能希望编辑问题以更详细地描述遇到的任何问题,也许还提供一些示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM