java 在 JPanel 上绘制矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7723591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:07:42  来源:igfitidea点击:

Drawing rectangles on a JPanel

javaswingjpaneldrawgraphics2d

提问by infoSyStem

I have a JScrollPane and on top of it I have a JPanel named 'panel1'. I want some rectangles to be drawn on this JPanel.

我有一个 JScrollPane,最重要的是我有一个名为“panel1”的 JPanel。我想在这个 JPanel 上绘制一些矩形。

I have a class named DrawRectPanel which extends JPanel and does all the drawing stuff. The problem is that, I tried to draw the rectangles on panel1 by writing the following code :

我有一个名为 DrawRectPanel 的类,它扩展了 JPanel 并完成了所有绘图工作。问题是,我尝试通过编写以下代码在 panel1 上绘制矩形:

panel1.add(new DrawRectPanel());

but nothing appeared on panel1 then I tried, just as a test to the class DrawRectPanel :

但是没有出现在 panel1 上然后我尝试了,就像对 DrawRectPanel 类的测试一样:

JFrame frame = new JFrame();
frame.setSize(1000, 500);
Container contentPane =    frame.getContentPane();
contentPane.add(new DrawRectPanel());
frame.show();

This worked, and produced the drawings but on a separate JFrame How can I draw the rectangles on panel1 ? Thanks in advance.

这有效,并在单独的 JFrame 上生成了图纸如何在 panel1 上绘制矩形?提前致谢。

EDIT : code for DrawRectPanel

编辑:DrawRectPanel 的代码

public class DrawRectPanel extends JPanel  {

    DrawRectPanel() {
        Dimension g = new Dimension(400,400);
        this.setPreferredSize(g);
        System.out.println("label 1");
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("label 2");
        g.setColor(Color.red);
        g.fillRect(20, 10, 80, 30);
    }
 }

only label 1 is printed on the screen

屏幕上仅打印标签 1

采纳答案by mKorbel

still no idea,

还是不知道

for example

例如

enter image description here

在此处输入图片说明

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class CustomComponent extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent() {
        setTitle("Custom Component Graphics2D");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponents());
        pack();
        // enforces the minimum size of both frame and component        
        setMinimumSize(getSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        CustomComponent main = new CustomComponent();
        main.display();
    }
}

class CustomComponents extends JComponent {

    private static final long serialVersionUID = 1L;

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

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

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

回答by gtiwari333

instead of adding

而不是添加

contentPane.add(new DrawRectPanel());

you should do

你应该做

contentPane.add(panel1);

Because you already have new DrawRectPanelin panel1. But in your code you are adding another instance of DrawRectPanelin contentPane. And never added panel1in none of your container.

因为您DrawRectPanel在 panel1 中已经有了 new 。但是在您的代码中,您正在添加DrawRectPanelin 的另一个实例contentPane。并且从未添加panel1过您的container.

回答by Funny Geeks

to fix your problem, change "paintComponent" to "paint" when the window repaints automatically, it should work.

要解决您的问题,请在窗口自动重新绘制时将“paintComponent”更改为“paint”,它应该可以工作。