我试图将JPanels添加到GridbagLayout,但它们没有出现

时间:2013-09-02 10:04:02

标签: java swing layout-manager gridbaglayout

这是代码。

    public class Habitacion extends JFrame{
   ArrayList <JButton> botones=new ArrayList();
   ArrayList<ascensor> ascensores=new ArrayList();
   boolean ban=true;
   int numeroX=0;
    int numeroY=0;
    int i=0,j=0,k=0;
    JPanel botonesPanel;
    JPanel panelAscensores;
    public Habitacion(){         
    //JFrame ventana = new JFrame();
    this.setTitle("ascensor");
    this.setLayout(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
    this.setSize(1366,768);
    this.botonesPanel= new JPanel(new GridLayout(8, 1));
    this.panelAscensores= new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();    
    for(i=0;i<8;i++){
        numeroX=0;
        for(j=0;j<10;j++){
            if(ascensores.size()!=10){
                ascensores.add(new ascensor(5+numeroX,20+numeroY));
                c.fill = GridBagConstraints.CENTER;
                c.gridx = j;
                c.gridy = 0;
                this.panelAscensores.add(ascensores.get(j), c);
                ascensores.get(j).setBounds(0+numeroX,0,100,720);
            }
this.setLayout(new BorderLayout());
       this.add(botonesPanel,BorderLayout.EAST);
         this.add(panelAscensores,BorderLayout.CENTER);
        }

这是电梯类。对于习惯类(它是一个框架)还有更多内容,但它是一个MouseListener,如果没有可视化Lables我无法实现

public class ascensor extends JPanel implements Runnable{
 public int posX,y,newY,puertaDer,puertaIzq;
 //MiMouseAdapter e=new MiMouseAdapter();
 Thread hilo;
 boolean ban=true;

public ascensor(int posX,int posY){
    this.posX=posX;
    this.y=posY;
    this.newY=posY;
    puertaDer=posX;
    puertaIzq=posX+40;
    hilo=new Thread(this);
}

@Override
public void run() {
    int numero=0;
    Boolean bandera=true;

    while(true){
    while(bandera){
        if(newY < y)
            numero=-1;
        else
            numero=1;
        System.out.println(y);
        try {
            Thread.sleep(10);
        } catch (InterruptedException ex) {

        }
        y=y+numero;
        if(y==newY)                
            hilo.suspend();
        repaint();
    }       
    System.out.println("posicion Y:");
    System.out.println(newY);
    repaint();
    }

}
 @Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D pluma=(Graphics2D)g;

    g.drawRect(posX,newY+10,40, 50);
    pluma.setColor(Color.red);
    pluma.drawLine(puertaDer, 10+newY, puertaDer, 60+newY);
    pluma.drawLine(puertaIzq, 10+newY,puertaIzq, 60+newY);
    }
  }
}

Ascensores,在此上下文中,是一个从JLAbel扩展的类。它与GridLAyout完美结合,问题是我必须在用户触发事件时移动这些面板。我认为在GridBagLayout中移动元素会更容易,但它似乎不起作用。顺便说一句,这就像电梯在现实生活中会起作用一样,所以这就是我正在努力的事情。如果有人对此有所了解,也会非常感激。

0 个答案:

没有答案