如何“绘画”在 JPanel 上的 JLabels 上?

发布于 2025-01-06 22:54:10 字数 650 浏览 2 评论 0原文

我有一组 JLabel,每个 JLabel 都包含一个字母(通过 seText()),不透明,背景设置为白色,位于带有 GridLayout< 的 JPanel 上/code> 因此标签形成了一个表格。 我正在做一个简单的动画,突出显示某些行和列,然后显示交叉点。我可以使用标签的 setBackground() 来达到此目的,但我认为如果能够使用 Graphics 对象(也许绘制一个绕路口转一圈,然后清除它)。 我尝试扩展 JLabel,或直接在 JPanel 上绘图(在方法中使用 getGraphics()),但它不起作用,我在这种情况下,认为绘图位于标签后面。我不知道在这两种情况下“绘画”代码应该放在哪里,屏幕上什么也没有出现。

简而言之,像下面这样的方法可以用来在标签上绘图吗?
它应该是 JLabel 还是 JPanel 方法?

public void drawsomething() {
    Graphics2D g2d = (Graphics2D) getGraphics();
    g2d.fillRect(100, 100, 100, 100);
    }

I have a set of JLabels, each containing a letter (via seText()), opaque and background set to white, on a JPanel with a GridLayout so the labels are forming a table.
I am doing a simple animation of highlighting certain rows and columns then there intersection. I can use the setBackground() of labels for this purpose, but thought I'd have more "choices" if a was able to use a Graphics object (maybe drawing a circle around intersection, then clearing it).
I tried to extend JLabel, or drawing on the JPanel directly(using getGraphics() in a method) but it didn't work, I think the drawing is behind the labels in this case. I can't figure out where should the "painting" code be placed in either case, nothing appeared on the screen.

in short, a method like the following, can be used to draw on top of labels?
should it be a JLabel or a JPanel method?

public void drawsomething() {
    Graphics2D g2d = (Graphics2D) getGraphics();
    g2d.fillRect(100, 100, 100, 100);
    }

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

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

发布评论

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

评论(4

霓裳挽歌倾城醉 2025-01-13 22:54:10

如果您重写paintChildren()会怎样?

protected void paintChildren(Graphics g) {
  super.paintChildren(g);
//paint your lines here
}

What if you override paintChildren() ?

protected void paintChildren(Graphics g) {
  super.paintChildren(g);
//paint your lines here
}
青春如此纠结 2025-01-13 22:54:10

您可能想尝试使用 JLayeredPane 在现有 JComponent 之上绘制特定绘图,

请参阅此处的示例 http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

You might want to try a JLayeredPane to paint your specific drawings on top of the existing JComponents

see example here http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

宫墨修音 2025-01-13 22:54:10

我真的对绘图还不太了解,但只是创建了一个小示例代码供您查看,希望您能从中获得一些信息。为了在JLabel上绘画,您可以使用它的paintComponent(Graphics g)方法。

示例代码:

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

public class DrawingOnJLabel extends JFrame
{
    private CustomLabel label;
    private int flag = 1;
    private JPanel contentPane;

    public DrawingOnJLabel()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        label = new CustomLabel(200, 200);
        label.setLabelText("A");
        label.setValues(50, 50, 100, 100, 240, 60);

        final JButton button = new JButton("CLEAR");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        if (flag == 1)              
                        {
                            label.setFlag(flag);
                            flag = 0;
                            button.setText("REPAINT");
                            contentPane.revalidate();
                            contentPane.repaint();
                        }   
                        else if (flag == 0) 
                        {
                            label.setFlag(flag);
                            flag = 1;
                            button.setText("CLEAR");
                            contentPane.revalidate();
                            contentPane.repaint();
                        }
                    }
                });
            }
        });     

        contentPane.add(label);

        add(contentPane, BorderLayout.CENTER);
        add(button, BorderLayout.PAGE_END);
        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new DrawingOnJLabel();
            }
        });
    }
}

class CustomLabel extends JLabel
{
    private int sizeX;
    private int sizeY;
    private int x, y, width, height, startAngle, arcAngle;
    private int flag = 0;
    private String text;

    public CustomLabel(int sX, int sY)
    {
        sizeX = sX;
        sizeY = sY; 
    }

    // Simply call this or any set method to paint on JLabel.
    public void setValues(int x, int y, int width, int height, int startAngle, int arcAngle)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.startAngle = startAngle;
        this.arcAngle = arcAngle;
        repaint();
    }

    public void setFlag(int value)
    {
        flag = value;
        repaint();
    }

    public Dimension getPreferredSize()
    {
        return (new Dimension(sizeX, sizeY));
    }

    public void setLabelText(String text)
    {
        super.setText(text);
        this.text = text;
        flag = 0;
        repaint();
    }

    public void paintComponent(Graphics g)
    {
        if (flag == 0)
        {
            g.setColor(Color.RED);          
            g.drawString(text, 20, 20);
            g.setColor(Color.BLUE);
            g.drawOval(x, y, width, height);
            g.fillOval(x + 20, y + 20, 15, 15);
            g.fillOval(x + 65, y + 20, 15, 15);
            g.fillRect(x + 40, y + 40, 5, 20);
            g.drawArc(x + 20, y + 30, 55, 55, startAngle, arcAngle);            
        }
        else if (flag == 1)
        {
            g.clearRect(x, y, width, height);
        }
    }
}

I really don't know much about drawing stuff yet, but just created one small sample code for you to look at, hopefully you can get some information out of it. In order to paint on the JLabel you can use it's paintComponent(Graphics g) method.

A Sample Code :

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

public class DrawingOnJLabel extends JFrame
{
    private CustomLabel label;
    private int flag = 1;
    private JPanel contentPane;

    public DrawingOnJLabel()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        label = new CustomLabel(200, 200);
        label.setLabelText("A");
        label.setValues(50, 50, 100, 100, 240, 60);

        final JButton button = new JButton("CLEAR");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        if (flag == 1)              
                        {
                            label.setFlag(flag);
                            flag = 0;
                            button.setText("REPAINT");
                            contentPane.revalidate();
                            contentPane.repaint();
                        }   
                        else if (flag == 0) 
                        {
                            label.setFlag(flag);
                            flag = 1;
                            button.setText("CLEAR");
                            contentPane.revalidate();
                            contentPane.repaint();
                        }
                    }
                });
            }
        });     

        contentPane.add(label);

        add(contentPane, BorderLayout.CENTER);
        add(button, BorderLayout.PAGE_END);
        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new DrawingOnJLabel();
            }
        });
    }
}

class CustomLabel extends JLabel
{
    private int sizeX;
    private int sizeY;
    private int x, y, width, height, startAngle, arcAngle;
    private int flag = 0;
    private String text;

    public CustomLabel(int sX, int sY)
    {
        sizeX = sX;
        sizeY = sY; 
    }

    // Simply call this or any set method to paint on JLabel.
    public void setValues(int x, int y, int width, int height, int startAngle, int arcAngle)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.startAngle = startAngle;
        this.arcAngle = arcAngle;
        repaint();
    }

    public void setFlag(int value)
    {
        flag = value;
        repaint();
    }

    public Dimension getPreferredSize()
    {
        return (new Dimension(sizeX, sizeY));
    }

    public void setLabelText(String text)
    {
        super.setText(text);
        this.text = text;
        flag = 0;
        repaint();
    }

    public void paintComponent(Graphics g)
    {
        if (flag == 0)
        {
            g.setColor(Color.RED);          
            g.drawString(text, 20, 20);
            g.setColor(Color.BLUE);
            g.drawOval(x, y, width, height);
            g.fillOval(x + 20, y + 20, 15, 15);
            g.fillOval(x + 65, y + 20, 15, 15);
            g.fillRect(x + 40, y + 40, 5, 20);
            g.drawArc(x + 20, y + 30, 55, 55, startAngle, arcAngle);            
        }
        else if (flag == 1)
        {
            g.clearRect(x, y, width, height);
        }
    }
}
峩卟喜欢 2025-01-13 22:54:10

使用paintComponent(Graphics g) 而不是paint(Graphics g)。这将覆盖 GUI

Use paintComponent(Graphics g) instead of paint(Graphics g). That will paint over the GUI

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