在 JPanel 上绘制矩形

发布于 2024-12-08 23:05:29 字数 998 浏览 0 评论 0原文

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

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

panel1.add(new DrawRectPanel());

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

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

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

编辑 : 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);
    }
 }

仅在屏幕上打印标签 1

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.

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 :

panel1.add(new DrawRectPanel());

but nothing appeared on panel1
then I tried, just as a test to the class 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.

EDIT :
code for 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金兰素衣 2024-12-15 23:05:29

仍然不知道,

例如

在此处输入图像描述

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

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);
    }
}
疾风者 2024-12-15 23:05:29

而不是添加,

contentPane.add(new DrawRectPanel());

你应该这样做

contentPane.add(panel1);

,因为你已经在 panel1 中有了新的 DrawRectPanel 。但在您的代码中,您将在 contentPane 中添加另一个 DrawRectPanel 实例。并且从未在任何容器中添加panel1

instead of adding

contentPane.add(new DrawRectPanel());

you should do

contentPane.add(panel1);

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

梦情居士 2024-12-15 23:05:29

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文