如何使用setLayout(null)和背景图像将JPanel绝对定位在另一个JPanel上? [英] How to absolutely position a JPanel over another JPanel with setLayout(null) and a background image?
问题描述
我正在使用Java中的JFrames,特别是需要重叠的绝对定位元素。我知道要覆盖组件,应该制作JPanel(使用 setOpacity(false);
),并使用 setBounds(x,y)定位它,x2,y2);
或 setPosition(x,y)
& 的setSize(X,Y)
。不幸的是,这些面板就像CSS的 inline-divs
;他们只占用了他们所需的房间数量,而且不会堆叠。
I'm working with JFrames in Java, specifically with absolutely positioned elements that need to overlap. I understand that to overlay components, one should make a JPanel (with setOpacity(false);
), and position it with either setBounds(x,y,x2,y2);
or setPosition(x,y)
& setSize(x,y)
. Unfortunately the panels act like CSS's inline-divs
; they take up only the needed amount of room on their line, and do not stack.
这是我到目前为止的代码,但它似乎没有采取行动就像我想象的那样:
This is the code I have so far, but it doesn't seem to act like I'd imagine it would:
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,但不必要。
...cot'd, however unnecessary.
所以基本上,我想知道如何在具有背景图像的JPanel上绝对定位JPanels(或JComponents,如果这更简单)。
So basically, I'd like to know how to absolutely position JPanels (or JComponents, if that's simpler) over a JPanel with a background-image.
感谢您看一下这个问题,我花了太多时间在这个方法上;注释掉的代码通过我发布的内容延伸了近500行,所以我无处可去。下面的图片显示了我想要完成的内容的粗略说明,我不确定我是否真的接近了它,因为有时JComponents似乎消失,好像它们在背景图像后面,但是我想找到最有可能在我面前的简单解决方案!
Thanks for taking a look at this question, I've spent far too much time on this method; the commented-out code extends nearly 500 lines passed what I posted, so I have nowhere else to turn to. The image below shows a crude illustration of what I'm trying to accomplish, I'm not sure if I'actually come close to getting it yet, because sometimes the JComponents seem to disappear as if they're behind the background image, however I'd like to find the simple solution that's most likely right in front of my eyes!
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!
这样的东西?
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);
}
});
}
}
这篇关于如何使用setLayout(null)和背景图像将JPanel绝对定位在另一个JPanel上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!