Java “如何设置背景图像”;在"下,;所有MIG布局';s细胞
这是我的背景,没有使用miglayoutJava “如何设置背景图像”;在"下,;所有MIG布局';s细胞,java,swing,layout,jlabel,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 JL
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(800500)代码>包括
,然后绝对协调的边框
中用作常量的新的migloayout(“,”[800px],“[500px]”
大于migloayout
没有/减去JFrame
,部分图像被画出屏幕边框
- 您可以使用
frame.setLayout(新的migloayout(“调试”)、“[400px]”、“[300px]”进行模拟代码>安装
frame.setLayout(新的miglyout(“,”[400px],“[300px]”)代码>
- 在
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;
}
}
手动设置GUI的大小通常不是一个好主意。相反,让组件以更“自然”的方式调整自身和GUI的大小。另外,使用容器添加到背景上的大多数组件可能不应该是不透明的。谢谢。我试着修正尺寸,去掉不透明的部分。仍然不能得到完全相同的结果。有没有办法设置一个背景图像,使其“位于”所有mig电池下?我试图设置(700400)仍有板,并且图像未完全显示使用MIGLOATED(“插入0”)删除空格和
frame.pack()代码>而不是固定大小。只需添加插入0
让我高兴,您的答案将是完美的:)。@Anthony Accioly但我认为默认间距是可以接受的,在标准布局管理器中,您必须创建…………哦,来吧。。。让我们杀死插入物并繁殖臭虫:)。是的,你做到了!