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

从Java获得有效的屏幕尺寸

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

从Java获得有效的屏幕尺寸

这可以确定没有任务栏的屏幕大小(以像素为单位)

//size of the screenDimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//height of the task barInsets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());int taskBarSize = scnMax.bottom;//available size of the screen setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight());

编辑

有人可以在Xx_nix和Mac OSX上运行此代码,并检查JDialog是否确实位于右下角吗?

import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.UIManager.LookAndFeelInfo;public class NotificationPopup {    private static final long serialVersionUID = 1L;    private LinearGradientPaint lpg;    private JDialog dialog = new JDialog();    private BackgroundPanel panel = new BackgroundPanel();    public NotificationPopup() {        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();        Insets scnMax = Toolkit.getDefaultToolkit().     getScreenInsets(dialog.getGraphicsConfiguration());        int taskBarSize = scnMax.bottom;        panel.setLayout(new GridBagLayout());        GridBagConstraints constraints = new GridBagConstraints();        constraints.gridx = 0;        constraints.gridy = 0;        constraints.weightx = 1.0f;        constraints.weighty = 1.0f;        constraints.insets = new Insets(5, 5, 5, 5);        constraints.fill = GridBagConstraints.BOTH;        JLabel l = new JLabel("You have got 2 new Messages.");        panel.add(l, constraints);        constraints.gridx++;        constraints.weightx = 0f;        constraints.weighty = 0f;        constraints.fill = GridBagConstraints.NONE;        constraints.anchor = GridBagConstraints.NORTH;        JButton b = new JButton(new AbstractAction("x") { private static final long serialVersionUID = 1L; @Override public void actionPerformed(final ActionEvent e) {     dialog.dispose(); }        });        b.setOpaque(false);        b.setMargin(new Insets(1, 4, 1, 4));        b.setFocusable(false);        panel.add(b, constraints);        dialog.setUndecorated(true);        dialog.setSize(300, 100);        dialog.setLocation(screenSize.width - dialog.getWidth(),     screenSize.height - taskBarSize - dialog.getHeight());        lpg = new LinearGradientPaint(0, 0, 0, dialog.getHeight() / 2,     new float[]{0f, 0.3f, 1f}, new Color[]{new Color(0.8f, 0.8f, 1f),         new Color(0.7f, 0.7f, 1f), new Color(0.6f, 0.6f, 1f)});        dialog.setContentPane(panel);        dialog.setVisible(true);    }    private class BackgroundPanel extends JPanel {        private static final long serialVersionUID = 1L;        BackgroundPanel() { setOpaque(true);        }        @Override        protected void paintComponent(final Graphics g) { final Graphics2D g2d = (Graphics2D) g; g2d.setPaint(lpg); g2d.fillRect(1, 1, getWidth() - 2, getHeight() - 2); g2d.setColor(Color.BLACK); g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);        }    }    public static void main(final String[] args) {        try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {     System.out.println(info.getName());     if ("Nimbus".equals(info.getName())) {         UIManager.setLookAndFeel(info.getClassName());         break;     } }        } catch (UnsupportedLookAndFeelException e) {        } catch (ClassNotFoundException e) {        } catch (InstantiationException e) {        } catch (IllegalAccessException e) {        }        SwingUtilities.invokeLater(new Runnable() { @Override public void run() {     NotificationPopup notificationPopup = new NotificationPopup(); }        });    }}


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

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

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