Paint in JFrame not working (Java)

為{幸葍}努か 提交于 2020-01-14 10:41:40

问题


So in class we are making a Java program. We are trying to use the paint(Graphics g) function in the JFrame. We have tried it in the past (weeks ago) and it used to work. But now it doesnt (or we made a mistake somewhere). We have also tried to use paintComponent(Graphics g) but nothing seems to work. Here is our code:

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

回答1:


So what you're doing there, is implementing a paint method inside your own MainAc class, not the JFrame.

Your MainAc class itself, should derive from the JFrame.

The code below should work. If you don't understand inheritance, you should look over your class notes, it'll be in there.

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:


Your class doesn't extend from any Component capable of painting

Have a read through Performing Custom Painting for more ideas

You could do something like:

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


        }

    }

}

Where possible, you should avoid overriding top level container's paint methods, if for no other reason, they're not double buffered.

You should also try and extend from Swing based components for the same reason, as mixing heavy and lightweight components can cause issues with painting.




回答3:


As the code is, you are just defining a paint method in a class. The class extends Object, so you are not overridding paint on a JComponent.

Your class must extend any subclass of Component or Component itself in order to actually override paint or paintComponent. In your case, MainAc would extend JFrame.




回答4:


Here is a simplified version of Sticky's code that seems to work. I changed it to an extend JApplet and use the init() method instead of the 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();
    }
}


来源:https://stackoverflow.com/questions/13136942/paint-in-jframe-not-working-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!