我在 Eclipse Indigo 中使用 WindowBuilder 来处理不同的布局。基本上,我只是想彻底了解每种方法的工作原理以及哪些方法最适合不同的场景。下面的代码有效,但是我想调整一些东西,但我不确定如何获得我想要的效果。
public class Dashboard extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Dashboard frame = new Dashboard();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Dashboard() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 602, 372);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel drawingPanel = new JPanel();
drawingPanel.setBounds(6, 6, 487, 251);
contentPane.add(drawingPanel);
drawingPanel.setLayout(null);
JScrollPane propertiesScrollPane = new JScrollPane();
propertiesScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
propertiesScrollPane.setBounds(6, 264, 487, 80);
contentPane.add(propertiesScrollPane);
JPanel selectionPanel = new JPanel();
propertiesScrollPane.setViewportView(selectionPanel);
selectionPanel.setLayout(new BoxLayout(selectionPanel, BoxLayout.X_AXIS));
JPanel surfacesPanel = new JPanel();
selectionPanel.add(surfacesPanel);
surfacesPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
surfacesPanel.setLayout(new BoxLayout(surfacesPanel, BoxLayout.Y_AXIS));
JCheckBox surfaceCheck = new JCheckBox("Visible");
surfaceCheck.setHorizontalAlignment(SwingConstants.CENTER);
surfacesPanel.add(surfaceCheck);
JButton surfaceButton = new JButton("Surfaces");
surfacesPanel.add(surfaceButton);
JPanel spacesPanel = new JPanel();
selectionPanel.add(spacesPanel);
spacesPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
spacesPanel.setLayout(new BoxLayout(spacesPanel, BoxLayout.Y_AXIS));
JCheckBox spacesCheck = new JCheckBox("Visible");
spacesCheck.setHorizontalAlignment(SwingConstants.CENTER);
spacesPanel.add(spacesCheck);
JButton spacesButton = new JButton("Spaces");
spacesPanel.add(spacesButton);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_1.setBounds(504, 6, 92, 251);
contentPane.add(scrollPane_1);
JLabel logoLabel = new JLabel("Logo Here");
LogoLabel.setHorizontalAlignment(SwingConstants.CENTER);
LogoLabel.setBounds(505, 264, 91, 80);
contentPane.add(logoLabel);
}
}
具体来说:
我希望 selectionPanel 锚定在左下角,propertiesPanel 锚定在右上角,logoLabel 锚定在右下角,drawingPanel 锚定在左上角。这个想法是,当主窗体调整大小时,滚动面板和标签应该准确地保持在它们相对于父窗体边缘的位置,而绘图面板应该水平和垂直调整大小,以便它占用所有剩余空间顶部框架的大小无关紧要。徽标不应在任何维度上调整大小,而 selectionPanel 应在 x 维度上调整大小,属性窗口应在 y 维度上调整大小。最后,我希望表面和空间面板内的按钮占据面板内的最大空间。也就是说,