Why are my JPanels not showing color or shapes

后端 未结 2 626
一整个雨季
一整个雨季 2021-01-26 11:04

So whenever I run the application the frame is there however all the colors and rectangles are not. I\'m making 3 different menus each intractable so I need 3 panels within my f

2条回答
  •  灰色年华
    2021-01-26 11:34

    You need to create an instance of your Application class and add it to a JFrame:

    mport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Application extends JPanel{
    
        public static void main(String[] args) {
    
            JFrame frame = new JFrame("FrogVibes");
            JPanel application = new Application();
            frame.add(application);
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawRect(0,700,400,100);
            g.drawRect(0, 600,100,150);
        }
    }
    

    Also, the method must be named paintComponent(), not Graphics(). Now Swing will call paintComponent() when it draws the window.

提交回复
热议问题