如何制作类似附加图像的布局

时间:2015-03-01 21:40:15

标签: java swing user-interface layout-manager

我正在尝试设计一个包含表单和几个项目的布局。但是我发现将物品放在正确的地方太难了。 在下图中,右边框架是我的目标设计,左侧是我可以制作的。 enter image description here 这是正确框架的代码:

public class GUI extends JFrame{


    public GUI(){
        JFrame frame = new JFrame("frame");
        frame.setSize(600, 600);

        JPanel panel = new JPanel(new BorderLayout());

        panel.add(new JLabel("Title"), BorderLayout.NORTH);

        JPanel formPanel = new JPanel(new GridLayout(1,2));
        panel.add(formPanel);
        TitledBorder formPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
        formPanel.setBorder(formPanelTitle);

        //LEFT PANEL
        JPanel labelsPanel = new JPanel(new GridLayout(4,1));
        TitledBorder labelsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
        labelsPanel.setBorder(labelsPanelTitle);

        labelsPanel.add(new JLabel("Label 1"));
        labelsPanel.add(new JLabel("Label 2"));
        labelsPanel.add(new JLabel("Label 3"));
        labelsPanel.add(new JLabel("Label 4"));

        formPanel.add(labelsPanel);

        //RIGHT PANEL
        JPanel fieldsPanel = new JPanel(new GridLayout(4,1));
        TitledBorder fieldsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
        fieldsPanel.setBorder(fieldsPanelTitle);

        fieldsPanel.add(new JTextField("Label 1"));
        fieldsPanel.add(new JTextField("Label 2"));
        fieldsPanel.add(new JTextField("Label 3"));
        fieldsPanel.add(new JTextField("Label 4"));

        formPanel.add(fieldsPanel);

        //BOTTOM PANEL
        JPanel bottomPanel = new JPanel(new GridLayout(2,1));
        TitledBorder BottomPanelTitle = BorderFactory.createTitledBorder("GridLayout(2,1)");
        bottomPanel.setBorder(BottomPanelTitle);
        panel.add(bottomPanel, BorderLayout.SOUTH);

        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(new JButton("Browse"));
        buttonPanel.add(new JLabel("Label"));
        TitledBorder buttonPanelTitle = BorderFactory.createTitledBorder("FlowLayout()");
        buttonPanel.setBorder(buttonPanelTitle);
        bottomPanel.add(buttonPanel);

        JPanel secondButtonPanel = new JPanel(new GridLayout(1,2));
        secondButtonPanel.add(new JButton("Back"));
        secondButtonPanel.add(new JButton("Next"));
        TitledBorder secondButtonPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
        secondButtonPanel.setBorder(secondButtonPanelTitle);
        bottomPanel.add(secondButtonPanel);

        frame.add(panel);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        // TODO code application logic here
        new GUI();

    }

}

我不确定代码是否真的是最佳的,因为有很多内部面板并且使它太复杂。我也不能把物品放在我想要的地方。是否有任何建议或想法使这种布局看起来更好?

1 个答案:

答案 0 :(得分:3)

  • 使用JPanel创建GridBagLayout并向其添加标签/字段,这将形成" center"部分布局。
  • 创建JPanel并向其添加Browse按钮JLabel。将GridBagConstraints#gridwidth设置为REMAINDER,将其添加到您的第一个面板
  • 使用JPanel创建BorderLayout,将第一个面板添加到CENTER位置。将标题Label添加到NORTH位置,您可能需要调整它horizontalAlignment属性
  • 使用JPanel创建FlowLayout,与RIGHT对齐,然后添加"返回"和"下一步"它的按钮。将其添加到上一个面板的SOUTH位置。

查看Laying Out Components Within a Container了解详情