[英]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. 我想制作记忆游戏,我需要在4x5的JPanel的每个单元格中放置两个图像。 For this I created a JPanel 1x2 with two images inside and put it in the JPanel of 4x5.
为此,我创建了一个JPanel 1x2,内部有两个图像,并将其放入4x5的JPanel中。 But the result is:
但结果是:
Result: 结果:
So, if understand correctly, you're problem is, you're not seeing 20 new panels, only one. 所以,如果理解正确,你就是问题,你没有看到20个新面板,只有一个。
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... 而你最终会得到像......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.