栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java将JFileChooser置于所有窗口之上

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java将JFileChooser置于所有窗口之上

API showOpenDialog()
指向
showDialog()
,它表示:“如果父级为null,则对话框不依赖可见窗口,并且放置在与外观相关的位置,例如屏幕的中心。”

下面的示例将选择器放置在

L&F
的屏幕中央。你可能会看到它与你的相比。

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;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();        f.setTitle("Title");        f.add(new JScrollPane(this), BorderLayout.CENTER);        JMenuBar menuBar = new JMenuBar();        JMenu menu = new JMenu("File");        menu.setMnemonic('F');        menu.add(new JMenuItem(openAction));        menu.add(new JMenuItem(clearAction));        menuBar.add(menu);        f.setJMenuBar(menuBar);        f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        f.pack();        f.setSize(new Dimension(640, 480));        f.setLocationRelativeTo(null);        f.setVisible(true);    }    public ImageApp() {        this.setComponentPopupMenu(popup);        popup.add("Popup Menu");        popup.add(new JMenuItem(openAction));        popup.add(new JMenuItem(clearAction));    }    @Override    public Dimension getPreferredSize() {        if (image == null) { return new Dimension();        } else { return new Dimension(image.getWidth(), image.getHeight());        }    }    @Override    public void paintComponent(Graphics g) {        super.paintComponent(g);        g.drawImage(image, 0, 0, null);    }    private class ClearAction extends AbstractAction {        public ClearAction(String name) { super(name); this.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C); this.putValue(Action.ACCELERATOR_KEY,     KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK));        }        @Override        public void actionPerformed(ActionEvent e) { image = null; revalidate(); repaint();        }    }    private class ImageOpenAction extends AbstractAction {        public ImageOpenAction(String name) { super(name); this.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O); this.putValue(Action.ACCELERATOR_KEY,     KeyStroke.getKeyStroke(KeyEvent.VK_O, MASK));        }        @Override        public void actionPerformed(ActionEvent e) { int returnVal = chooser.showOpenDialog(chooser); if (returnVal == JFileChooser.APPROVE_OPTION) {     File f = chooser.getSelectedFile();     try {         image = ImageIO.read(f);         revalidate();         repaint();     } catch (IOException ex) {         ex.printStackTrace(System.err);     } }        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385110.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号