嗨,我正在尝试创建一个布局如下图的窗口:
我的想法是使用类似的面板和嵌套布局:
到目前为止,这是我的代码,但它无法正常工作。我该怎么做,我该怎么做不同,甚至我的基本概念是错误的?
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();
}
}
(People 是一个自定义类,主要我只是调用 DetailWindow 构造函数)
我在这里先向您的帮助表示感谢!