我有关于我的pokerodds计算器的布局问题。我JFrame
存在一个带有网格布局的顶部面板和一个带有边框布局的中心面板。此中心面板添加了JLabel
BorderLayout.CENTER
中的图片和JPanel
中的BorderLayout.NORTH
。 BorderLayout.NORTH
中的面板有两个JButton。现在我想把一个JButton放在JLabel(可以推送)之上,但我不能centerPanel.add(boardCard1.BorderLayout.CENTER);
,因为我已经为JLabel做了这个。如何解决这个问题并将Cards
放在桌子的中央?我只在代码段中包含了aceClubs卡,以提供更好的可读性。
public class OddsCalculator extends JFrame{
JPanel topPanel;
JLabel tablePicture;
JPanel centerPanel;
//ImagePanel panel = new ImagePanel(new ImageIcon(this.getClass().getResource("/images/pokertable.png")).getImage());
JPanel centerPanelNorth;
Card aceClubs;
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(new BorderLayout());
tablePicture = new JLabel(new ImageIcon(this.getClass().getResource(Constants.POKERTABLE_ICON)));
centerPanelNorth=new JPanel();
aceClubs=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); //was centerpanel
//PLAYER 1 CARD SPOTS
playerOneCardOne.iconPath=Constants.CARD_BACKSIDE;
playerOneCardOne.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(playerOneCardOne.iconPath)));
playerOneCardOne.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
playerOneCardOne.setBorderPainted(false);
playerOneCardOne.setContentAreaFilled(false);
playerOneCardOne.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
PlayerOneMouseClicked(evt);
}
});
//centerPanel.add(playerOneCardOne,BorderLayout.WEST); //was centerpanel
playerOneCardTwo.iconPath=Constants.CARD_BACKSIDE;
playerOneCardTwo.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(playerOneCardTwo.iconPath)));
playerOneCardTwo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
playerOneCardTwo.setBorderPainted(false);
playerOneCardTwo.setContentAreaFilled(false);
playerOneCardTwo.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
PlayerOneMouseClicked(evt);
}
});
//centerPanel.add(playerOneCardTwo,BorderLayout.EAST);
//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);
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,BorderLayout.CENTER); //TABLE
centerPanel.add(centerPanelNorth,BorderLayout.NORTH);
centerPanelNorth.add(playerOneCardOne);
centerPanelNorth.add(playerOneCardTwo);
//How to add for example boardCard1 on top of the table?
}
private void MouseClicked(java.awt.event.MouseEvent evt) {
//JButton b=(JButton)evt.getSource();
playerCardSpotSender=(Card)evt.getSource();
System.out.println(playerCardSpotSender.suit+" "+playerCardSpotSender.kind);
if (playerCardSpotTarget != null && playerCardSpotTarget.isBorderPainted()) {
playerCardSpotSender.setLocation(playerCardSpotTarget.getLocation());
System.out.println(playerCardSpotTarget.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;
}
目前的布局:
**编辑:** 如何使用OverlayLayout将按钮置于中心位置?
答案 0 :(得分:0)
首先,你的问题太复杂,代码太多了。您的问题是在图像顶部显示一个按钮。因此,创建一个带有图像的框架,并为图像添加一个按钮。它应该需要大约20行代码来创建一个执行此操作的简单程序。然后,一旦您了解了如何执行此操作的基本概念,您就可以将更改添加到实际程序中。
您发布的所有其他代码与问题无关,完全没必要。当您提出问题时,请学习简化您的问题。
现在我想把一个JButton放在JLabel(可扩展的)之上,但是我不能做中心事件(boardCard1.BorderLayout.CENTER);因为我已经为JLabel做了这个。
所以你将按钮添加到标签:
label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());
只要您要显示的组件小于标签上图像的大小,组件就会在图像中居中。