如何通过与setLayout的(零)和背景图像的另一个JPanel的绝对定位一个JPanel?(Ho

2019-09-17 21:01发布

我在Java JFrames工作,特别是对需要重叠的绝对定位的元素。 据我所知,叠加部件,人们应该做出一个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);

... cot'd,但是没有必要。

所以基本上,我想知道如何绝对位置JPanels(或JComponents,如果是简单的)在一个JPanel与背景图像。

感谢您抽空看看这个问题,我已经花了这种方法太多时间; 被注释掉的代码扩展近500线传递什么我张贴,所以我无处可转。 下图显示的是什么,我试图完成粗插图,我不知道如果I'actually接近得到它,因为有时JComponents似乎消失,就好像他们是背景图像的背后,但是我想找到简单的解决方案,它在我的眼前最有可能的吧!

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

Answer 1:

你设置setLayout(null)在你JFrame ,但没有对“backgroundpanel”(其布局默认设置FlowLayout )。

你不应该设置你的布局Login框-因为它是默认设置为BorderLayout -和它的确定(你想要的“backgroundpanel”成长相匹配的父母)。 相反setLayout(null)JPanel - “backgroundpanel” -将你添加任意的定位板。



Answer 2:

我想找到简单的解决方案,它在我的眼前最有可能的吧!

像这样的事情?

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


文章来源: How to absolutely position a JPanel over another JPanel with setLayout(null) and a background image?