gpt4 book ai didi

Java Jpanel setBounds 不工作

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:36 24 4
gpt4 key购买 nike

我在包含 2 个 JPanel 的 JFrame 中遇到了一个奇怪的问题。我有一个名为“SlotCheck”的类,它扩展了 JFrame。我有一个名为“Slot”的类,它扩展了 JPanel。在 SlotCheck 构造函数中,我添加了 2 个 Slot 实例,分别称为 sl 和 s2。我使用 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;
}

}

感谢您的帮助。

最佳答案

  • setBounds(...) 仅适用于空布局。
  • 但是话虽如此,不要使用空布局!
  • 不要使用setBounds(...)
  • 了解 Swing 布局管理器,让它们为您完成放置组件的繁重工作。教程链接为:Lesson: Laying Out Components Within a Container
  • 不要忘记在您的paintComponent方法中调用super.paintComponent(g);,通常在覆盖的第一行。

关于Java Jpanel setBounds 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17771914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com