That Thatson
That Thatson

Reputation: 309

JPanel is not showing up

I copied the examples straight from the book. The code is supposed to draw a couple things on the JFrame, but nothing shows up(other than the JFrame) Here is the class with the main method

import java.awt.BorderLayout;
import javax.swing.JFrame;

public class JavaApplication24 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    JFrame frame = new JFrame("Window");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(480,270);

    frame.setVisible(true);
    NewClass panel = new NewClass();

    frame.add(BorderLayout.CENTER, panel);

    }

Here is a subclass of JPanel

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;


public class NewClass extends JPanel {

    @Override
    public void paintComponent(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(0,0,100,100);

        g.setColor(Color.GREEN);
        g.drawRect(50,50,100,100);

        g.setColor(Color.RED);
        g.drawString("Hello",200,200);

        g.setColor(Color.RED);
        g.fillOval(240,40,100,30);

    }
}

Upvotes: 0

Views: 1304

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Issue #1

Your NewClass should provide sizing hints which the layout manager (BorderLayout in this case) can make decisions about how best to layout your component.

You should also be calling super.paintComponent before doing any custom painting, otherwise you will end up with no end of rendering artifacts appearing

public class NewClass extends JPanel {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, 100, 100);

        g.setColor(Color.GREEN);
        g.drawRect(50, 50, 100, 100);

        g.setColor(Color.RED);
        g.drawString("Hello", 200, 200);

        g.setColor(Color.RED);
        g.fillOval(240, 40, 100, 30);

    }
}

I copied the examples straight from the book

I hoping you just made some minor mistakes, otherwise I'd be concerned about the validity of the book :P

You should also be starting your UI from within the context of the Event Dispatching Thread, this tends to solve issues which can appear on different platforms...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }
            
            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new NewClass());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

See Initial Threads for more details

Issue #2

You should not make the UI visible until AFTER you've established it, instead of

frame.setVisible(true);
NewClass panel = new NewClass();

frame.add(BorderLayout.CENTER, panel);

Use something more like...

NewClass panel = new NewClass();
frame.add(BorderLayout.CENTER, panel);    
frame.setVisible(true);

There are ways you can trigger and update after the frame is made visible, but this is just the simplest fix

Upvotes: 1

Related Questions