简体   繁体   English

在JPanel的JLabel顶部将JButton居中

[英]Center JButtons on top of JLabel in JPanel

I'm making an odds calculator and I am having some difficulties with the layout. 我正在制作赔率计算器,但布局有些困难。 My layout so far is: a JFrame with BorderLayout existing of a top panel with a grid layout positioned north in the JFrame and a center panel positioned in the center of the JFrame. 到目前为止,我的布局是:具有BorderLayout的JFrame存在于顶部面板中,顶部面板的网格布局位于JFrame的北部,而中央面板位于JFrame的中心。 In the center panel I want to draw the player cards and the table plus the cards on the table and a button. 在中间面板中,我想绘制玩家纸牌和桌子以及桌子上的纸牌和一个按钮。

Here's the layout I want to achieve (the cards top panel which is working isn't shown here). 这是我要实现的布局(正在工作的卡顶部面板未在此处显示)。

在此处输入图片说明

My layout so far: 到目前为止我的布局:

在此处输入图片说明

In this layout 4 cards are shown in the center panel they should be positioned (+1 card extra) in the center of the table (on top of the table). 在此布局中,中央面板中显示了4张卡,这些卡应放置在桌子中央(桌子上方)(额外+1张卡)。 The table is a JLabel added to the center panel, and the cards are buttons. 表格是添加到中间面板的JLabel,卡片是按钮。 Which layout do I need for the center panel to get the layout I want to achieve? 中心面板需要哪种布局才能获得想要的布局? Do I need more panels? 我需要更多面板吗?

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

    }
  }

For readability I've only included one card from the top panel in my code snippet. 为了提高可读性,我仅在代码段中包括了顶部面板中的一张卡。

Card.java Card.java

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

Instead of adding your image as JLabel, look how to render an image as part of the JPanel and then just add your buttons as normal. 与其将图像添加为JLabel,不如将图像呈现为JPanel的一部分,然后像往常一样添加按钮。 At the moment the buttons/jlabel are fighting for space. 目前,按钮/ jlabel正在争夺空间。

Googling "Set background image on JPanel" should get you the answer you need, just ignore the results talking about using a JLabel. 谷歌搜索“在JPanel上设置背景图像”应该会为您提供所需的答案,而无需理会有关使用JLabel的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM