【问题标题】:Blank JPanel even when I draw on it即使我在上面画画也空白的JPanel
【发布时间】:2013-12-17 13:14:05
【问题描述】:

从我前几天开始编写的一个简单程序继续,我有一个新的查询。我希望程序在面板中间画一个黑色圆圈。稍后我将使用按钮来移动圆圈,但我还没有在那个阶段。程序运行没有错误,我得到一个白色面板,下面有我的按钮,但白色面板中间没有黑色圆圈。我搜索了一些以前推荐使用paintComponent 的帖子,我已经这样做了,但是我遗漏了一些东西,因为它没有按我的预期工作,并且repaint() 也不起作用。感谢您收到任何提示。

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MovingArrows extends JFrame implements ActionListener {

    private JButton buttonUp, buttonDown, buttonLeft, buttonRight;
    private JPanel panel;
    private int xCircleCentre, yCircleCentre;

    final int xCircleCentreStarting = 250, yCircleCentreStarting = 250;
    final int RADIUS = 20;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        MovingArrows frame = new MovingArrows();
        frame.setSize(550, 600);

        frame.setVisible(true);
        frame.createGUI();
        // frame.repaint();

    }

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout());

        panel = new JPanel();
        panel.setPreferredSize(new Dimension(500, 500));
        panel.setBackground(Color.white);

        window.add(panel);

        buttonUp = new JButton("Up");
        buttonDown = new JButton("Down");
        buttonLeft = new JButton("Left");
        buttonRight = new JButton("Right");
        window.add(buttonUp);
        window.add(buttonDown);
        window.add(buttonLeft);
        window.add(buttonRight);
        buttonUp.addActionListener(this);
        buttonDown.addActionListener(this);
        buttonLeft.addActionListener(this);
        buttonRight.addActionListener(this);
        // panel.repaint();

        Graphics paper = panel.getGraphics();

        paintComponent(paper);

    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.black);
        g.fillOval(xCircleCentreStarting - RADIUS, yCircleCentreStarting
                - RADIUS, RADIUS * 2, RADIUS * 2);

    }

    @Override
    public void actionPerformed(ActionEvent event) {

        Graphics paper = panel.getGraphics();
        paper.setColor(Color.black);
        paper.fillOval(xCircleCentreStarting - RADIUS, yCircleCentreStarting
                - RADIUS, RADIUS * 2, RADIUS * 2);

    }
}

【问题讨论】:

    标签: java swing drawing jpanel paintcomponent


    【解决方案1】:

    1) 你尝试在没有paintComponent() 的JFrame 上绘画,这是错误的。例如,您需要在 JPanel 的覆盖 paintComponent() 方法中进行自定义绘画。阅读有关custom paintings 的更多信息。

    2)在你的actionPerformed()方法中,重画椭圆,你只需要调用repaint()方法,就可以了,你不需要自己获取Graphics实例来绘制。

    我已更改您的代码,请检查:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class MovingArrows extends JFrame implements ActionListener {
    
        private JButton buttonUp, buttonDown, buttonLeft, buttonRight;
        private int xCircleCentre, yCircleCentre;
    
        final int xCircleCentreStarting = 250, yCircleCentreStarting = 250;
        final int RADIUS = 20;
    
        public static void main(String[] args) {
    
            MovingArrows  frame = new MovingArrows();
            frame.createGUI();
            frame.pack();
            frame.setVisible(true);
    
        }
    
        private void createGUI() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            JPanel btnPanel = new JPanel();
            Container window = getContentPane();
            DrawHere drawHere = new DrawHere();
            drawHere.setPreferredSize(new Dimension(400,400));
            window.add(drawHere);
            window.add(btnPanel, BorderLayout.SOUTH);
            buttonUp = new JButton("Up");
            buttonDown = new JButton("Down");
            buttonLeft = new JButton("Left");
            buttonRight = new JButton("Right");
            btnPanel.add(buttonUp);
            btnPanel.add(buttonDown);
            btnPanel.add(buttonLeft);
            btnPanel.add(buttonRight);
            buttonUp.addActionListener(this);
            buttonDown.addActionListener(this);
            buttonLeft.addActionListener(this);
            buttonRight.addActionListener(this);
        }
    
        @Override
        public void actionPerformed(ActionEvent event) {
            repaint();
        }
    
        class DrawHere extends JPanel {
    
            public DrawHere(){
                setBackground(Color.WHITE);
            }
    
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.black);
                g.fillOval(xCircleCentreStarting - RADIUS, yCircleCentreStarting
                        - RADIUS, RADIUS * 2, RADIUS * 2);
    
            }
        }
    }
    

    它看起来像:

    【讨论】:

    • 非常感谢您的回复和代码。我会努力解决它,希望我能理解。我没有看到你给我的链接...我阅读了一些其他指南,但我仍然无法从他们那里找出我的错误。
    • 经历过,我想我明白了。无法直接控制程序何时在面板上绘制真是太奇怪了。我还想请您为我整理按钮并将它们放在面板上 - 我还没有阅读框架布局,这是我很快要做的事情。
    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    相关资源
    最近更新 更多