JLabel 之上的 JButton
JButton on top of JLabel
我有一个关于我的 pokerodds 计算器的布局问题。我的 JFrame
存在一个带网格布局的顶部面板和一个带边框布局的中心面板。此中心面板添加了一个 JLabel
,在 BorderLayout.CENTER
中有一个图像,在 BorderLayout.NORTH
中添加了一个 JPanel
。 BorderLayout.NORTH
中的面板有两个 JButton。现在我想在 JLabel(扑克 table)之上放置一个 JButton,但我不能这样做 centerPanel.add(boardCard1.BorderLayout.CENTER);
,因为我已经为 JLabel 这样做了。我该如何解决这个问题并将 Cards
放在 table 的中心?我只在代码片段中包含了 aceClubs Card 以提供更好的可读性。
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 将按钮置于中心?
首先,您的问题太复杂,代码太多。您的问题是关于在图像顶部显示一个按钮。因此,创建一个带有图像的框架并向图像添加一个按钮。创建一个执行此操作的简单程序大约需要 20 行代码。然后一旦您理解了如何执行此操作的基本概念,您就可以将更改添加到您的实际程序中。
您发布的所有其他代码与问题无关,完全没有必要。学会在提问时简化问题。
Now I want to place a JButton on top of the JLabel (the pokertable) but I cant do centerPanel.add(boardCard1.BorderLayout.CENTER); because I already do this for the JLabel.
因此您将按钮添加到标签:
label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());
只要您要显示的组件小于标签上图像的大小,组件就会在图像中居中。
我有一个关于我的 pokerodds 计算器的布局问题。我的 JFrame
存在一个带网格布局的顶部面板和一个带边框布局的中心面板。此中心面板添加了一个 JLabel
,在 BorderLayout.CENTER
中有一个图像,在 BorderLayout.NORTH
中添加了一个 JPanel
。 BorderLayout.NORTH
中的面板有两个 JButton。现在我想在 JLabel(扑克 table)之上放置一个 JButton,但我不能这样做 centerPanel.add(boardCard1.BorderLayout.CENTER);
,因为我已经为 JLabel 这样做了。我该如何解决这个问题并将 Cards
放在 table 的中心?我只在代码片段中包含了 aceClubs Card 以提供更好的可读性。
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 将按钮置于中心?
首先,您的问题太复杂,代码太多。您的问题是关于在图像顶部显示一个按钮。因此,创建一个带有图像的框架并向图像添加一个按钮。创建一个执行此操作的简单程序大约需要 20 行代码。然后一旦您理解了如何执行此操作的基本概念,您就可以将更改添加到您的实际程序中。
您发布的所有其他代码与问题无关,完全没有必要。学会在提问时简化问题。
Now I want to place a JButton on top of the JLabel (the pokertable) but I cant do centerPanel.add(boardCard1.BorderLayout.CENTER); because I already do this for the JLabel.
因此您将按钮添加到标签:
label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());
只要您要显示的组件小于标签上图像的大小,组件就会在图像中居中。