0

以下代码有什么问题?为什么不显示矩形

public class SynapsePermanencesViewer {

public JPanel createContentPane(Region region) {
JPanel synapseLayer = new JPanel();
synapseLayer.setLayout(null);

Column[][] columns = region.getColumns();

JPanel redSquare = new JPanel();
Color color = new Color(128, 0, 0);
redSquare.setBackground(color);
int squareLength = 50;
redSquare.setSize(squareLength, squareLength);

// calculate the correct location
redSquare.setLocation(150, 150); // <==== This square isn't displaying WHY???

synapseLayer.setOpaque(true);
return synapseLayer;
}

public SynapsePermanencesViewer(Region region) {
JFrame frame = new JFrame("Synapse Permanences Viewer");

frame.setContentPane(this.createContentPane(region));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
Region parentRegion = new Region("parentRegion", 2, 2, 1, 20, 1);
Region childRegion = new Region("childRegion", 4, 4, 1, 20, 3);
RegionToRegionConnect connectType = new RegionToRegionRectangleConnect();
connectType.connect(childRegion, parentRegion, 0, 0);

SynapsePermanencesViewer object = new SynapsePermanencesViewer(parentRegion);
}

}

4

1 回答 1

2
  1. 您不会将 redSquare 添加到 synapseLayer。

  2. 即使您确实添加了正方形,它也不会显示,因为 synapseLayer 使用空布局,因此该面板的大小为 (0, 0)。因此,当您打包框架时,没有什么可显示的。

不要使用空布局!!!让布局管理器为您确定面板的大小,以便 pack() 方法正常工作。

于 2013-06-24T22:33:23.977 回答