组件在JPanel中不可见

时间:2011-07-23 20:36:03

标签: java swing graphics awt paint

我在一个名为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);
    }

Ball扩展Component并绘制球

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

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

有人知道为什么在添加到Box时球不可见吗?

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

  1. 不要混用重量级和轻量级组件。您应该改为JComponent
  2. 您应该覆盖paintComponent(),而不是paint()
  3. Ball是否有尺寸?如果您未向Ball提供Dimension,则无法显示。

答案 2 :(得分:2)

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

答案 3 :(得分:1)

有三个可能的错误

1 /使用JLabel

进行最简单的绘画

2 /时间javax.swing.Timer

3 / paintComponents代替paint(代表AWT Compoents并绘制DefaultXxxUI

并将它们放在一起,例如

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);
    }
}