java - 如何设置背景图像 "under"所有miglayout的单元格

标签 java swing layout jlabel miglayout

这是我的背景,没有使用 miglayout

private void initialize() {
        frame = new JFrame();
        frame.setSize(800, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel background=new JLabel(new ImageIcon("pic1.jpg"));
        frame.add(background); 
}

看起来很不错 enter image description here


但是我想使用MigLayout

这是代码

private void initialize() {
    frame = new JFrame();
    frame.setSize(800, 500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new MigLayout("", "[800px]", "[500px]"));

    JLabel background=new JLabel(new ImageIcon("pic1.jpg"));
    frame.getContentPane().add(background, "cell 0 0,grow"); 
            // also tried the following to force background "under" all cells 
            // frame.getContentPane().add(background); 
 }

看起来很糟糕 那些顶部和右侧的白边框从哪里来?为什么图像没有完全显示? enter image description here

最佳答案

  • frame.setSize(800, 500);包括Borders ,则绝对协调new MigLayout("", "[800px]", "[500px]")用作 MigLayout 中的常量大于 JFrame无/减Borders ,图像的一部分被绘制在屏幕之外,

  • 您可以使用 frame.setLayout(new MigLayout("debug", "[400px]", "[300px]")); 进行模拟而是frame.setLayout(new MigLayout("", "[400px]", "[300px]"));

  • 使用JFrame.pack()之前JFrame.setVisible(true)

  • 使用了代码中的大部分详细信息

enter image description here

编辑

通过删除默认间隙

enter image description here

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import net.miginfocom.swing.MigLayout;


public class MigAndIcon {

    private JLabel label = new JLabel();
    private Random random = new Random();
    private Timer backTtimer;
    private JFrame frame = new JFrame("Test");

    public MigAndIcon() {
        //frame.setLayout(new MigLayout("", "[400px]", "[300px]"));
        frame.setLayout(new MigLayout("insets 0, debug", "[400px]", "[300px]"));
        frame.add(label, "cell 0 0, grow"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        startBackground();
        frame.setVisible(true);
    }

    private void startBackground() {
        backTtimer = new javax.swing.Timer(1500, updateBackground());
        backTtimer.start();
        backTtimer.setRepeats(true);
    }

    private Action updateBackground() {
        return new AbstractAction("Background action") {
            private final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                label.setIcon(new ImageIcon(getImage()));
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MigAndIcon t = new MigAndIcon();
            }
        });
    }

    public BufferedImage getImage() {
        int w = label.getWidth();
        int h = label.getHeight();
        GradientPaint gp = new GradientPaint(0f, 0f, new Color(
                127 + random.nextInt(128),
                127 + random.nextInt(128),
                127 + random.nextInt(128)),
                w, w,
                new Color(random.nextInt(128), random.nextInt(128), random.nextInt(128)));
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
        g2d.setColor(Color.BLACK);
        return bi;
    }
}

关于java - 如何设置背景图像 "under"所有miglayout的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274722/

相关文章:

c# - 登录前隐藏菜单,登录后显示

java - 是否可以并行初始化多个惰性集合?

java - 单元测试 - 在哪里放置初始化代码?

JAVA 用空格填充字符串(JFrame)

Java swing 设置大小

java - 在 Swing Java 中创建自定义列表

android - TextInputLayout 轮廓提示颜色

layout - Webmatrix 布局——插入到头部?

java - 如何在Jtable中制作多行单元格

java - 为什么要获得具有相同时区的不同 localDate