简体   繁体   中英

JPanel inside JPanel in JAVA

  public static void main(String[] args) {
      JTextField text = new JTextField();
      JFrame frame = new JFrame();
      frame.setLayout(new BorderLayout());      
      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(5, 4));
      JPanel panel2 = new JPanel();
      panel2.setLayout(new FlowLayout());



      JLabel imgLabel1 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abs.jpg"));
      JLabel imgLabel2 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abss.jpg"));


      imgLabel1.setPreferredSize(new Dimension(100,100));
      imgLabel2.setPreferredSize(new Dimension(100,100));

      panel2.add(imgLabel1);
      panel2.add(imgLabel2);


      for(int i=0; i<20; i++){
          panel.add(panel2);
      }


      frame.add(text, BorderLayout.NORTH);
      frame.add(panel, BorderLayout.CENTER);      
      frame.setPreferredSize(new Dimension(1280,700));
      frame.pack();
      frame.setVisible(true);      
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

I want to make a memory game, I need to put two images in each cell of the JPanel of 4x5. For this I created a JPanel 1x2 with two images inside and put it in the JPanel of 4x5. But the result is:

Result:

在此输入图像描述

So, if understand correctly, you're problem is, you're not seeing 20 new panels, only one.

The problem is, a component can only reside in a single container, once, so doing something like...

for (int i = 0; i < 20; i++) {
    panel.add(panel2);
}

is the equivalent of doing something like...

panel.add(panel2);

You actually need to create a new instance of the component on each iteration of the loop

What I would suggest you do is create a "wrapper" or "card" panel which can contain the two images. In my testing I just used coloured panels, but you get the idea...

public class WrapperPane extends JPanel {

    public WrapperPane() {
        setLayout(new FlowLayout());
        add(makePanel(Color.RED));
        add(makePanel(Color.GREEN));
        // This is just for demonstration purposes
        setBorder(new LineBorder(Color.DARK_GRAY));
    }

    protected JPanel makePanel(Color background) {
        JPanel panel = new JPanel();
        panel.setBackground(background);
        panel.setPreferredSize(new Dimension(100, 100));
        return panel;
    }

}

The you'd just have to do something like...

JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));

for (int i = 0; i < 20; i++) {
    panel.add(new WrapperPane());
}

frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// Don't do this, just let the content make it's own
// calculations
//frame.setPreferredSize(new Dimension(1280, 700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

And you'd end up with something like...

漂亮

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