海蘭,我試圖像在這張圖片創建一個佈局的窗口: layoutJava的嵌套佈局
我的IDE是使用面板和佈局鳥巢這樣的事情: layout2
這裏是我的代碼到目前爲止,但它無法正常工作。我該怎麼做,我應該做什麼不同,甚至我的基本概念是錯誤的?
package layout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
import models.People;
public class DetailWindow extends JFrame{
public DetailWindow(People newPeople) {
JFrame frame = new JFrame("Detail Window");
JPanel mainPanel = new JPanel(new GridLayout(1,2));
frame.add(mainPanel);
JPanel leftPanel = new JPanel(new GridLayout(2,1));
frame.add(leftPanel);
JPanel rightPanel = new JPanel(new GridLayout(3,1));
frame.add(rightPanel);
JPanel pictureHolder = new JPanel(new FlowLayout());
pictureHolder.add(new JLabel(new ImageIcon(newPeople.getPic())));
leftPanel.add(pictureHolder);
JPanel infoHolder = new JPanel(new GridLayout(6,1));
infoHolder.add(new JTextField(newPeople.getLb_name()));
infoHolder.add(new JTextField(newPeople.getName()));
infoHolder.add(new JTextField(newPeople.getLb_occup()));
infoHolder.add(new JTextField(newPeople.getOccup()));
infoHolder.add(new JTextField(newPeople.getLb_BD()));
infoHolder.add(new JTextField(newPeople.getBD()));
leftPanel.add(infoHolder);
rightPanel.add(new JTextField(newPeople.getName()));
rightPanel.add(new JTextField(newPeople.getOccup()));
rightPanel.add(new JTextField(newPeople.getDetail()));
frame.pack();
frame.setSize(600, 400);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.show();
}
}
(人們的是一個自定義類,並在主只需要調用DetailWindow構造函數)
感謝您的幫助提前!