简体   繁体   English

形状不会显示在我的GUI JFrame上

[英]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 @Override注释用于您认为是替代的任何方法
  • The paintComponent method is protected, not public paintComponent方法是受保护的,不是公共的
  • Call the super's method in your override 在您的替代中调用超级方法
  • Best to override getPreferredSize of the JPanel if you need to fix its size 如果需要修复其大小,最好覆盖JPanel的getPreferredSize
  • Start the Swing GUI on the Swing event thread for thread-safety 为了确保线程安全,在Swing事件线程上启动Swing GUI
  • 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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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