gpt4 book ai didi

java - 框架中有多个 JPanel/具有背景图像和顶部有组件的另一层

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:27 24 4
gpt4 key购买 nike

我有一个带有 JPanel 的 JFrame,其中有一个带有 ImageIcon() 的 JLabel。一切都运行良好,问题是我现在想添加另一个 JPanel 以及所有其他东西,如按钮等到 JFrame。但它仍然在顶部显示背景图像,而第二个 JPanel 则没有。

有人可以帮助我吗?这是我的代码的摘录:

JFrame window = new JFrame("Http Download");


/*
* 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 HTTPDownloadGUI() {

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel1.setLayout(null);
panel1.setSize(windowSize);
panel1.setOpaque(false);

panel2.setLayout(null);
panel2.setSize(windowSize);
panel2.setOpaque(false);

lbl1.setSize(windowSize);
lbl1.setLocation(0, 0);
lbl1.setIcon(new ImageIcon(getClass().getResource("bg1.png")));
panel1.add(lbl1);

lbl2.setBounds(0, 0, 100, 100);
//lbl2.setIcon(new ImageIcon(getClass().getResource("bg2.png")));
lbl2.setBackground(Color.GREEN);
panel2.add(lbl2);

panel1.add(panel2);

window.add(panel1);

int X = (screen.width / 2) - (windowSize.width / 2);
int Y = (screen.height / 2) - (windowSize.height / 2);

window.setBounds(X,Y , windowSize.width, windowSize.height);
window.setVisible(true);

}

最佳答案

  1. 避免使用 null 布局,这里麻烦多了才值得
  2. 将框架布局设置为BorderLayout
  3. 将标签添加到框架
  4. 将标签布局设置为BorderLayout
  5. 创建面板并将其不透明属性设置为 false
  6. 正常添加其他组件
  7. 将面板添加到标签

结帐

例子

用例子更新

  • panel1为主背景...
  • panel1的布局设置为BorderLayout
  • lbl1添加到panel1
  • lbl1的布局设置为BorderLayout
  • panel2 的布局设置为您想要使用的...
  • panel2 的不透明度属性设置为 false (panel2.setOpacity(false))
  • lbl2添加到panel2
  • panel2添加到lbl1
  • panel1 添加到您想要的任何地方。

enter image description here

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);

}
}
}

关于java - 框架中有多个 JPanel/具有背景图像和顶部有组件的另一层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13886476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com