如何重叠JPanel(背景)和JTextField

时间:2019-06-21 16:08:44

标签: java swing jframe jpanel jtextfield

我正在为某人的毕业创建登录应用程序,我需要几个文本字段和一个背景,我添加了背景,现在需要添加文本字段,问题是它们似乎不会排在最前面彼此。

我已经分别尝试了它们,没有彼此,它们都可以正常工作,但是我无法将它们堆叠在一起,我在该站点上看到了几个答案来处理类似的问题,但是对于此应用程序,我需要放置几个背景上的文本字段仅一个,这就是我到目前为止所拥有的...

        //creates the frame with a title as a parameter
        JFrame frame = new JFrame("Sign In Sheet");
        //sets the size
        frame.setSize(1000, 556);
        //makes it so the application stops running when you close it
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //puts it in the center of the screen
        frame.setLocationRelativeTo(null);
        //makes it so you can't resize it
        frame.setResizable(false);

        //setting the background by looking for the image
        try{
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Gabriel R. Warner/Desktop/clouds.png")))));
        }catch(IOException e){
            //and prints an error message if it's not found
            System.out.println("well it didn't work");
        }

        //adding text fields with names apropriate to function
        JTextField name1 = new JTextField();
        name1.setPreferredSize(new Dimension(200, 15));
        name1.setBackground(Color.WHITE);
        frame.add(name1);

        //makes frame visible
        frame.setVisible(true);

简单地说文本字段不会显示背景,所有结果仅提供单个文本字段的答案

1 个答案:

答案 0 :(得分:1)

问题出在这一行:frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Gabriel R. Warner/Desktop/clouds.png")))));

在此行中,将JLabel设置为JFrame的内容窗格。然后,您frame.add(name1);正在将JTextField添加到JLabel ...这似乎不正确,对吧?

答案是创建一个新的JPanel,将背景图像添加到此面板,将面板设置为框架的内容窗格,最后将文本字段添加到面板/ contentpane。

示例:

@SuppressWarnings("serial")
public class FrameWithBackgroundImage extends JFrame {

    public FrameWithBackgroundImage() {
        super("Sign In Sheet");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        try {
            Image bgImage = loadBackgroundImage();
            JPanel backgroundImagePanel = new JPanel() {

                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    g.drawImage(bgImage, 0, 0, null);
                }
            };

            setContentPane(backgroundImagePanel);
        } catch (IOException e) {
            e.printStackTrace();
        }

        JTextField textField = new JTextField(10);

        add(textField);

    }

    private Image loadBackgroundImage() throws IOException {
        File desktop = new File(System.getProperty("user.home"), "Desktop");
        File image = new File(desktop, "img.jpg");
        return ImageIO.read(image);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new FrameWithBackgroundImage().setVisible(true);
        });
    }
}

预览: enter image description here 值得一读的问题:Simplest way to set image as JPanel background