1

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

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

这是它的外观:

在此处输入图像描述

我很想知道为什么会这样。

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

}

我感谢你的帮助。

4

2 回答 2

2
  • setBounds(...)仅适用于空布局。
  • 但话虽如此,不要使用空布局!
  • 不要使用setBounds(...)
  • 了解 Swing 布局管理器,让他们为您完成铺设组件的繁重工作。教程链接是:课程:在容器中布置组件
  • 不要忘记super.paintComponent(g);在您的paintComponent 方法内部调用,通常在覆盖的第一行。
于 2013-07-21T11:46:42.267 回答
-1

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

于 2013-07-21T11:53:12.683 回答