简体   繁体   中英

Can't see child JPanel in JPanel in JFrame

I've created a JFrame . Inside this JFrame , I've created a JPanel . Inside this JPanel I've created another JPanel (lets call it "A").

I've drawn in "A" a rectangle, and wanted to create buttons using graphics. There is no rectangle in my gui. I could see that the paintComponent() method inside "A" is not being invoked.

Code: The JPanels: (the child JPanel is inner class)

public class MemoryPanel extends JPanel {
    
    public MemoryPanel(){
        setPreferredSize(new Dimension(350,448));
    }
    
    @Override 
    public void paintComponent(Graphics g) {    
        //POSITIONING
        setLayout(new BorderLayout());
        
        //CREATE MEMORY BUTTONS
        MemButton a=new MemButton();
        
        //Drawing Rectangles for Memory
        add(a,BorderLayout.CENTER);

    } 
    

    
    private class MemoryButton extends JPanel{
        public MemoryButton(){
            setLayout(null);
            setPreferredSize(new Dimension(87,40));
        }
        
        @Override
        public void paintComponent(Graphics g){
            Graphics2D td= (Graphics2D)g;
            td.drawRect(0, 0, 20, 20);
        }
    }
}

You should program the JButtons first in order for your graphics to work as buttons. I belive this post will help you with that:

Creating a custom button in Java

I you want a rectangle to be the background for your buttons you can draw it in your main panel and add the buttons on it. Try using different Layouts to mantain some order.

I've made a simple GUI to test your code and the rectangle appears correctly. I made no relevant changes in the code that you posted.

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

public class SimpleJFrameProgram extends JFrame {
     private static final long serialVersionUID = 1L;

     public SimpleJFrameProgram() {
         super("TEST");

         initComponents();

         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.pack();
         this.setLocationRelativeTo(null);
         this.setVisible(true);
     }


    private void initComponents() {
        MemoryPanel memoryPanel = new MemoryPanel();

        this.add(memoryPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    new SimpleJFrameProgram();
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
       });
    }
 }

I've applyed minor changes to your MemoryPanel : replaced MemButton by your MemoryButton and fill the rectangle in red to improve its visibility for the test. Without this last change, the rectangle appears too.

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

  import javax.swing.JPanel;

  public class MemoryPanel extends JPanel {

    public MemoryPanel(){
         setPreferredSize(new Dimension(350,448));
    }

    @Override
    public void paintComponent(Graphics g) {
        // POSITIONING
        setLayout(new BorderLayout());

        // CREATE MEMORY BUTTONS
        MemoryButton a = new MemoryButton();

        // Drawing Rectangles for Memory
        add(a,BorderLayout.CENTER);

    }

    private class MemoryButton extends JPanel{
        public MemoryButton(){
            setLayout(null);
            setPreferredSize(new Dimension(87,40));
        }

        @Override
        public void paintComponent(Graphics g) {

            Graphics2D td = (Graphics2D) g;
            td.setColor(Color.red);
            td.fillRect(0, 0, 20, 20);
        }
    }
 }

This is the obtained result:

在此处输入图片说明

Maybe your problem is located on initializing the parent JFrame .

Changing the class name of MemoryButton fixed it.

I had another package with the same class name.

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