【问题标题】:Drawing a JComponent inside a JPanel在 JPanel 中绘制 JComponent
【发布时间】:2012-10-13 12:22:32
【问题描述】:

我试图在 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);
     }
}

【问题讨论】:

  • public class Icon extends JComponent,不要使用reserved Java words, JComponents namens or API names作为自定义类名,例如使用MyIcon
  • 另见Initial Threads
  • 另见这个非常简单的图标编辑视图:Grid

标签: java swing graphics2d jcomponent null-layout-manager


【解决方案1】:
【解决方案2】:

也许一个好的方法是 Icon 类不会扩展 JComponent 而只是一个简单的对象。

然后,您将当前的Icon.paintComponent 重命名为drawIcon 之类的名称,并直接从MyPanel.paintComponent 调用drawIcon 方法并将引用传递给Graphics 对象。

这样您就不必担心使用空布局,并且只需使用Graphics(2d) API 即可控制显示图标的位置。

【讨论】:

  • 这可以解决这个问题,但不会回答我为什么我的代码不能正常工作。
猜你喜欢
  • 2013-03-28
  • 2012-09-27
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多