java - 在 JPanel 内绘制 JComponent

标签 java swing graphics2d jcomponent null-layout-manager

我正在尝试在 JPanel 内显示 JComponent。 我使用空布局,因为组件的位置可以在运行时更改,并且我必须控制它们。

但是下面的代码不起作用。如果我显式调用“paintComponent”方法,JComponent 仅在显示时变得可见,我认为这不是一个好的做法。

我的 JComponent 类

public class MyIcon extends JComponent 
{
    private double xPos;
    private double yPos;
    private double radius = 30;

    public MyIcon(double xPos, double yPos)
    {
            this.xPos = xPos;
            this.yPos = yPos;
            this.setBounds((int)xPos, (int)yPos, (int)radius, (int)radius);
            this.setPreferredSize(new Dimension((int)radius, (int)radius ) );
    }

    public Dimension getPreferredSize()
    {
            return ( new Dimension( (int)radius, (int)radius ) );
    }

    public Dimension getMinimumSize() 
    {
            return new Dimension((int)radius, (int)radius);
    }


    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.BLACK);
            g2d.drawOval( (int)xPos, (int)yPos, (int)radius, (int)radius);

            g2d.fillOval((int)xPos, (int)yPos, (int)radius, (int)radius);

            System.out.println("Icon.paintComponnet() called");
    }
}

我的面板类

public class MyPanel extends JPanel
{
    private MyIcon myIcon;
    private Graphics2D graphics;
    private int width = 700;
    private int height = 500;

    public MyPanel()
    {
        myIcon = new MyIcon(20,30);
        init();
    }

    private void init()
    {
        setLayout(null);
        setBackground(Color.WHITE);
        setPreferredSize( new Dimension(width, height) );
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        add( myIcon );
        myIcon.repaint();       
    }

    public void paintComponent(Graphics g)
    {
        graphics = (Graphics2D) g;
        super.paintComponent(g);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        System.out.println("MyPanel.paintComponnet() called");

        // why my Icon only gets painted if I explicitly call for it ?

        //myIcon.paintComponent(g);
    }
}

我的框架类

public class Editor {

    public static void main(String[] args)
    {
        Editor editor = new Editor();
    }

    private MyPanel myPanel;
    private JFrame frame;
    private JToolBar toolBar;

    public Editor()
    {
        myPanel = new MyPanel();
        init();
    }

    private void init()
    {
        frame = new JFrame("Editor");
        Container content = frame.getContentPane();
        frame.setSize(800, 600);
        frame.setLayout(new BorderLayout(15,15));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        content.add(myPanel,BorderLayout.WEST);

        frame.pack();
        frame.setVisible(true);
     }
}

最佳答案

关于java - 在 JPanel 内绘制 JComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12873056/

相关文章:

java - JUnit 测试 JTable 单元格工具提示

Java 无法在 JPanel 上看到图形

java - 你能帮忙解决 Java 中的正则表达式问题吗?

java - 在 java 中使用 Keylistener 的 CardLayout 问题

java - 生产者消费者问题

java - JTextArea问题

java - 为什么保存的图片这么小?

java - paintComponent 每次我想要它时都不会被调用

java - Spring IOC Bootstrap是否可能

java - JPA - 将 @Version 保存为数据类型 long