Java-Jpanel setBounds funktioniert nicht

Stieß ich auf eine seltsame problem in einen JFrame cointaining 2 JPanels.
Ich habe eine Klasse namens "SlotCheck" erstreckt JFrame.
und ich habe eine Klasse namens "Slot" Erweitert JPanel.
In der SlotCheck Konstruktor füge ich 2-Slot-Instanzen genannt sl und s2.
Ich verwenden Sie setBounds() zu setzen, Sie in zwei verschiedene Standorte auf dem Brett.

sl.setBounds funktioniert und setzt es wo ich angeben.
aber s2.setBounds funktioniert nicht und er setzt es in (0,0).

hier ist, wie es aussieht:

Java-Jpanel setBounds funktioniert nicht

ich würde gerne wissen, warum es das tut.

hier ist die SlotCheck Klasse:

package Try1;
import java.io.IOException;
import javax.swing.JFrame;

public class SlotCheck extends JFrame{
  private Slot sl;
  private Slot s2;
  public SlotCheck() throws IOException{
  sl = new Slot(4,2,SlotType.white);
  System.out.print(sl.toString());
  add(sl);
  sl.setBounds(100,0,40,300);
  sl.setVisible(true);

  s2 = new Slot(6,2,SlotType.black);
  System.out.print(s2.toString());
  add(s2);
  s2.setBounds(200,0,40,300);
  s2.setVisible(true);
  }

  public static void main(String[]args) throws IOException{
  SlotCheck sc = new SlotCheck();
  sc.setDefaultCloseOperation(EXIT_ON_CLOSE);
  sc.setTitle("check of slot");
  sc.setVisible(true);
  sc.setLocation(300, 200);
  sc.setSize(400,400);
  }
}

Und hier ist die Slot-Klasse:

Es ist ziemlich lang, aber ich hoffe, Sie verstehen.
Auch die paintComponent-Klasse ist nach unten in den code und ich denke, die die eine, die Fragen so u kann es gehen.

  package Try1;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Stack;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

//slot is a defined place on the backgammon board that can hold 
//one type of pieces black or white.
//there are 24 slots 12 on each side and 6 in each quartor.
//if a slot holds 2 or more pieces those pieces cannot be eaten.
public class Slot extends JPanel{
  private int slotNumber;
  private int piecesAmount;
  private SlotType type;
  private Stack<WhitePiece> wPieces;
  private Stack<BlackPiece> bPieces;
  private Image wPieceImage;
  private Image bPieceImage;
  public Slot() throws IOException{
  type = SlotType.empty;
  piecesAmount = 0;
  setSize(300,40);
  wPieces = new Stack<WhitePiece>();
  bPieces = new Stack<BlackPiece>();
  wPieceImage = ImageIO.read(new File("pics/whitePiece.png"));
  bPieceImage = ImageIO.read(new File("pics/blackPiece.png"));
 }
  public Slot(int pA, int sN, SlotType t) throws IOException{
  if(t != SlotType.empty){
      piecesAmount = pA;
      slotNumber = sN;
      type = t;
      wPieces = new Stack<WhitePiece>();
      bPieces = new Stack<BlackPiece>();
      wPieceImage = ImageIO.read(new File("pics/whitePiece.png"));
      bPieceImage = ImageIO.read(new File("pics/blackPiece.png"));
      if(t == SlotType.black){
          for(int i=0;i<pA;i++)
              bPieces.push(new BlackPiece());
      }else{
          for(int i=0;i<pA;i++)
              wPieces.push(new WhitePiece());
      }
  }
  }
  public SlotType getType(){
  return type;
  }
  public void setType(SlotType t){
  if(piecesAmount == 0)
    type = t;
  }
   public int getPiecesAmount(){
  return piecesAmount;
  }
   public void setPiecesAmount(int pa) throws IOException{ 
  if(type != SlotType.empty){
      piecesAmount = pa;
      if(type == SlotType.black){
         if(pa>bPieces.size())
           for(int i=0;i<(pa-bPieces.size());i++)
              bPieces.push(new BlackPiece());
         else
             if(pa<bPieces.size())
                 for(int i=0;i<(bPieces.size()-pa);i++)
                     bPieces.pop();
     }
      else{
          if(pa>wPieces.size())
               for(int i=0;i<(pa-wPieces.size());i++)
                  wPieces.push(new WhitePiece());
             else
                 if(pa<wPieces.size())
                     for(int i=0;i<(wPieces.size()-pa);i++)
                         wPieces.pop();
      }
  }else{
      System.out.println("Slot #"+slotNumber+" is Empty Slot");
  }
  }
  public void decreasePiecesAmount(){
  if(type != SlotType.empty){
      piecesAmount --;
      if(type == SlotType.black)
          bPieces.pop();
      else
          wPieces.pop();
  }
  }
  public void increasePiecesAmount() throws IOException{
  if(type != SlotType.empty){
      piecesAmount ++;
      if(type == SlotType.black)
          bPieces.push(new BlackPiece());
      else
          wPieces.push(new WhitePiece());
  }
  }
  public void pushPiece(){

  }
  public void paintComponent(Graphics g){     
  if(type == SlotType.empty){ 
      System.out.println("no type selected slot is empty Slot  Number"+slotNumber);
  }else
      if(type == SlotType.white){
        if(!wPieces.isEmpty()){
        try {
            wPieces.push(new WhitePiece());
        } catch (IOException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(slotNumber <= 11){
          for(int i=0;i<piecesAmount;i++){
            g.drawImage( wPieceImage, 5, i*30, null);
          }
        }
        else{
            for(int i=0;i<piecesAmount;i++){
                g.drawImage(wPieceImage, 5,300-(i*30), null);
            }
       }
        }else{
            System.out.println("Slot Stack is Empty Slot #"+slotNumber);
        }
      }else
      {
          if(!bPieces.isEmpty()){

          if(slotNumber<=11){
             for(int i=0;i<piecesAmount;i++){
                g.drawImage(bPieceImage, 5, i*30, 30, 30, null);
            }
          }else{
              for(int i=0;i<piecesAmount;i++){
                    g.drawImage(bPieceImage, 5, 300-(i*30), 30, 30, null);
              }  
          }
    }
          else{
      System.out.println("Slot Stack is empty Slot #"+slotNumber);
   }
}

}
protected void setSlotNumber(int sN){
  slotNumber = sN;
}
public int getSlotNumber(){
  return slotNumber;
}
public String toString(){
return "Slot #"+slotNumber+"\nSlot Type is: "+type.toString()+"\nAmount of pieces is: "+piecesAmount;
  }

}

Ich appritiate Ihre Hilfe.

Neben der Beratung schon von Hovercraft, sicher sein, zu tun alle GUI-Aufgaben in der event-dispatch-thread. Siehe: docs.oracle.com/javase/tutorial/uiswing/concurrency/...

InformationsquelleAutor Gilad Ben Ye | 2013-07-21

Schreibe einen Kommentar