为什么fillRect命令不会显示在我的JFrame上?

Bob*_*ard 1 java swing jpanel

所以我试图通过创建一个对象并将其添加到JFrame来单击鼠标时绘制一个矩形.但是一旦命令运行它就不会出现.有什么想法吗?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui3 extends JFrame {
    private JPanel mousepanel;
    private JLabel statusbar;

    public Gui3(){
        super("The title");

        mousepanel = new JPanel();
        mousepanel.setBackground(Color.WHITE);
        add(mousepanel, BorderLayout.CENTER);

        statusbar = new JLabel("Default");
        add(statusbar, BorderLayout.SOUTH);

        HandlerClass handler = new HandlerClass();
        mousepanel.addMouseListener(handler);
        mousepanel.addMouseMotionListener(handler);
    }   
    private class HandlerClass implements MouseListener, MouseMotionListener
        {
Run Code Online (Sandbox Code Playgroud)

这就是问题出现的地方.程序及其所有方法都有效,它只是绘制了问题的矩形.绘制形状的对象如下.

public void mouseClicked(MouseEvent event) {
        statusbar.setText(String.format("Clicked at %d,%d",event.getX(),event.getY()));
                DrawShapes shapes = new DrawShapes();
                add(shapes);
            }
            public void mousePressed(MouseEvent event){
                statusbar.setText("You pressed down the mouse");
            }
            public void mouseReleased(MouseEvent event){
                statusbar.setText("You released the button");
            }
            public void mouseEntered(MouseEvent event){
                statusbar.setText("You entered the area");
                mousepanel.setBackground(Color.RED);
            }
            public void mouseExited(MouseEvent event){
                statusbar.setText("The mouse has left the window");
                mousepanel.setBackground(Color.WHITE);
            }
            //These are mouse motion events
            public void mouseDragged(MouseEvent event){
                statusbar.setText("You are dragging the mouse");
            }
            public void mouseMoved(MouseEvent event){
                statusbar.setText("You are moving the mouse");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是绘制矩形的对象

import java.awt.*;
import javax.swing.*;
public class DrawShapes extends JPanel {
    public void PaintComponent(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(0,0,30,30);
    }
}
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 6

关于

public void PaintComponent(Graphics g){
    g.setColor(Color.BLUE);
    g.fillRect(0,0,30,30);
}
Run Code Online (Sandbox Code Playgroud)

明白

PaintComponent != paintComponent
Run Code Online (Sandbox Code Playgroud)

一定要使用@Override注释让你知道你什么时候或者没有覆盖你认为的方法.

正确的方法看起来像:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);  // don't forget this!
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 30, 30);
}
Run Code Online (Sandbox Code Playgroud)

此外,如果要替换原始JPanel,请使用CardLayout帮助您轻松完成此操作.否则revalidate(),repaint()在交换容器中的组件后,必须确保自己打电话.

例如,

    @Override
    public void mouseClicked(MouseEvent event) {
        statusbar.setText(String.format("Clicked at %d,%d", event.getX(), event.getY()));
        remove(mousepanel);
        DrawShapes shapes = new DrawShapes();
        getContentPane().add(shapes, BorderLayout.CENTER);
        getContentPane().revalidate();
        getContentPane().repaint();
    }
Run Code Online (Sandbox Code Playgroud)

  • (1+)此外,您应该覆盖类的`getPreferredSize()`方法,以便其他布局管理器可以使用此组件.阅读[自定义绘画]上的Swing教程中的部分(http://docs.oracle.com/javase/tutorial/uiswing/painting/step3.html),获取一个工作示例,其中显示了如何完成此操作.保持教程的链接,因为它有关于所有Swing基础知识的信息. (2认同)
  • 我认为每个问题都应该有一个MCVE.每当我看到一个有20多条评论的问题时,我都会避免这个问题,因为OP只是没有花时间听取给出的建议.我们都知道1)应该使用DocumentFilter而不是自定义文档,2)问题在于未发布的代码. (2认同)