Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 组件在JPanel中不可见_Java_Swing_Graphics_Awt_Paint - Fatal编程技术网

Java 组件在JPanel中不可见

Java 组件在JPanel中不可见,java,swing,graphics,awt,paint,Java,Swing,Graphics,Awt,Paint,我将JPanel放在一个名为Box的JFrame容器中 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(OFFSE

我将JPanel放在一个名为Box的JFrame容器中

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);
}
   ...
}
当我向容器中添加一个带球的盒子时,我只能看到这个盒子。如果我加一个球,我就能看到球


有人知道为什么球添加到长方体时不可见吗?

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

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

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

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

不要混合使用重型和轻型部件。你应该扩展它。 您应该覆盖paintComponent,而不是paint。 球有大小吗?如果尚未为Ball提供尺寸标注,则该尺寸标注将不可见。 不要混合使用重型和轻型部件。你应该扩展它。 您应该覆盖paintComponent,而不是paint。 球有大小吗?如果尚未为Ball提供尺寸标注,则该尺寸标注将不可见。
有三种可能的错误

1/使用JLabel最简单的油漆

2/通过javax.swing.Timer进行计时

3/油漆组件,而非AWT组件的油漆和油漆默认XXXUI

比如说,把它们放在一起

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

有三种可能的错误

1/使用JLabel最简单的油漆

2/通过javax.swing.Timer进行计时

3/油漆组件,而非AWT组件的油漆和油漆默认XXXUI

比如说,把它们放在一起

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

为了更快地获得更好的帮助,请发布一个。为了更快地获得更好的帮助,请发布一个。然后我们需要一个完整的示例,请参见安德鲁评论中的链接。然后我们需要一个完整的示例,请参见安德鲁评论中的链接。球的大小确实为零,不幸的是,告诉我这一点的用户已删除了他们的答案。这就是问题所在,现在已经解决了。@Alex,如果你想接受的话,我取消了我的答案。但不管怎样,我很高兴你的问题得到了解决。球的大小确实是零,不幸的是,告诉我这个的用户删除了他们的答案。这就是问题所在,现在已经解决了。@Alex,如果你想接受的话,我取消了我的答案。但不管怎样,我很高兴你的问题已经解决了。