java - GlassPane 不会显示应用于它的背景图像

标签 java swing

此方法将从 JFrame 获取玻璃面板,该引用存储在 JPanel 对象中,我正在使用 JPanel 对象中的 getGraphics 获取图形引用,然后尝试为玻璃面板绘制背景图像,当我运行此代码时它显示闪烁的背景图像,然后图像消失。我尝试了很多方法,例如使用 getClass.getResources(path) 而不是 File 或扩展 JPanel 并设置图像,因为它具有相同的结果。

private void createProfile()
    {
        gls = (JPanel) mainUi.getGlassPane();

        gls.setLayout(new GridBagLayout());


        Graphics g = gls.getGraphics();
        BufferedImage img=null;
        try {
            img = ImageIO.read(new File("/data/images/Lighthouse.jpg"));
            System.out.println("Hello");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        g.drawImage(img, 0, 0, gls.getWidth()
                ,gls.getHeight(), null);
        //gls.removeAll();


        JPanel profileName = new JPanel(new BorderLayout());

        JPanel profileTitlePanel = new JPanel(new BorderLayout());
        profileTitlePanel.setSize(0, 20);
        profileTitlePanel.setBackground(Color.black);
        JLabel profileTitleLabel = new JLabel("  Create New Profile                      ");
        profileTitleLabel.setForeground(Color.white);

        profileTitleLabel.setFont(getFont(Font.BOLD,20));
        profileTitlePanel.add(profileTitleLabel);

        profileName.add(BorderLayout.NORTH, profileTitlePanel);

        JPanel profileSelection = new JPanel(new GridLayout(5,1));
        profileSelection.add(new JPanel());
        JLabel nameLabel = new JLabel("   Enter Your Name : ");
        Font font = getFont(Font.HANGING_BASELINE,16);
        nameLabel.setFont(font);
        profileSelection.add(nameLabel);


        final JTextField name = new JTextField();

        name.setFont(font);
        profileSelection.add(name);

        profileSelection.add(new JPanel());

        JPanel okClear = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton ok = new JButton("Ok");
        ok.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                File file = new File("data/profiles/"+name.getText()+"/save");
                try {
                    if(!file.exists())
                    {
                        file.mkdirs();
                        new File("data/profiles/"+name.getText()+"/saves.dat").createNewFile();
                        noProfile=false;

                        System.out.println("The Current Profile -> "+noProfile);

//                      mainWindow();
                        currentProfile = name.getText();
                        new DataOutputStream(new FileOutputStream("data/state.dat")).writeBoolean(noProfile);
                        new DataOutputStream(new FileOutputStream("data/current_profile.dat")).writeUTF(currentProfile);
                        //new DataOutputStream(new FileOutputStream(file)).writeInt(1);
                        gls.setVisible(false);
                        mainUi.repaint();
                        mainWindow();
                    }
                    else
                    {
                        createProfile();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });
        okClear.add(ok);
        JButton clear = new JButton("Clear");
        clear.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                name.setText("");
            }

        });
        okClear.add(clear);

        profileSelection.add(okClear);

        profileName.add(profileSelection);

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.ipadx= 10;
        gbc.ipady = 10;


        profileName.setBorder(BorderFactory.createEtchedBorder());
        profileName.setSize(400, 250);

        gls.add(profileName,gbc);



        gls.setVisible(true);
    }

最佳答案

不要使用getGraphics,这不是在 Swing 中完成自定义绘画的方式。

看看Performing Custom Painting了解更多详情。

您应该创建自己的组件,可能使用 JPanel 并覆盖它的 paintComponent 方法以在那里执行自定义绘制。

不要忘记使用 setOpaque(false) 使组件透明,并使用 setGlassPane(...) 应用于根 Pane

参见 How to use root panes了解更多详情

关于java - GlassPane 不会显示应用于它的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19194264/

相关文章:

java - 我怎样才能让这段代码在 Eclipse 之外运行并允许用户在 JFrame 或类似的东西中输入/输出

java - 老程序员希望使用 subversion assembla

java - 在java函数中传递类作为参数

java - 在java JPanel中对齐文本

java - 每次在 Java Swing 中单击 JFrame 中的 JButton 时,如何添加一个新的 JPanel 实例

java - 为什么.bat中编写的命令在java中无限运行?

java 删除一个组件

java - 当用户在其外部单击时如何使模态 JDialog 闪烁

java - 调用单独的 Java 文件来打开第二个 GUI 窗口

java - 在 JLayeredPane 中分层 JLabels