java - 即使我在 JPanel 上绘图时也是空白的

标签 java swing drawing jpanel paintcomponent

继续我前几天开始编写的一个简单程序,我有一个新的查询。我希望程序在面板中间画一个黑色圆圈。稍后我将使用按钮来移动圆圈,但我还没有达到那个阶段。程序运行没有错误,我得到一个白色面板,下面有我的按钮,但白色面板中间没有黑色圆圈。我搜索了一些以前推荐使用 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);

    }
}

最佳答案

1) 您尝试在没有 paintComponent() 的 JFrame 上绘画,这是错误的。例如,您需要在 JPanel 的重写 paintComponent() 方法中进行自定义绘画。了解更多关于 custom paintings .

2)在您的 actionPerformed() 方法中,要重绘椭圆,您只需要调用 repaint() 方法即可完成所有操作,您无需获取 图形实例并自己绘制。

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

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

        }
    }
}

它看起来像:

enter image description here

关于java - 即使我在 JPanel 上绘图时也是空白的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20635156/

相关文章:

java - 在哪里运行复杂的算法?服务器端还是客户端?

Java分割嵌套括号字符串

java - 如何在 JTable 上添加图像背景,滚动 JTable 时不滚动

android - 自定义 ProgressBar 成为温度计

sql-server - SVG viewBox 反转 Y 坐标

java - 在java代码中创建隐藏文件夹

java - TableModel,导致 Java JFrame 中出现重复的数据库信息

java - 在 JavaFX 中的 Pane 之间切换

android - 如何在Android上绘制未填充的数字?

java - 我如何通过 ssh 连接到 unix 机器并从 Java tail?