简体   繁体   English

为什么图像不覆盖我的JPanel?

[英]Why doesn't the image paint over my JPanel?

I have been struggling with this for some time. 我已经为此苦苦挣扎了一段时间。 At first, I only used ActionListener, then I added the paintComponent, but I have no idea what to put there. 最初,我仅使用ActionListener,然后添加了paintComponent,但不知道该放在哪里。 I read some tutorials and used their code as an example, but it still doesn't work. 我阅读了一些教程,并以其代码为例,但仍然无法正常工作。 Right now, the end result is the same as it was without PaintComponent. 现在,最终结果与没有PaintComponent时的结果相同。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; 


public class Scream extends JPanel   {
    private JButton button = new JButton("OK");
    private Color screenColor;
    private JPanel panel = new JPanel(); 
    private JFrame frame;
    private Dimension screenSize;
    private ImageIcon image;
    private JLabel label = new JLabel(image);
    private int x;
    private int y; 
    private boolean mouseClicked;

    public Scream() {

        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e ) {
                if (e.getSource() == button) {
                    mouseClicked = true; 
                    frame.getContentPane().add(label);
                    frame.setSize(image.getIconWidth(), image.getIconHeight()); 
                    panel.repaint();

                }
            }
        });

        frame = new JFrame ("Existential angst");
        screenColor = new Color(150, 100, 0);
        panel.setBackground( screenColor );
        frame.add(button, BorderLayout.PAGE_END);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1300, 700);
        frame.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        image.paintComponent(this, g, 1300, 700);
    } 


    public static void main (String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Scream scream = new Scream();
            }
        });
    }
}

If you are trying to dynamically add an image to a panel then you need to add the label to the panel. 如果尝试将图像动态添加到面板,则需要在面板上添加标签。 There is no need for any custom painting. 不需要任何自定义绘画。

The basic code for adding components to a visible GUI is: 将组件添加到可见GUI的基本代码是:

panel.add(...);
panel.revalidate();
panel.repaint();

Also, don't attempt to set the size of the frame to the size of the image. 另外,请勿尝试将框架的尺寸设置为图像的尺寸。 A frame contains a titlebar and borders. 框架包含标题栏和边框。 Instead you can use frame.pack() ; 相反,您可以使用frame.pack()

I noticed a couple of issues: 我注意到了两个问题:

  • image is never initialized to anything so it is null, effectively making the label empty. image永远不会初始化为任何东西,因此它为null,有效地使标签为空。 I assume maybe your example was just incomplete? 我想也许您的例子还不完整?

  • Once I initialized the image to something, your example still did not work. 将图像初始化为某种形式后,您的示例仍然无法正常工作。 Turns out adding label without specifying any constraint basically does nothing (I assume since adding a component to a border layout without a constraint puts it in the center where panel already is). 事实证明,在不指定任何约束的情况下添加标签基本上没有任何作用(我想是因为在没有约束的情况下将组件添加到边框布局会将其放置在面板已经位于的中心)。 When I added the label to BorderLayout.NORTH, everything worked (though resizing the frame to the size of the image makes it only partially visible since the frame includes the OK button) 当我将标签添加到BorderLayout.NORTH时,一切正常(尽管将框架的大小调整为图像的大小使得它仅部分可见,因为框架包括“确定”按钮)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM