编程语言
首页 > 编程语言> > java-为什么从未调用paintComponent?

java-为什么从未调用paintComponent?

作者:互联网

我有以下代码.基本上我有一个带有背景图像的框架.我在框架中也有三个面板:面板1、2和3.2& 3工作正常,因为我没有将它们归类.但是,一旦我将其子类化,即将面板1放入JPanel的paintComponent方法中,逻辑就停止工作,因为该方法从不被调用并且foo从不被打印.我不知道为什么.感谢您的帮助.我尝试了其他类似主题的一些建议,但没有帮助.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) throws IOException {

        JFrame.setDefaultLookAndFeelDecorated(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        int fooPanelX = 5;
        int fooPanelY = 160;
        int fooPanelWidth = 470;
        int fooPanelHeight = 305;

        int bar0PanelX = 5;
        int bar0PanelY = 550;
        int bar0PanelWidth = 230;
        int bar0PanelHeight = 210;

        int bar1PanelX = bar0PanelX * 2 + bar0PanelWidth + bar0PanelX;
        int bar1PanelY = bar0PanelY;
        int bar1PanelWidth = bar0PanelWidth;
        int bar1PanelHeight = bar0PanelHeight;

        JPanel panel1 = new Panel1(fooPanelX, fooPanelY, fooPanelWidth, fooPanelHeight);

        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.WHITE);
        panel2.setLocation(bar0PanelX, bar0PanelY);
        panel2.setSize(bar0PanelWidth, bar0PanelHeight);
        panel2.setOpaque(false);
        panel2.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        panel2.setBounds(bar0PanelX, bar0PanelY, bar0PanelWidth, bar0PanelHeight);

        JPanel panel3 = new JPanel();
        panel3.setBackground(Color.WHITE);
        panel3.setLocation(bar1PanelX, bar1PanelX);
        panel3.setSize(bar1PanelWidth, bar1PanelHeight);
        panel3.setOpaque(false);
        panel3.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        panel3.setBounds(bar1PanelX, bar1PanelY, bar1PanelWidth, bar1PanelHeight);

        JLabel imagePanel = new JLabel(new ImageIcon(ImageIO.read(new File("image.png"))));

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == 27) {
                    System.exit(0);
                }
            }

        });

        frame.setContentPane(imagePanel);
        frame.getContentPane().add(panel1);
        frame.getContentPane().add(panel2);
        frame.getContentPane().add(panel3);
        frame.setLocation((int) (screenSize.getWidth() * 0.75),
                (int) (screenSize.getHeight() * 0.25));
        frame.pack();
        frame.setVisible(true);

    }

    @SuppressWarnings("serial")
    static class Panel1 extends JPanel {

        int x, y, w, h;

        public Panel1(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
        }

        @Override
        public void paintComponent(Graphics graphics) {

            System.out.println("foo");
            super.paintComponents(graphics);

            setBackground(Color.WHITE);
            setLocation(x, y);
            setSize(w, h);
            setOpaque(true);
            setBorder(BorderFactory.createLineBorder(Color.WHITE));

         }

    }

}

更新:如果您找不到问题,那么可以为我提供一种替代方法来执行以下操作.我需要一个带有背景图像的框架,并在背景图像的顶部有三个面板.这三个面板必须在背景图像上具有像素完美的位置和大小,才能看起来正确.就是这样.我将重新粉刷三个面板,但是背景图像将保持不变.

解决方法:

好吧,问题在于您没有使用适当的LayoutManager.

默认情况下,JLabel不附带任何LayoutManager.因此,当您添加Panel1时,它的大小为0x0,位于(0,0)中,并且由于没有LayoutManager会更改它,因此它将保留该大小和位置.具有空边界时,永远不会绘制您的组件,因此永远不会调用paintComponent方法.

现在,您永远不要在paintComponent中执行此操作:

        setBackground(Color.WHITE);
        setLocation(x, y);
        setSize(w, h);
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.WHITE));

在构造函数或其他方法中执行此操作. paintComponent用于“绘制组件”,而不更改其属性.

标签:paintcomponent,swing,linux,java
来源: https://codeday.me/bug/20191031/1976837.html