gpt4 book ai didi

java - 在 jbutton 中排序图像和文本

转载 作者:行者123 更新时间:2023-12-02 02:45:26 25 4
gpt4 key购买 nike

我试图将 JButton 上的文本覆盖在其后面的 ImageIcon 上。但是,当添加 imageIcon 时,文本就会消失。有什么办法可以指定它的显示顺序吗?下面,我尝试单独添加图像和文本,看看这是否会影响它,但没有运气。谁能帮帮我吗?

private void initButtons() {

int locationX = 0, locationY = 525;

for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
boardArray[x][y] = new ChessButton();
boardArray[x][y].setSize(75, 75);
boardArray[x][y].setLocation(locationX, locationY);
boardArray[x][y].setXAndY(x, y);

if ((x % 2 == 0 && y % 2 == 1) || (x % 2 == 1 && y % 2 == 0)) {
boardArray[x][y].setColour("white");
boardArray[x][y].setIcon(new ImageIcon("Assets/white_null_null.png"));

} else {

boardArray[x][y].setColour("black");
boardArray[x][y].setIcon(new ImageIcon("Assets/black_null_null.png"));

}
//this adds the images in an alternating pattern

chessFrame.add(boardArray[x][y]);
locationX = locationX + 75;

}
locationX = 0;

locationY = locationY - 75;

}

}

void initPieces() {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {

if ((x % 2 == 0 && y % 2 == 1) || (x % 2 == 1 && y % 2 == 0)) {
boardArray[x][y].setFont(new Font("Arial Unicode MS", Font.PLAIN, 40));
boardArray[x][y].setText("\u2654");//sets a particular chess piece as text, just testing it now.

} else {

boardArray[x][y].setFont(new Font("Arial Unicode MS", Font.PLAIN, 40));
boardArray[x][y].setText("\u2654");//sets a particular chess piece as text, just testing it now.
//this is suposed to overlay the image over the text, but it is not.
}

}
}
}

最佳答案

首先,我看到您正在调用此方法:boardArray[x][y].setLocation(locationX, locationY);

.setLocation(...) 表示您使用的是 null 布局,请阅读 Null layout is evilWhy is it frowned upon to use a null layout in Swing?了解为什么应该避免使用它。

为了创建棋盘,我将使用 GridLayout ,请阅读如何使用不同的layout managers

现在,要将文本覆盖在图标上,您只需调用 JButton#setHorizontalAlignment(...)方法并传递 SwingConstants.CENTER 作为参数。

例如:

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class ButtonWithImageAndText {
private JFrame frame;
private JButton button;
private Icon icon;

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ButtonWithImageAndText().createAndShowGui());
}

private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
try {
icon = new ImageIcon(ImageIO.read(getClass().getResource("up.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
button = new JButton();

button.setIcon(icon);

button.setText("Click me!");
button.setHorizontalTextPosition(SwingConstants.CENTER);
button.setFont(new Font("Arial", Font.PLAIN, 40));
button.setForeground(Color.RED);

frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

输出如下:

enter image description here

对于您以后的问题,请阅读如何制作有效的 Minimal, Complete and Verifiable Example (MCVE)这表明了您的问题,并且您最好尝试自己解决它,我发布的代码就是一个很好的例子,因为您可以复制粘贴它并看到与我相同的结果

关于java - 在 jbutton 中排序图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44636539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com