【问题标题】:Can you display an Image on a certain region of your desktop in Java?你能用 Java 在桌面的某个区域显示图像吗?
【发布时间】:2022-01-16 13:30:34
【问题描述】:

有没有办法使用 JFrame 让图像出现在我的桌面两侧?

我的桌面上同时显示了一个 JOptionPane.showInputDialog 和一个图像,但我不希望它们相互重叠。我宁愿让一个显示在右侧,一个显示在左侧,这样我就不必总是移动窗口。

【问题讨论】:

  • 一个 JOptionPane 将在框架或桌面上居中,这取决于您在方法中使用的参数。如果您想完全控制该位置,那么您需要: 1) 阅读JOptionPane API,您将在其中找到有关“直接使用”的部分,这将使您可以访问选项窗格使用的 JDialog,以便您可以控制位置,或者 2) 只需使用 JDialog,这样您就可以完全控制组件和对话框的位置。

标签: java image swing jframe jpanel


【解决方案1】:

在多屏幕环境中,这可能有点困难,因为您需要考虑很多变量。您可以将多屏幕环境视为“虚拟屏幕”,其中默认屏幕可能不在0x0。此外,添加操作系统菜单/任务栏之类的内容,您还可以处理其他一系列事情。

以下示例显示两个框架,均沿“默认”屏幕的“安全可视区域”底部对齐,并位于“默认”屏幕“安全可视区域”的左侧和右侧

import java.awt.EventQueue;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame happyFrame = new JFrame();
                    happyFrame.add(new HappyPane());
                    happyFrame.pack();

                    JFrame sadFrame = new JFrame();
                    sadFrame.add(new SadPane());
                    sadFrame.pack();

                    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                    GraphicsDevice screenDevice = ge.getDefaultScreenDevice();
                    GraphicsConfiguration gc = screenDevice.getDefaultConfiguration();
                    Rectangle bounds = screenDevice.getDefaultConfiguration().getBounds();

                    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);

                    bounds.x += insets.left;
                    bounds.y += insets.top;
                    bounds.width -= insets.left + insets.right;
                    bounds.height -= insets.top + insets.bottom;

                    happyFrame.setLocation(bounds.x, (bounds.y + bounds.height) - happyFrame.getSize().height);
                    sadFrame.setLocation((bounds.x + bounds.width) - sadFrame.getSize().width, (bounds.y + bounds.height) - sadFrame.getSize().height);

                    happyFrame.setVisible(true);
                    sadFrame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class HappyPane extends JPanel {

        public HappyPane() throws IOException {
            setBorder(new EmptyBorder(16, 16, 16, 16));
            setLayout(new GridBagLayout());
            add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/images/happy.png")))));
        }

    }

    public class SadPane extends JPanel {

        public SadPane() throws IOException {
            setBorder(new EmptyBorder(16, 16, 16, 16));
            setLayout(new GridBagLayout());
            add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/images/sad.png")))));
        }

    }
}

你也可以看看

如果您想了解更多信息,那么您可能真的很想知道 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2019-01-13
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多