Java赔率计算器布局

时间:2015-04-10 09:09:55

标签: java

我正在用Java制作一个扑克赔率计算器。我想要的布局是这样的(标签显示现在显示的每个玩家下面的赔率)

enter image description here

表格上方的矩形都是扩展JButton的Card对象。玩家和桌面下的矩形也是卡片对象。新的手形与正常的JButton一样。死卡也是卡片对象。表本身是JLabel。我读了不同的布局,但我不知道使用哪一个(对于JFrame和JPanel)。如您所见,我还需要将一些组件放在彼此之上。在这里设置布局的最佳方法是什么?多个面板,空布局?

在代码下方,您可以看到我如何创建Card并添加它(我这样做52x)。你可以看到我如何添加表格和玩家1点1 Card

public class OddsCalculator extends JFrame{

JPanel cardsPanel;
JLabel tablePicture;

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;

public OddsCalculator(){
    initUI();
}


public void initUI() {;
   setLayout(new FlowLayout(FlowLayout.LEFT));
   cardsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
   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();

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

   cardsPanel.setPreferredSize(new Dimension(1200,800));//was 1000/600

   getContentPane().add(cardsPanel);

   //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);
        }
    });
   cardsPanel.add(aceClubs);

   cardsPanel.add(tablePicture); //add the table

   //PLAYER 1 CARD SPOTS
   playerOneCardOne.iconPath=Constants.CARD_BACKSIDE;
   playerOneCardOne.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(playerOneCardOne.iconPath)));
   playerOneCardOne.setBorder(null);
   playerOneCardOne.setContentAreaFilled(false);
   playerOneCardOne.setLocation(new Point(100,100));
   cardsPanel.add(playerOneCardOne);
}

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

     JButton b=(JButton)evt.getSource();
     System.out.println("Location: "+b.getLocation());
     b.setVisible(false);

}              

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

Card.cs

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

1 个答案:

答案 0 :(得分:1)

使用BorderLayoutb1,顶部窗格中的卡片(PAGE_START),底部窗格中的死卡片(PAGE_END)和{{1 }},b2,位于中心(BorderLayout)窗格中。 CENTER的左右窗格将为空。

将表格放在b1的中间窗格中,将播放器放在顶部,底部,左侧(b2)和右侧(LINE_START)窗格中。

通过向LINE_END窗格添加适当的布局管理器来放置“新手”窗口小部件。 b2.LINE_STARTGridLayout可能是一个不错的选择。

我使用了AWT / Swing名称作为布局管理器。如果您选择使用JavaFX,只需切换到JavaFX等效项:

AWT:GridBagLayout = JavaFX:BorderLayout

AWT:BorderPane / GridLayout = JavaFX:GridBagLayout

在JavaFX中,使用GridPane将小部件放在彼此之上。

链接:

AWT/Swing Layout managers

JavaFX Layout managers