>避免空布局,这里更麻烦,然后它们是值得的
>将框架设置为BorderLayout
>将标签添加到框架中
>将标签布局设置为BorderLayout
>创建面板并将其opaque属性设置为false
>按正常情况添加其他组件
>将面板添加到标签
查看
举些例子
以示例更新
> panel1是主要背景……
>将panel1的布局设置为BorderLayout
>将lbl1添加到panel1
>将lbl1的布局设置为BorderLayout
>将panel2的布局设置为您想要使用的内容……
>将panel2的opacity属性设置为false(panel2.setOpacity(false))
>将lbl2添加到panel2
>将panel2添加到lbl1
>将panel1添加到您想要的任何内容中.
public class TestLayout17 {
public static void main(String[] args) {
new TestLayout17();
}
public TestLayout17() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
/*
* Background Section
*/
JPanel panel1 = new JPanel();
JLabel lbl1 = new JLabel();
/*
* Component Section
*/
JPanel panel2 = new JPanel();
JLabel lbl2 = new JLabel();
/*
* Dimension Section
*/
Dimension windowSize = new Dimension(800, 600);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
public TestPane() {
setLayout(new BorderLayout());
panel1.setLayout(new BorderLayout());
lbl1.setLayout(new BorderLayout());
URL url = getClass().getResource("/bg1.gif");
System.out.println(url);
try {
BufferedImage image = ImageIO.read(url);
Image smaller = image.getScaledInstance(-1, image.getHeight() / 2, Image.SCALE_SMOOTH);
lbl1.setIcon(new ImageIcon(smaller));
} catch (Exception e) {
e.printStackTrace();
}
// lbl1.setIcon(new ImageIcon(url));
panel1.add(lbl1);
add(panel1);
panel2.setLayout(new GridBagLayout());
panel2.setOpaque(false);
lbl2.setBorder(new EmptyBorder(8, 8, 8, 8));
lbl2.setBackground(Color.GREEN);
lbl2.setText("Say hello");;
lbl2.setOpaque(true);
panel2.add(lbl2);
lbl1.add(panel2);
}
}
}