gpt4 book ai didi

java - 如何使用 setLayout(null) 和背景图像将 JPanel 绝对定位在另一个 JPanel 之上?

转载 作者:行者123 更新时间:2023-11-30 05:53:08 32 4
gpt4 key购买 nike

我正在使用 Java 中的 JFrame,特别是需要重叠的绝对定位元素。我知道要覆盖组件,应该制作一个 JPanel(使用 setOpacity(false);),并使用 setBounds(x,y,x2,y2); 定位它> 或 setPosition(x,y) & setSize(x,y)。不幸的是,面板的行为类似于 CSS 的 inline-divs;它们只占用生产线上所需的空间,并且不会堆叠。

这是我目前的代码,但它似乎并不像我想象的那样:

class Login extends JFrame {
private JPanel backgroundpanel;
private JPanel panel;
private JPanel panel2;
private JTextField usernameBox;
private JPasswordField passwordBox;
private JButton button;
private int height = 319;
private int width = 452;
private ImageIcon ii = new ImageIcon("special-window-BG.png");
private JLabel image;

public Login() {
setLayout(null);
setTitle("Login");
setSize(width,height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);

buildPanel();

add(backgroundpanel);
setVisible(true);
}

private void buildPanel() {
usernameBox = new JTextField(20);
passwordBox = new JPasswordField(20);
button = new JButton("Login");
image = new JLabel(ii);

backgroundpanel = new JPanel();
panel = new JPanel();
panel2 = new JPanel();

backgroundpanel.add(panel);
backgroundpanel.add(panel2);
backgroundpanel.add(image);

panel.setBackground(Color.red);
panel.setBounds(0, 0, 10, 10);
panel.setOpaque(false);

panel2.setBackground(Color.blue);
panel2.setBounds(0, 0, 10, 10);
panel2.setOpaque(false);

panel.add(passwordBox);
panel2.add(button);

backgroundpanel.setOpaque(false);
backgroundpanel.isOptimizedDrawingEnabled();
backgroundpanel.setBounds(0, 0, width, height);

...婴儿床,无论多么不必要。

基本上,我想知道如何将 JPanel(或 JComponents,如果更简单的话)绝对定位在具有背景图像的 JPanel 上。

感谢您查看这个问题,我在这个方法上花费了太多时间;注释掉的代码扩展了将近 500 行,超过了我发布的内容,所以我无处可去。下图粗略地展示了我要完成的工作,我不确定我是否真的接近完成它,因为有时 JComponents 似乎消失了,就像它们在背景图像后面一样,但是我想找到最有可能就在我眼前的简单解决方案!

http://i.stack.imgur.com/revz8.jpg

最佳答案

I'd like to find the simple solution that's most likely right in front of my eyes!

是这样的吗?

LoginPanel

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class LoginPanel extends JPanel {

private static final long serialVersionUID = 1L;

BufferedImage image;

LoginPanel(BufferedImage image) {
super(new GridBagLayout());

this.image = image;

JPanel controls = new JPanel(new BorderLayout(15,35));
controls.setOpaque(false);
controls.setBorder(new EmptyBorder(110,0,0,0));

JPanel fields = new JPanel(new GridLayout(0,1,30,30));
fields.setOpaque(false);
controls.add(fields, BorderLayout.CENTER);
fields.add(new JTextField(20));
fields.add(new JPasswordField(20));

JPanel button = new JPanel(new GridBagLayout());
button.setOpaque(false);
controls.add(button, BorderLayout.PAGE_END);
button.add(new JButton("Log In"));

Dimension prefSize = new Dimension(image.getWidth(),image.getHeight());
setPreferredSize(prefSize);

add(controls);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}

public static void main(String[] args) throws Exception {
URL url = new URL("http://i.stack.imgur.com/revz8.jpg");
final BufferedImage image = ImageIO.read(url);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
LoginPanel p = new LoginPanel(image);
JOptionPane.showMessageDialog(null, p);
}
});
}
}

关于java - 如何使用 setLayout(null) 和背景图像将 JPanel 绝对定位在另一个 JPanel 之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10929931/

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