如何将 JFileChooser 放置在所有窗口的顶部?
在 Java 中,当使用 JFileChooser 时,文件选择器对话框可能会令人沮丧隐藏在其他窗户后面。要解决这个问题,必须了解 showOpenDialog() 的 API,它引用了 showDialog()。根据文档,“如果父级为空,则对话框依赖于不可见的窗口,并且它被放置在依赖于外观和感觉的位置,例如屏幕中心。”
To将选择器放置在屏幕中央,请考虑以下代码示例:
package gui; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.KeyStroke; /** * @see http://stackoverflow.com/questions/8507521 * @see http://stackoverflow.com/questions/5129294 */ public class ImageApp extends JPanel { private static final int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); private JFileChooser chooser = new JFileChooser(); private Action openAction = new ImageOpenAction("Open"); private Action clearAction = new ClearAction("Clear"); private JPopupMenu popup = new JPopupMenu(); private BufferedImage image; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ImageApp().create(); } }); } public void create() { JFrame f = new JFrame();
以上是Java中如何始终将JFileChooser显示在顶部?的详细内容。更多信息请关注PHP中文网其他相关文章!