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

Dal*_*lox 4 java graphics swing paint jframe

所以在课堂上我们正在制作一个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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 11

你的课程不会延伸到任何Component有能力的绘画

阅读通过执行自定义绘画阅读更多的想法

你可以这样做:

在此输入图像描述

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();


        }

    }

}
Run Code Online (Sandbox Code Playgroud)

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

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

  • +1表示完整的工作示例和扩展JPanel :) (2认同)

Ric*_*lor 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);
}

}
Run Code Online (Sandbox Code Playgroud)

  • 建议OP从顶级容器延伸并且无法调用super.paint并不是OP的优惠,并且是一种非常糟糕的方法 - IHMO (2认同)