CHE*_*HKA 2 java layout swing 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);
}
Run Code Online (Sandbox Code Playgroud)
它看起来非常好
但我想使用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);
}
Run Code Online (Sandbox Code Playgroud)
看起来很糟糕那些顶级和右白色的寄宿生来自哪里?为什么图像没有完全显示?
frame.setSize(800, 500);
是包含Borders
,然后绝对协调new MigLayout("", "[800px]", "[500px]")
用作常数MigLayout
大于JFrame
无/负Borders
,你的图像的一部分被画在屏幕外,
你可以使用frame.setLayout(new MigLayout("debug", "[400px]", "[300px]"));
instread 来模拟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;
}
}
Run Code Online (Sandbox Code Playgroud)