简体   繁体   中英

Nothing is getting displayed on JFrame

This what I am doing but is nothing is getting displayed on Jframe window. I have not extended class JFrame to do my, is it necessary to do so for displaying objects on window.

public class testGraphics {
    static JFrame workingFrame = null;

    public static void main(String args[])
    {
        JFrame workingManager = new JFrame("Hello");
        workingManager.setSize(500, 500);
        workingManager.setVisible(true);
        Graphics g = workingManager.getGraphics();
        JPanel jp = (JPanel) workingManager.getContentPane();
        workingManager.paintComponents(g);
        g.fillOval(0, 0, 30, 30);
        g.drawOval(0, 50, 30, 30);
        g.setColor(Color.CYAN);
    }
}

Do not ever call getGraphics() or explicitly call paintXxx() to do custom painting. The correct way to do custom painting is to override the paintComponent method of the panel to paint on. The overriden method will be implicitly called for you. Then add that panel to the frame. Also you should override the getPreferredSize() of the panel, so it has a preferred size, so you can just pack the frame

class PaintPanel extends JPanel {
    @Override
    protected paintComponent(Grapchics g) {
        super.paintComponent(g);
        g.drawString(....);
    }
    @Override
    public Dimension getPreferredSize() {'
        return new Dimension(300, 300);
    }
}

Then add it to the frame (or if you want to set it as the content pane of the frame, do that instead)

PaintPanel panel = new PaintPaint();
frame.add(panel);
...
frame.pack();

See more at Performing Custom Painting

I made several changes to your code to get it to work properly.

  1. I changed your main method to call the SwingUtilities invokeLater method to make sure that the Swing components were defined and used on the Event Dispatch thread .

  2. I created a drawing JPanel. I set the color first, then drew the ovals.

  3. I added a JFrame default close operation. You must specify a default close operation, or else your Java application will continue running after you close the JFrame.

  4. I moved the size to the drawing panel. The frame size will be calculated when you call the JFrame pack method.

And here's the modified code:

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

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

public class TestGraphics implements Runnable{

    private JFrame workingManager;

    private JPanel drawingPanel;

    @Override
    public void run() {
        workingManager = new JFrame("Hello");
        workingManager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        drawingPanel = new DrawingPanel();

        workingManager.add(drawingPanel, BorderLayout.CENTER);
        workingManager.pack();
        workingManager.setLocationByPlatform(true);
        workingManager.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TestGraphics());
    }

    public class DrawingPanel extends JPanel {

        private static final long   serialVersionUID    = 
                -3701718376300985046L;

        public DrawingPanel() {
            this.setPreferredSize(new Dimension(500, 500));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.CYAN);
            g.fillOval(0, 0, 30, 30);
            g.drawOval(0, 50, 30, 30);
        }

    }

}

The setSize() and setVisible() must be at the bottom of the method:

 JFrame workingManager = new JFrame("Hello");
    Graphics g = workingManager.getGraphics();
    JPanel jp = (JPanel) workingManager.getContentPane();
    workingManager.paintComponents(g);
    g.fillOval(0, 0, 30, 30);
    g.drawOval(0, 50, 30, 30);
    g.setColor(Color.CYAN);

    workingManager.setSize(500, 500);
    workingManager.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM