setBounds不显示JPanel中的组件

时间:2015-08-11 12:22:50

标签: java swing layout-manager

我为JPanel创建了自己的布局管理器。在布局管理器的布局容器方法中,我添加了三个组件,前两个组件正在显示。我错过了什么吗?这是布局管理器的代码。

package com.indigo.neuron.gui.bandtrades.symbollist.orderentry.strategy;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import com.indigo.neuron.gui.bandtrades.layouts.LayoutConstants;
import com.indigo.neuron.gui.bandtrades.symbollist.SymbolPanel;
import com.indigo.neuron.gui.bandtrades.symbollist.orderentry.QuantityFieldPanel;
import com.indigo.neuron.gui.bandtrades.utils.MapUtils;
import com.indigo.neuron.gui.bandtrades.workspace.interfaces.ISymbolPanelLayoutManager;

public class SliceAnyStrategyLayoutManager implements ISymbolPanelLayoutManager {

    private Map<String, Component> labels = new HashMap<String, Component>();
    private Map<String, Component> fields = new HashMap<String, Component>();

    private boolean isFullWidth = true;

    private Insets insets = new Insets(2, 15, 15, 15);
    private Insets compactInsets = new Insets(2, 5, 5, 5);
    private Dimension prefCompSize = new Dimension(80, 24);
    private Dimension prefLabelSize = new Dimension(30, 24);
    private Dimension prefCompactLabelSize = new Dimension(30, 24);
    private int fullSpaceAfterLabel = 3;
    private int fullSpaceAfterField = 10;
    private int narrowSpaceAfterLabel = 3;
    private int narrowSpaceAfterField = 10;
    private int rowSpace = 10;
    private int compactRowSpace = 5;
    private Rectangle invisible = new Rectangle(0, 0, 0, 0);

    private Component sliceQtyField;

    /**
     * Reflection uses the following constructor
     */
    public SliceAnyStrategyLayoutManager(){

    }

    public void addLayoutComponent(String name, Component comp) {
        if (comp instanceof JLabel) {
            labels.put(name, comp);
        } else {
            fields.put(name, comp);
        }
    }

    public void removeLayoutComponent(Component comp) {

    }

    public Dimension preferredLayoutSize(Container parent) {
        int w = isFullWidth ? SymbolPanel.SYMBOL_PANEL_BASE_PLUS_SCROLL.width
                  : SymbolPanel.NARROW_SYMBOL_PANEL_BASE_PLUS_SCROLL.width;

        Component qtyField = fields.get(SliceStrategyFields.PREFERRED_SLICE_QUANTITY);
        int h = isFullWidth ? (insets.top + qtyField.getPreferredSize().height
                  + insets.bottom + rowSpace)
                  : (compactInsets.top + (qtyField.getPreferredSize().height + compactRowSpace )
                      + compactInsets.bottom);
        return new Dimension(w, h);     
    }

    public Dimension minimumLayoutSize(Container parent) {
        return preferredLayoutSize(parent);
    }

    public void layoutContainer(Container parent) {
        if (isFullWidth) {
            doRegularLayout(parent);
        } else {
            doRegularCompactLayout(parent);
        }
    }

    private void doRegularLayout(Container parent){
        int x = insets.left+fullSpaceAfterLabel;
        int y = insets.top;

        //Single Row here

        JLabel qtyLabel = (JLabel)labels.get(SliceStrategyFields.PREFERRED_SLICE_QUANTITY);
        qtyLabel.setVerticalAlignment(SwingConstants.CENTER);
        qtyLabel.setBounds(x, y, prefLabelSize.width, prefLabelSize.height);
        x+= prefLabelSize.width+fullSpaceAfterLabel;

        if(sliceQtyField != null){
            sliceQtyField.setBounds(invisible);
        }
        sliceQtyField = ((QuantityFieldPanel)fields.get(SliceStrategyFields.PREFERRED_SLICE_QUANTITY)).getCurrentComponent();
        Dimension qtyDim =  sliceQtyField.getPreferredSize();
        sliceQtyField.setBounds(x, y, qtyDim.width, prefCompSize.height);

        y+= prefCompSize.width+narrowSpaceAfterField;
        JLabel intervalLabel = (JLabel)labels.get(SliceStrategyFields.PREFERRED_INTERVAL);
        intervalLabel.setText("Comp");
        intervalLabel.setVerticalAlignment(SwingConstants.CENTER);
        intervalLabel.setBounds(0, 400, prefLabelSize.width, prefLabelSize.height);
      x+= prefLabelSize.width+fullSpaceAfterLabel;

    }

    private void doRegularCompactLayout(Container parent){
        int x = insets.left;
        int y = insets.top;


        JLabel qtyLabel = (JLabel)labels.get(SliceStrategyFields.PREFERRED_SLICE_QUANTITY);
        qtyLabel.setVerticalAlignment(SwingConstants.CENTER);
        qtyLabel.setBounds(x-2, y, prefCompactLabelSize.width, prefCompactLabelSize.height);
        x+= prefCompactLabelSize.width+narrowSpaceAfterLabel;

        if(sliceQtyField != null){
            sliceQtyField.setBounds(invisible);
        }
        sliceQtyField = ((QuantityFieldPanel)fields.get(SliceStrategyFields.PREFERRED_SLICE_QUANTITY)).getCurrentComponent();
        Dimension qtyDim =  sliceQtyField.getPreferredSize();
        sliceQtyField.setBounds(x, y, qtyDim.width, qtyDim.height);

    }

    public void clearAll(){
        labels.clear();
        fields.clear();
        sliceQtyField = null;
    }

    public void setFullWidth(boolean b) {
        isFullWidth = b;
    }

    public void setLayoutMap(Map<String, Object> map) {
        this.prefCompSize = MapUtils.getAsDimension(map, LayoutConstants.FULL_DIMENSION_1);
          this.prefLabelSize = MapUtils.getAsDimension(map, LayoutConstants.FULL_DIMENSION_2);
          this.prefCompactLabelSize = MapUtils.getAsDimension(map, LayoutConstants.NARROW_DIMENSION_2);
          this.rowSpace = MapUtils.getAsInt(map,LayoutConstants.FULL_ROW_SPACE);
          this.compactRowSpace = MapUtils.getAsInt(map, LayoutConstants.NARROW_ROW_SPACE);
          this.insets = MapUtils.getAsInsets(map, LayoutConstants.INSETS_WIDE);
          this.compactInsets = MapUtils.getAsInsets(map, LayoutConstants.INSETS_NARROW);
          this.fullSpaceAfterField  = MapUtils.getAsInt(map,LayoutConstants.FULL_SPACE_AFTER_FIELD);
          this.fullSpaceAfterLabel = MapUtils.getAsInt(map,LayoutConstants.FULL_SPACE_AFTER_LABEL);
          this.narrowSpaceAfterField = MapUtils.getAsInt(map,LayoutConstants.NARROW_SPACE_AFTER_FIELD);
          this.narrowSpaceAfterLabel = MapUtils.getAsInt(map,LayoutConstants.NARROW_SPACE_AFTER_LABEL);       
    }
}

这就是我想要实现的目标

enter image description here

0 个答案:

没有答案