我想通过SpringLayouts的组合(一系列相互独立的SpringLayout)来管理容器的复杂结构

特别是,一个由包含JPanels的主SpringLayout管理的JFrame:每个JPanel应该由其自己的SpringLayout管理。

不幸的是,如果我尝试应用两个不同的SpringLayout,框架上什么也不会出现

我编写了这个简单的代码来向您展示问题。该代码包含:


JFrame及其SprilgLayout
JPanel及其SpringLayout
JPanel中的一个简单JButton


这里的代码:

import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class SpringLayout_cobination {

    // THE OBJECTS
    static JFrame frame;// The frame
    static JPanel panel;        // The container
    static JButton Button;      // Object

    // THE LayOuts
    static SpringLayout LayOut_Frame;
    static SpringLayout LayOut_panel;
    static Container contentPane;

    public static void main(String[] args) {

        // Start frame and componets
        frame=new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(830, 420);

        panel=new JPanel();
        Button=new JButton("ok");

        panel.add(Button);
        panel.setSize(new Dimension(100,100));
        frame.add(panel);

        // Set the layouts
        LayOut_Frame=new SpringLayout(); // Frame LayOut
        frame.setLayout(LayOut_Frame);
        contentPane=frame.getContentPane(); // Main Container of the frame

        LayOut_panel=new SpringLayout();
        panel.setLayout(LayOut_panel);

        LayOut_Frame.putConstraint(SpringLayout.NORTH, panel,30,SpringLayout.NORTH, contentPane);
        LayOut_Frame.putConstraint(SpringLayout.WEST, panel,30,SpringLayout.WEST , contentPane);

        LayOut_panel.putConstraint(SpringLayout.NORTH, contentPane,30,SpringLayout.NORTH, Button);
        LayOut_panel.putConstraint(SpringLayout.WEST, contentPane,30,SpringLayout.WEST , Button);

        frame.setVisible(true);
    }

}

最佳答案

应用两个不同的SpringLayout


如果我了解您的要求,我认为可以将SpringLayout组合如下:

import java.awt.*;
import javax.swing.*;

public class SpringLayoutCobinationTest {
  private static Component makeSubPanel() {
    JButton button = new JButton("ok");

    SpringLayout layout = new SpringLayout();
    JPanel pnl = new JPanel(layout);

    layout.putConstraint(SpringLayout.NORTH, button, 10, SpringLayout.NORTH, pnl);
    layout.putConstraint(SpringLayout.WEST, button, 10, SpringLayout.WEST, pnl);

    pnl.add(button);
    pnl.setBorder(BorderFactory.createLineBorder(Color.RED));

    return pnl;
  }
  public Component makeUI() {
    Component sub = makeSubPanel();

    SpringLayout layout = new SpringLayout();
    SpringLayout.Constraints c = layout.getConstraints(sub);
    c.setWidth(Spring.constant(100));
    c.setHeight(Spring.constant(100));

    JPanel panel = new JPanel(layout);
    layout.putConstraint(SpringLayout.NORTH, sub, 30, SpringLayout.NORTH, panel);
    layout.putConstraint(SpringLayout.WEST, sub, 30, SpringLayout.WEST, panel);
    panel.add(sub);

    return panel;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.add(new SpringLayoutCobinationTest().makeUI());
      f.setSize(420, 420);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

08-16 12:11