这是我的背景,没有使用 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);
}
看起来很不错
但是我想使用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);
}
看起来很糟糕 那些顶部和右侧的白边框从哪里来?为什么图像没有完全显示?
最佳答案
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)
使用了代码中的大部分详细信息
编辑
通过删除默认间隙
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/