繁体   English   中英

组件在JPanel中不可见

[英]Component not visible in JPanel

我在名为Box的JFrame的容器中有JPanel

public Box(){
        add(new Ball());
    }

    public void paint(Graphics g){
        g.setColor(Color.WHITE);
        g.fillRect(OFFSET, OFFSET, WIDTH, HEIGHT);
        g.setColor(Color.BLACK);
        g.drawRect(OFFSET, OFFSET, WIDTH, HEIGHT);
    }

球延伸组件并绘制球

public class Ball extends Component{
   ...
public void paint(Graphics g){
    g.setColor(Color.BLACK);
    g.fillOval(xCoord, yCoord, radius, radius);
}
   ...
}

当我将带有球的盒子添加到容器中时,我只能看到盒子。 如果我只添加一个球,我可以看到该球。

有谁知道为什么将球添加到盒子后不可见?

除了覆盖paintComponent ,还可以使用LayoutManager自动设置边界。 为了进行测试,可以将Box实例的LayoutManager设置为null,并在Ball实例上使用setBounds

  1. 请勿混用重量轻的组件。 您应该改为扩展JComponent
  2. 您应该覆盖paintComponent() ,而不是paint()
  3. Ball有大小吗? 如果您没有为Ball提供Dimension ,则它将不可见。

在Swing中,通常不应覆盖paint方法。 请改用paintComponent

有三个可能的错误

1 /使用JLabel简单的绘制

2 /通过javax.swing.Timer计时

3 /用paintComponents代替paint (对于AWT CompoentsDefaultXxxUI

并放在一起,例如

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

public class AnimationJPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private int cx = 0;
    private int cy = 150;
    private int cw = 20;
    private int ch = 20;
    private int xinc = 1;
    private int yinc = 1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                AnimationJPanel panel = new AnimationJPanel();
                panel.setPreferredSize(new Dimension(400, 300));
                panel.animate();
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public AnimationJPanel() {
        setLayout(new BorderLayout());
        JLabel label = new JLabel("This is an AnimationJPanel");
        label.setForeground(Color.RED);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        add(label);
        setBackground(Color.BLACK);
        setForeground(Color.RED);
        setOpaque(true);
    }

    public void animate() {
        new Timer(15, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Rectangle oldCircle = new Rectangle(cx - 1, cy - 1, cw + 2, ch + 2);
                cx += xinc;
                cy += yinc;
                if (cx >= getWidth() - cw || cx <= 0) {
                    xinc *= -1;
                }
                if (cy >= getHeight() - ch || cy <= 0) {
                    yinc *= -1;
                }
                repaint(oldCircle);
                repaint(cx - 1, cy - 1, cw + 2, ch + 2);
            }
        }).start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawOval(cx, cy, cw, ch);
    }
}

暂无
暂无

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

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