Java Jpanel setBounds不起作用

时间:2013-07-21 11:37:00

标签: java swing graphics jpanel layout-manager

我在JFrame中遇到了2个JPanels的奇怪问题。 我有一个名为“SlotCheck”的类,它扩展了JFrame。 我得到了一个名为“Slot”的类,它扩展了JPanel。 在SlotCheck构造函数中,我添加了2个名为sl和s2的Slot实例。 我使用setBounds()将它们放在板上的两个不同位置。

sl.setBounds工作并将其放在我指示的位置。 但s2.setBounds不起作用,它把它放在(0,0)。

这是它的外观:

enter image description here

我很想知道它为什么这样做。

这是SlotCheck类:

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);
  }
}

这是Slot类:

有点长,但我希望你能理解。 此外,paintComponent类在代码中是关闭的,我认为它是重要的,所以你可以去那里。

  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;
  }

}

我认可你的帮助。

2 个答案:

答案 0 :(得分:2)

  • setBounds(...)仅适用于空布局。
  • 但话说回来,不要使用null布局!
  • 请勿使用setBounds(...)
  • 了解Swing布局管理器,让他们为您完成大量的铺设组件。教程链接为:Lesson: Laying Out Components Within a Container
  • 不要忘记在paintComponent方法中调用super.paintComponent(g);,通常在覆盖的第一行。

答案 1 :(得分:-1)

好的,我发现了这个问题。 我没有把setLayout(null);