在jbutton中订购图像和文本

ordering image and text in jbutton

我试图将 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.
            }

        }
    }
}

使用容器的 setComponentZOrder(...) 方法 class。这将使您能够设置组件的 Z-index,以按照您喜欢的顺序设置它们。另请参阅 this

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

.setLocation(...) 表示您正在使用 null 布局,请阅读 Null layout is evil and Why is it frowned upon to use a null layout in Swing? 以了解为什么应避免使用它。

为了创建棋盘,我将使用 GridLayout, please read how to use the different 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);
    }
}

给出以下输出:

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