gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:20:06 25 4
gpt4 key购买 nike

这是我没有使用 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.setVisible(true)

    之前使用JFrame.pack()
  • 使用了您代码中的大部分细节

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com