JFrame中的绘制不起作用(Java)

时间:2012-10-30 10:22:20

标签: java swing graphics jframe paint

所以在课堂上我们正在制作一个Java程序。我们正在尝试在JFrame中使用paint(Graphics g)函数。我们在过去(几​​个星期前)尝试过它并且它曾经工作过。但现在它没有(或者我们在某处犯了错误)。我们也尝试使用paintComponent(Graphics g),但似乎没有任何效果。 这是我们的代码:

public class MainAc {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Class Paint");       
        JButton button = new JButton("Click for more");             
        frame.setSize(800, 600);    
        frame.add(button);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.setLayout(null);
        button.setLocation(100,100);
        button.setSize(200,100);
        frame.setVisible(true);     
    }
    public void paint(Graphics g){
        g.drawString("Hello", 200, 50);
    }
}

4 个答案:

答案 0 :(得分:10)

您的课程不会延伸到任何能够绘画的Component

阅读Performing Custom Painting以获取更多想法

您可以执行以下操作:

enter image description here

public class SimplePaint {

    public static void main(String[] args) {
        new SimplePaint();
    }

    public SimplePaint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected class PaintPane extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            String text = "Look ma, no hands";
            FontMetrics fm = g2d.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g2d.drawString(text, x, y);
            g2d.dispose();


        }

    }

}

在可能的情况下,您应该避免覆盖顶级容器的paint方法,如果没有其他原因,它们不会被双重缓冲。

出于同样的原因,你也应该尝试从基于Swing的组件扩展,因为混合重型和轻量级组件会导致绘画问题。

答案 1 :(得分:5)

所以你在那里做的是在你自己的paint课程中实施MainAc方法,而不是JFrame

您的MainAc课程本身应来自JFrame

以下代码应该有效。如果你不了解继承,你应该查看你的课堂笔记,它会在那里。

public class MainAc extends JFrame {

public static void main(String[] args) {
    new MainAc();
}

public MainAc() {
    super("Class Paint");       
    JButton button = new JButton("Click for more");             
    setSize(800, 600);    
    add(button);
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    button.setLayout(null);
    button.setLocation(100,100);
    button.setSize(200,100);
    setVisible(true);
}

public void paint(Graphics g) {
    super.paint(g);
    g.drawString("Hello", 200, 50);
}

}

答案 2 :(得分:4)

正如代码所示,您只是在类中定义paint方法。该类扩展了Object,因此您overridding上不会JComponent绘画。

您的课程必须扩展ComponentComponent本身的任何子类,才能真正覆盖paintpaintComponent。在您的情况下,MainAc会延长JFrame

答案 3 :(得分:-1)

以下是Sticky代码的简化版本,似乎有效。我将其更改为扩展JApplet并使用init()方法而不是main()

public class JavaApplication51 extends JApplet {
    public  void something() {
       JButton button = new JButton("Click for more");             
        add(button);
        setLayout(null);
        button.setLayout(null);
        button.setLocation(100,100);
        button.setSize(101,20);
        setVisible(true); 
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.drawString("Hello", 200, 50);
    }

    public void init() {
        something();
    }
}