为什么这个图形组件不起作用?

时间:2014-05-17 23:00:36

标签: java paintcomponent

我正在尝试在“Graphics”类中绘制一个矩形,但由于某种原因矩形没有出现,但程序没有返回任何错误。我从来没有经历过这样的问题,所以我很困惑。

main()的

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Main
{

    public Main()
    {
        JFrame window = new JFrame();

        Sound soundCall = new Sound();
        Graphics graphicsCall = new Graphics();

        final JPanel container = new JPanel();

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(container);
        window.setSize(600, 400);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        window.setResizable(false);

    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Main();
            }
        });

    }

图形()

import java.awt.Color;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class Graphics extends JPanel
{

    public void paintComponent(java.awt.Graphics g)
    { 
        super.paintComponent(g);
        g.setColor(Color.GRAY);
        g.drawRect(500, 500, 500, 500);
    }

}

编辑HOVERCRAFT

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Main
{

    public Main()
    {
        JFrame window = new JFrame();

        Sound soundCall = new Sound();
        Draw drawCall = new Draw();

        final JPanel container = new JPanel();

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(drawCall);
        window.setSize(600, 400);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        window.setResizable(false);

    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Main();
            }
        });

    }

}

通过添加此window.getContentPane().add(drawCall);,我要求将drawCall更改为组件

编辑2:

public class Draw 
{

    public class Graphics extends JPanel
    {

        public void paintComponent(java.awt.Graphics g)
        { 
            super.paintComponent(g);
            g.setColor(Color.GRAY);
            g.drawRect(0, 0, 500, 500);
        }

    }


}

ERROR The method add(Component) in the type Container is not applicable for the arguments (Draw)

1 个答案:

答案 0 :(得分:2)

您将graphicsCall变量添加到任何内容,因此不会显示。解决方案:将其添加到容器(例如您刚刚创建的JPanel),或者直接添加到JFrame的contentPane。

即改变这个:

    JFrame window = new JFrame();

    Sound soundCall = new Sound();
    Graphics graphicsCall = new Graphics();

    final JPanel container = new JPanel();

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().add(container);

到此:

    JFrame window = new JFrame();

    Sound soundCall = new Sound();
    Graphics graphicsCall = new Graphics();

    // final JPanel container = new JPanel();

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().add(soundCall);

顺便说一下,你需要将这个类从Graphics重命名为其他东西,否则你可能会让自己或编译器感到困惑,因为已经存在一个具有该名称的关键Java类。

另外,请避免使用setSize(...)。最好让您的绘图JPanel覆盖getPreferredSize()并在JFrame上调用pack()


修改
根据MadProgrammer精明的观察,你正在超出组件的界限。


编辑2
关于您的最新代码,请:

public class Draw 
{
    public class Graphics extends JPanel
    {
        public void paintComponent(java.awt.Graphics g)
        { 
            super.paintComponent(g);
            g.setColor(Color.GRAY);
            g.drawRect(0, 0, 500, 500);
        }
    }
}

是无用的残骸。你为什么不必要地把一个类包装在一个类中?相反,为什么不简单:

public class Draw extends JPanel   {

    public void paintComponent(java.awt.Graphics g)
    { 
        super.paintComponent(g);
        g.setColor(Color.GRAY);
        g.drawRect(0, 0, 500, 500);
    }

    @Override
    public Dimension getPreferredSize() {
       // return an appropriate Dimension here
    }
}