如何在所有miglayout的单元格下“设置”背景图像

时间:2014-03-08 20:34:09

标签: 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

1 个答案:

答案 0 :(得分:5)

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

  • 您可以使用Borders

    的<{1}}实例进行模拟
  • frame.setLayout(new MigLayout("debug", "[400px]", "[300px]"));

  • 之前使用frame.setLayout(new MigLayout("", "[400px]", "[300px]"));
  • 使用了代码中的大部分详细信息

enter image description here

修改

删除默认差距

enter image description here

JFrame.pack()