在JPanel中的JLabel顶部中心JButtons

时间:2015-04-13 12:04:40

标签: java swing

我正在制作赔率计算器,我在布局方面遇到了一些困难。到目前为止,我的布局是:一个JFrame,BorderLayout存在一个顶部面板,网格布局位于JFrame的北面,一个中心面板位于JFrame的中心。在中间面板中,我想绘制玩家卡片和桌子以及桌子上的卡片和按钮。

这是我想要实现的布局(此处未显示正在工作的卡片顶部面板)。

enter image description here

到目前为止我的布局:

enter image description here

在此布局中,4张牌显示在中间面板中,它们应位于桌子中央(桌子顶部)的位置(额外+1卡)。该表是添加到中心面板的JLabel,卡是按钮。中心面板需要哪种布局才能获得我想要实现的布局?我需要更多面板吗?

public class OddsCalculator extends JFrame{

JPanel topPanel;
JLabel tablePicture;
JPanel centerPanel;


Card aceClubs;
Card kingClubs;
Card queenClubs;
Card jackClubs;
Card tenClubs;
Card nineClubs;
Card eightClubs;
Card sevenClubs;
Card sixClubs;
Card fiveClubs;
Card fourClubs;
Card threeClubs;
Card twoClubs;

//spades
Card aceSpades;
Card kingSpades;
Card queenSpades;
Card jackSpades;
Card tenSpades;
Card nineSpades;
Card eightSpades;
Card sevenSpades;
Card sixSpades;
Card fiveSpades;
Card fourSpades;
Card threeSpades;
Card twoSpades;

//hearts
Card aceHearts;
Card kingHearts;
Card queenHearts;
Card jackHearts;
Card tenHearts;
Card nineHearts;
Card eightHearts;
Card sevenHearts;
Card sixHearts;
Card fiveHearts;
Card fourHearts;
Card threeHearts;
Card twoHearts;

//diamonds
Card aceDiamonds;
Card kingDiamonds;
Card queenDiamonds;
Card jackDiamonds;
Card tenDiamonds;
Card nineDiamonds;
Card eightDiamonds;
Card sevenDiamonds;
Card sixDiamonds;
Card fiveDiamonds;
Card fourDiamonds;
Card threeDiamonds;
Card twoDiamonds;

Card playerOneCardOne;
Card playerOneCardTwo;
Card playerTwoCardOne;
Card playerTwoCardTwo;
Card playerCardSpotTarget;
Card playerCardSpotSender;

Player player1=new Player();
Player player2=new Player();

Card boardCard1=new Card();
Card boardCard2=new Card();
Card boardCard3=new Card();
Card boardCard4=new Card();

Board board=new Board();



public OddsCalculator(){
    initUI();
}


public void initUI() {
   //cardsPanel=new JPanel(new GridLayout(4,13,0,0));
   setLayout(new BorderLayout());
   topPanel = new JPanel(new GridLayout(4,13,0,0)); 
   centerPanel=new JPanel(); 
   tablePicture = new JLabel(new ImageIcon(this.getClass().getResource(Constants.POKERTABLE_ICON)));

   aceClubs=new Card();
   kingClubs=new Card();
   queenClubs=new Card();
   jackClubs=new Card();
   tenClubs=new Card();
   nineClubs=new Card();
   eightClubs=new Card();
   sevenClubs=new Card();
   sixClubs=new Card();
   fiveClubs=new Card();
   fourClubs=new Card();
   threeClubs=new Card();
   twoClubs=new Card();

   aceSpades=new Card();
   kingSpades=new Card();
   queenSpades=new Card();
   jackSpades=new Card();
   tenSpades=new Card();
   nineSpades=new Card();
   eightSpades=new Card();
   sevenSpades=new Card();
   sixSpades=new Card();
   fiveSpades=new Card();
   fourSpades=new Card();
   threeSpades=new Card();
   twoSpades=new Card();

   aceHearts=new Card();
   kingHearts=new Card();
   queenHearts=new Card();
   jackHearts=new Card();
   tenHearts=new Card();
   nineHearts=new Card();
   eightHearts=new Card();
   sevenHearts=new Card();
   sixHearts=new Card();
   fiveHearts=new Card();
   fourHearts=new Card();
   threeHearts=new Card();
   twoHearts=new Card();

   aceDiamonds=new Card();
   kingDiamonds=new Card();
   queenDiamonds=new Card();
   jackDiamonds=new Card();
   tenDiamonds=new Card();
   nineDiamonds=new Card();
   eightDiamonds=new Card();
   sevenDiamonds=new Card();
   sixDiamonds=new Card();
   fiveDiamonds=new Card();
   fourDiamonds=new Card();
   threeDiamonds=new Card();
   twoDiamonds=new Card();

   playerOneCardOne=new Card();
   playerOneCardTwo=new Card();
   playerTwoCardOne=new Card();
   playerTwoCardTwo=new Card();

   boardCard1=new Card();
   boardCard2=new Card();
   boardCard3=new Card();

   board=new Board();

   //setLayout(new FlowLayout(FlowLayout.LEFT));

   topPanel.setPreferredSize(new Dimension(1200,450));//was 1000/600

   getContentPane().add(topPanel,BorderLayout.NORTH);

   //setSize(1000,1600);
   setDefaultCloseOperation(EXIT_ON_CLOSE);


   aceClubs.suit=Constants.CARD_SUIT_CLUBS;
   aceClubs.kind=Constants.CARD_KIND_ACE;
   aceClubs.iconPath=Constants.ACE_CLUBS_ICON;

   aceClubs.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(aceClubs.iconPath)));
   aceClubs.setBorder(null);
   aceClubs.setContentAreaFilled(false);
   aceClubs.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            MouseClicked(evt);
        }
    });
   topPanel.add(aceClubs,BorderLayout.PAGE_START);



   getContentPane().add(centerPanel,BorderLayout.CENTER); 

   //BOARD CARD SPOTS
   boardCard1.iconPath=Constants.CARD_BACKSIDE;
   boardCard1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(boardCard1.iconPath)));
   boardCard1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   boardCard1.setBorderPainted(false);
   boardCard1.setContentAreaFilled(false);
   boardCard1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   boardCard1.boardPosition=1;
   centerPanel.add(boardCard1);

   boardCard2.iconPath=Constants.CARD_BACKSIDE;
   boardCard2.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(boardCard2.iconPath)));
   boardCard2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   boardCard2.setBorderPainted(false);
   boardCard2.setContentAreaFilled(false);
   boardCard2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   //centerPanel.add(boardCard1,SwingConstants.CENTER);
   boardCard2.boardPosition=2;
   centerPanel.add(boardCard2);

   boardCard3.iconPath=Constants.CARD_BACKSIDE;
   boardCard3.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(boardCard3.iconPath)));
   boardCard3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   boardCard3.setBorderPainted(false);
   boardCard3.setContentAreaFilled(false);
   boardCard3.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   //centerPanel.add(boardCard1,SwingConstants.CENTER);
   boardCard3.boardPosition=3;
   centerPanel.add(boardCard3);

   boardCard4.iconPath=Constants.CARD_BACKSIDE;
   boardCard4.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(boardCard4.iconPath)));
   boardCard4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   boardCard4.setBorderPainted(false);
   boardCard4.setContentAreaFilled(false);
   boardCard4.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   //centerPanel.add(boardCard1,SwingConstants.CENTER);
   boardCard4.boardPosition=4;
   centerPanel.add(boardCard4);

   centerPanel.add(tablePicture); //TABLE





}

 private void MouseClicked(java.awt.event.MouseEvent evt)         
 {                              

     //JButton b=(JButton)evt.getSource();
     playerCardSpotSender=(Card)evt.getSource();
     //System.out.println("Location: "+b.getLocation());
     System.out.println(playerCardSpotSender.suit+" "+playerCardSpotSender.kind);
     //card.setVisible(false);



      if (playerCardSpotTarget != null && playerCardSpotTarget.isBorderPainted()) {
      playerCardSpotSender.setLocation(playerCardSpotTarget.getLocation());
             System.out.println(playerCardSpotTarget.getLocation());
             //add to board flop
             if (playerCardSpotTarget.boardPosition != null &&    playerCardSpotTarget.boardPosition <= Constants.NUMBER_OF_FLOP_CARDS) {


       playerCardSpotSender.setLocation(playerCardSpotSender.getLocation());

        }
     }

}   

private void PlayerOneMouseClicked(java.awt.event.MouseEvent evt){
     //JButton b=(JButton)evt.getSource();
     playerCardSpotTarget=(Card)evt.getSource();

    if(playerCardSpotTarget.isBorderPainted()){
        playerCardSpotTarget.setBorderPainted(false);
    }
    else{
        playerCardSpotTarget.setBorderPainted(true);
    }
}

public static void main(String[] args) {
  OddsCalculator oc=new OddsCalculator();
  oc.setVisible(true);
  oc.pack();

    }
  }

为了便于阅读,我只在我的代码段中的顶部面板中添加了一张卡片。

Card.java

public class Card extends JButton{
  int suit;
  int kind;
  boolean known;
  String iconPath;
  Integer boardPosition;
}

1 个答案:

答案 0 :(得分:0)

不要将图像添加为JLabel,而是查看如何将图像渲染为JPanel的一部分,然后正常添加按钮。按钮/ jlabel正在争夺太空。

谷歌搜索&#34;在JPanel&#34;上设置背景图像应该得到你需要的答案,只需忽略关于使用JLabel的结果。