简体   繁体   English

如何在 Java Swing 中使用 repaint 方法

[英]How to use the repaint method in Java Swing

I am very new to the Graphics portion of Java.我对 Java 的图形部分很陌生。 I have created a frame and on it I have added a panel whose color has been set to Green.我创建了一个框架,并在其上添加了一个面板,其颜色已设置为绿色。 Now on clicking that panel I want to draw a circle using a test class's object called Mypanel.现在单击该面板,我想使用名为 Mypanel 的测试类对象绘制一个圆圈。 But it does not happen.但它不会发生。 Please guide !请指导!

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

class Mypanel extends JPanel
{
    @Override
    public void paintComponent(Graphics g)
    {
        g.drawOval(15, 15, 5, 5);
    }        
}
public class algo extends javax.swing.JFrame {

    public algo() {
        initComponents();
        jPanel1.setBackground(Color.GREEN);
    }
    Mypanel p = new Mypanel() ;

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        p.repaint();
    }                                    

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new algo().setVisible(true);
            }
        });
    }

}

If I were to guess I would say that I am not supposed to use the repaint method, but I was told that this was to be used.如果我猜我会说我不应该使用 repaint 方法,但有人告诉我要使用它。

That code as supplied would not compile.提供的代码无法编译。 For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example .为了尽快获得更好的帮助,请发布一个最小的、完整的和可验证的示例简短的、自包含的、正确的示例

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

class Mypanel extends JPanel {

    boolean clicked = false;

    Mypanel() {
        setBackground(Color.GREEN);
        MouseListener mouseListener = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                clicked = true;
                repaint();
            }
        };
        this.addMouseListener(mouseListener);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 100);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (clicked) {
            g.drawOval(15, 15, 50, 50);
        }
    }
}

public class algo extends JFrame {

    public algo() {
        initComponents();
        pack();
        //jPanel1.setBackground(Color.GREEN); ?!?
    }

    protected final void initComponents() {
        add(new Mypanel());
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new algo().setVisible(true);
            }
        });
    }
}

There are a few things to correct in your example...在你的例子中有几件事需要纠正......

When you create the frame (ie in the constructor) you'll want to call super().当您创建框架(即在构造函数中)时,您需要调用 super()。 This is the first thing the constructor has to do.这是构造函数必须做的第一件事。 Then, you'll probably want to set an initial width/height, and set the background color of the frame green.然后,您可能需要设置初始宽度/高度,并将框架的背景颜色设置为绿色。

You need to add a mouse listener so that the mouseClicked method is actually called.您需要添加一个鼠标侦听器,以便实际调用 mouseClicked 方法。 Then have it add the 'MyPanel' object to the frame, and call repaint.然后让它将“MyPanel”对象添加到框架中,并调用重绘。

I think that's roughly what you're going for.我认为这大致是你想要的。

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

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