0

所以,正如标题所说,我希望我的 SplitPane 中的两个 ScrollPanes 填充 ButtonPanel 下的剩余空间。我已经尝试了其他几个 LayoutManager,但是在这些情况下,这个是“最好的”。

我在北部、东部、南部和西部设置了约束,所以我真的不明白为什么不尊重南部约束。

我也会使用一些其他 Layoutmanager,例如 BorderLayout,但这似乎也不起作用,因为 JScrollPane 在调整大小时会增长。

public class TestGui extends JSplitPane implements ActionListener {

    private static final long serialVersionUID = 7494479633065399119L;

    private JPanel ingredientsPanel;
    private SpringLayout ingredientsLayout;
    private DefaultListModel<String> ingredientsListModel;
    private JList<String> ingredientsList;
    private JScrollPane ingredientsScrollPane;
    private JPanel ingredientsButtonPanel;
    private JButton addIngredientButton;
    private JButton editIngredientButton;
    private JButton removeIngredientButton;

    private JPanel spicePanel;
    private SpringLayout spiceLayout;
    private DefaultListModel<String> spicesListModel;
    private JList<String> spicesList;
    private JScrollPane spicesScrollPane;
    private JPanel spiceButtonPanel;
    private JButton addSpiceButton;
    private JButton editSpiceButton;
    private JButton removeSpiceButton;

    public TestGui() {
        super(JSplitPane.VERTICAL_SPLIT, true);
        this.setMinimumSize(new Dimension(500, 600));
        this.setPreferredSize(new Dimension(500, 600));

        this.init();
        this.display();
    }

    private void init() {
        this.ingredientsLayout = new SpringLayout();
        this.ingredientsPanel = new JPanel(this.ingredientsLayout);
        this.ingredientsPanel.setMinimumSize(new Dimension(0, 200));
        this.ingredientsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK, 2), "Zutaten"));
        this.ingredientsListModel = new DefaultListModel<>();
        this.ingredientsList = new JList<>(this.ingredientsListModel);
        this.ingredientsScrollPane = new JScrollPane(this.ingredientsList);
        this.ingredientsButtonPanel = new JPanel();
        this.addIngredientButton = new JButton("Hinzufügen");
        this.addIngredientButton.addActionListener(this);
        this.addIngredientButton.setActionCommand("addIngredient");
        this.editIngredientButton = new JButton("Bearbeiten");
        this.editIngredientButton.addActionListener(this);
        this.editIngredientButton.setActionCommand("editIngredient");
        this.removeIngredientButton = new JButton("Löschen");
        this.removeIngredientButton.addActionListener(this);
        this.removeIngredientButton.setActionCommand("deleteIngredient");

        this.spiceLayout = new SpringLayout();
        this.spicePanel = new JPanel(this.spiceLayout);
        this.spicePanel.setMinimumSize(new Dimension(0, 200));
        this.spicePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK, 2), "Gewürze"));
        this.spicesListModel = new DefaultListModel<>();
        this.spicesList = new JList<>(this.spicesListModel);
        this.spicesScrollPane = new JScrollPane(this.spicesList);
        this.spiceButtonPanel = new JPanel();
        this.addSpiceButton = new JButton("Hinzufügen");
        this.addSpiceButton.addActionListener(this);
        this.addSpiceButton.setActionCommand("addSpice");
        this.editSpiceButton = new JButton("Bearbeiten");
        this.editSpiceButton.addActionListener(this);
        this.editSpiceButton.setActionCommand("editSpice");
        this.removeSpiceButton = new JButton("Löschen");
        this.removeSpiceButton.addActionListener(this);
        this.removeSpiceButton.setActionCommand("deleteSpice");
    }

    private void display() {

        this.ingredientsButtonPanel.add(this.addIngredientButton);
        this.ingredientsButtonPanel.add(this.editIngredientButton);
        this.ingredientsButtonPanel.add(this.removeIngredientButton);

        this.ingredientsLayout.putConstraint(SpringLayout.NORTH, this.ingredientsScrollPane, 5, SpringLayout.NORTH, this.ingredientsPanel);
        this.ingredientsLayout.putConstraint(SpringLayout.WEST, this.ingredientsScrollPane, 5, SpringLayout.WEST, this.ingredientsPanel);
        this.ingredientsLayout.putConstraint(SpringLayout.EAST, this.ingredientsPanel, 5, SpringLayout.EAST, this.ingredientsScrollPane);

        this.ingredientsLayout.putConstraint(SpringLayout.NORTH, this.ingredientsButtonPanel, 5, SpringLayout.SOUTH, this.ingredientsScrollPane);
        this.ingredientsLayout.putConstraint(SpringLayout.SOUTH, this.ingredientsPanel, 5, SpringLayout.SOUTH, this.ingredientsButtonPanel);

        this.ingredientsPanel.add(this.ingredientsScrollPane);
        this.ingredientsPanel.add(this.ingredientsButtonPanel);

        this.setTopComponent(this.ingredientsPanel);

        this.spiceButtonPanel.add(this.addSpiceButton);
        this.spiceButtonPanel.add(this.editSpiceButton);
        this.spiceButtonPanel.add(this.removeSpiceButton);

        this.spiceLayout.putConstraint(SpringLayout.NORTH, this.spicesScrollPane, 5, SpringLayout.NORTH, this.spicePanel);
        this.spiceLayout.putConstraint(SpringLayout.WEST, this.spicesScrollPane, 5, SpringLayout.WEST, this.spicePanel);
        this.spiceLayout.putConstraint(SpringLayout.EAST, this.spicePanel, 5, SpringLayout.EAST, this.spicesScrollPane);

        this.spiceLayout.putConstraint(SpringLayout.NORTH, this.spiceButtonPanel, 5, SpringLayout.SOUTH, this.spicesScrollPane);
        this.spiceLayout.putConstraint(SpringLayout.SOUTH, this.spicePanel, 5, SpringLayout.SOUTH, this.spiceButtonPanel);

        this.spicePanel.add(this.spicesScrollPane);
        this.spicePanel.add(this.spiceButtonPanel);

        this.setBottomComponent(this.spicePanel);

    }

    public JPanel getIngredientsPanel() {
        return this.ingredientsPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
        case "addIngredient":
            break;
        case "editIngredient":
            break;
        case "deleteIngredient":
            break;
        case "addSpice":
            break;
        case "editSpice":
            break;
        case "deleteSpice":
            break;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Test");
            frame.getContentPane()
                    .add(new TestGui());
            frame.setDefaultCloseOperation(3);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

}
4

0 回答 0