带有 SpringLayout 的 JPanel 上的 JButton 不可见

JButton on JPanel with SpringLayout not visible

我有一个 JPanel,其中包含另一个 JPanel,我想使用 SpringLayout 在其上放置一个 JButton。但由于某种原因,JButton 未绘制。 但是,如果我使用绝对定位而不是布局管理器,则会绘制 JButton。 如果我在为 SpringLayout 设置约束后打印出 JButton 的边界,我将得到宽度和高度为 0 的位置 (0, 0)。 通过手动设置 JButton 的大小(调用 setSize()),我可以以正确的大小绘制 JButton,但位置不正确。

到目前为止,这是我的代码的精简版本:

import java.awt.Dimension;

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


public class Panel extends JPanel {

    private JPanel innerPanel;

    public Panel(){
        innerPanel = new InnerPanel();
        this.setLayout(null);

        innerPanel.setBounds(30, 30, 100, 200);
        this.add(innerPanel);

        this.setOpaque(false);
    }


    public class InnerPanel extends JPanel {

        private SpringLayout layout;
        private JButton someButton;

        public InnerPanel() {
            layout = new SpringLayout();
            this.setLayout(layout);

            someButton = new JButton("X");
            someButton.setPreferredSize(new Dimension(45, 25));
            layout.putConstraint(SpringLayout.NORTH, someButton, +5, SpringLayout.NORTH, innerPanel);
            layout.putConstraint(SpringLayout.EAST, someButton, -5, SpringLayout.EAST, innerPanel);
            this.add(someButton);

            this.setOpaque(false);
        }

    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.add(new Panel());

        f.setSize(800, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setVisible(true);
    }

}

仅将一个 JButton 放在 JPanel 上的 SpringLayout 可能看起来有点过于复杂,但我计划向 JPanel 添加更多组件,为此我需要 SpringLayout。

我正在使用 Eclipse(没有 Window Builder)并且我正在 运行 OpenSuse,如果这很重要的话。

InnerPanel的构造函数中innerPanel变量显然是null

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

public class Panel2 extends JPanel {
  private JPanel innerPanel;

  public Panel2() {
    super(new BorderLayout());
    innerPanel = new InnerPanel();
    //this.setLayout(null);
    //innerPanel.setBounds(30, 30, 100, 200);
    this.add(innerPanel);
    this.setOpaque(false);
  }
  private /* TEST static */ class InnerPanel extends JPanel {
    private SpringLayout layout;
    private JButton someButton;

    public InnerPanel() {
      super();
      layout = new SpringLayout();
      this.setLayout(layout);

      someButton = new JButton("X");
      //someButton.setPreferredSize(new Dimension(45, 25));
      System.out.println(innerPanel); //TEST
      //layout.putConstraint(SpringLayout.NORTH, someButton, +5, SpringLayout.NORTH, innerPanel);
      //layout.putConstraint(SpringLayout.EAST, someButton, -5, SpringLayout.EAST, innerPanel);
      layout.putConstraint(SpringLayout.NORTH, someButton, +5, SpringLayout.NORTH, this);
      layout.putConstraint(SpringLayout.EAST, someButton, -5, SpringLayout.EAST, this);

      this.add(someButton);
      this.setOpaque(false);
    }
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.add(new Panel2());
    f.setSize(800, 600);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}