如何在Container的BorderLayout的SOUTH位置绘制一条水平线?

时间:2011-09-28 09:09:23

标签: java-me lwuit

我有一个Container,其布局是BorderLayout。我想在SOUTH位置画一条水平线,因为这个Container被重复绘制,所以我想用水平线分隔每个。

以下是代码:

tList = new List(tModel);
tList.setListCellRenderer(new CTable(listclient));

public class CTable extends Container implements ListCellRenderer {

  private Label pic = new Label("");
  private Container cnt;
  private Label name = new Label("");
  private Label credit = new Label("");
  private ligneHorizontal ligne;

  private Font fontLibelle = (MenuPrincipalForm.r).getFont("FontTextFieldBold");

  private Label focus = new Label("");

  public CTable(Vector valeur)
  {
      setLayout(new BorderLayout());
      addComponent(BorderLayout.WEST, pic);
      cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
      name.getStyle().setBgTransparency(0);
      name.getStyle().setFont(fontLibelle);
      credit.getStyle().setBgTransparency(0);
      cnt.addComponent(name);
      cnt.addComponent(credit);
      ligne = new ligneHorizontal(100);
      cnt.addComponent(ligne);
      addComponent(BorderLayout.CENTER, cnt);
      focus.getStyle().setBgTransparency(100);
  }

  public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
  {
      if ("-".equalsIgnoreCase(String.valueOf(value)))
      {
          name.setText(" - ");
          credit.setText("Encours : -  Impayés : -");
          pic.setIcon(null);
      }
      else
      {
          if (index%2 == 0)
              this.setUIID("evenRowsCell");
          else
              this.setUIID("oddRowsCell");
          name.setText("123456789012 - Rasolofomanana Marc");
          credit.setText("Crédits : 15.000 (Ar) Impayés : 10.000 (Ar)");
          pic.setIcon(null);
      }
      return this;
  }
  public Component getListFocusComponent(List list)
  {
      return focus;
  }
}

public class ligneHorizontal extends Label {
    public ligneHorizontal(int l)
    {
        String t = "";
        do
        {
            t.concat(new String("-"));
        }
        while(t.length()<l);
        this.setText(t);
        this.setPreferredH(5);
        this.getStyle().setBgColor(0);
    }
}

而且我想知道如何对List的行和奇数行进行着色,因为我尝试了但是直到我点击单元格才得到颜色。

2 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案:我派生BackgroundPainterpublic void paint(Graphics g,Rectangle rect)方法中我画了一行(g.drawLine(rect.getX(), rect.getY()+h-1, rect.getX()+w, rect.getY()+h-1);)。 因此,当我想在容器中绘制一条线时,我会写myContainer.getStyle().setBgPainter(new derivedClassName(myContainer));

答案 1 :(得分:0)

添加Label,首选高度为5,容器之间为黑色bg颜色