JFrame.setBackground(Color)修改ImageIcon大小

问题描述 投票:0回答:2

我从几天开始Java,我刚刚开始处理swing和AWT。我正在尝试制作一个透明的动画GIF并将我的窗口放到一个特定的位置。问题是我无法将其放在屏幕的右下角。所以,我尝试了一些事情(让窗口全屏显示并移动JLabel组件,用setBounds移动窗口,...)但我刚刚意识到如果我删除了

this.setBackground(new Color(0, 0, 0, 0));

我失去了透明度,但我的照片也改变了尺寸,并且处于好的位置。

代码:


public class Fenetre extends JFrame {
    private static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    private static final int screen_width = (int) screenSize.width;
    private static final int screen_height = (int) screenSize.height;

    URL url;                //Get the source URL
    Icon icon;              //For animated gif support
    JLabel label;           //Contain the picture

    /**** CONSTRUCTOR ****/
    public Fenetre(String pUrl,String name) throws MalformedURLException{
        url = new URL(pUrl);

        icon = new ImageIcon(url);
        label = new JLabel(icon);

        this.add(label);
        this.setTitle(name);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setUndecorated(true);
        this.setBackground(new Color(0, 0, 0, 0));  

        this.pack();
        this.setLocation(screen_width-label.getWidth(),screen_height-label.getHeight());
        this.setAlwaysOnTop(true);

        this.setVisible(true);
    }
}

我在我的主要实例化我的班级fenetre。

谢谢您的帮助 :)

编辑:我重新制定:我稍后会看到一个更合适的方法来获得屏幕的大小(现在这个工作)。我只有这个:exemple

在这里,我发布了两次:一次使用this.setBackground(new Color(0, 0, 0, 0));,另一次没有。我们可以清楚地看到,一个更大,更适合屏幕(其中一个没有)。

我想要的是在不调整图像大小的情况下获得透明度。

java swing jframe jlabel
2个回答
0
投票

我不是100%我理解你的特殊问题,但是,我会避免使用private static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();作为屏幕尺寸的决定,因为它没有考虑诸如任务栏和底座之类的操作系统元素之类的东西。

以下示例仅在屏幕的底部/右侧显示一个红色方块,避免使用任务栏和停靠栏等操作

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0, 0, 0, 0));  
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                Rectangle bounds = getScreenBounds();
                int x = bounds.x + (bounds.width - frame.getWidth());
                int y = bounds.y + (bounds.height - frame.getHeight());
                frame.setLocation(x, y);
                frame.setVisible(true);
            }
        });
    }

    public static Rectangle getScreenBounds() {
        Rectangle bounds = new Rectangle(0, 0, 0, 0);

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        bounds = gc.getBounds();

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

        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= (insets.right + insets.left);
        bounds.height -= (insets.bottom + insets.top);
        return bounds;
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBackground(Color.RED);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }

    }

}

Updated

我修改了TestPane以包含一个显示组件大小的标签......

public class TestPane extends JPanel {

    private JLabel label;

    public TestPane() {
        setBackground(Color.RED);
        label = new JLabel("...");
        setLayout(new GridBagLayout());
        add(label);
    }

    @Override
    public void invalidate() {
        super.invalidate();
        label.setText(getWidth() + "x" + getHeight());
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 100);
    }

}

有和没有调用frame.setBackground(new Color(0, 0, 0, 0));运行它,看到没有区别


0
投票

似乎安装了最后一个java JDK Dev Kit解决了这个问题。如果有人知道为什么这附加到9.0.4 JRE我非常感兴趣。

© www.soinside.com 2019 - 2024. All rights reserved.