简体   繁体   中英

shapes won't show up on my GUI JFrame

heres what my code looks like

import java.awt.*;
import javax.swing.*;


public class Test {

 public static void main(String[] args) {
    JFrame frame = new JFrame();
    MyDrawPanel shape = new MyDrawPanel();
    frame.getContentPane().add(shape);

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


 }

}
class MyDrawPanel extends JPanel{

 public void paintComponent (Graphics g) {
     g.setColor(Color.ORANGE);
     g.fillRect(20, 50, 100, 100);

 }
}

When I run it, the only thing that shows up is the frame, not the actual shape. Is there something I'm missing?

Note that this answer does not answer your direct question of why your posted code doesn't work, because while your code has problems, it should still display the square. But having said that, this post is meant to offer some suggestions on "better" practices:

  • Avoid magic values and magic numbers
  • Use @Override annotations for any method that you think is an override
  • The paintComponent method is protected, not public
  • Call the super's method in your override
  • Best to override getPreferredSize of the JPanel if you need to fix its size
  • Start the Swing GUI on the Swing event thread for thread-safety
  • Avoid hard-coding your graphic drawing positions, especially if you're thinking of animating it at a later date

This is a better representation of your code:

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

import javax.swing.*;

public class Test2 extends JPanel {
    private static final int PREF_W = 500;
    private static final int PREF_H = PREF_W;
    private static final Color RECT_COLOR = Color.ORANGE;
    private static final int RECT_WIDTH = 100;
    private static final int INIT_X = 20;
    private static final int INIT_Y = 50;
    private int rectX = INIT_X;
    private int rectY = INIT_Y;

    public Test2() {
        // TODO any initialization code goes here
    }

    // override annotation
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // avoid magic values and numbers
        g.setColor(RECT_COLOR);
        g.fillRect(rectX, rectY, RECT_WIDTH, RECT_WIDTH);
    }

    // best way to set size safely
    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }


    private static void createAndShowGui() {
        Test2 mainPanel = new Test2();

        JFrame frame = new JFrame("Test2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // be sure to start the GUI on the event thread
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

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