如何在另一个 JLabel 的图标上显示一个 JLabel 的图标
How to display a JLabel's icon over another JLabel's icon
假设我正在构建一个带有 swing 的国际象棋应用程序。我使用一组 JLabel 来表示棋盘(每个都有其适当的图标设置为 lightly/dark 阴影框)。我创建了另一个 JLabel 数组来保存棋子的图标,但我对 swing 不够熟悉,不知道如何实现它们以显示在棋盘顶部。有人知道任何技术吗?
我写了一个小例子,它构建了一个 window 和两个 JLabel。
请注意,grey.jpg 和 pawn.png 图像的大小为 128x128,棋子的背景为透明(这样我就避免了棋子图像的背景隐藏灰色矩形框)。
这是构建 window 并添加组件的 ChessFrame class:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ChessFrame extends JFrame {
private JPanel panel;
private JLabel greyBox;
private JLabel pawn;
public ChessFrame() {
super();
/* configure the JFrame */
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addComponents() {
panel = new JPanel();
greyBox = new JLabel(new ImageIcon("images/grey.jpg"));
pawn = new JLabel(new ImageIcon("images/pawn.png"));
/* add the pawn inside the grey box (we have to set a layout for the grey box JLabel) */
greyBox.setLayout(new BorderLayout());
greyBox.add(pawn);
/* add grey box to main JPanel and set its background to white so we observe the result better */
panel.add(greyBox);
panel.setBackground(Color.WHITE);
this.getContentPane().add(panel);
}
@Override
public void setVisible(boolean b) {
super.setVisible(b);
}
}
这里是创建 ChessFrame 对象并显示 window:
的 Main class
public class Main {
public static void main(String[] args) {
ChessFrame chessFrame = new ChessFrame();
chessFrame.addComponents();
chessFrame.setVisible(true);
}
}
假设我正在构建一个带有 swing 的国际象棋应用程序。我使用一组 JLabel 来表示棋盘(每个都有其适当的图标设置为 lightly/dark 阴影框)。我创建了另一个 JLabel 数组来保存棋子的图标,但我对 swing 不够熟悉,不知道如何实现它们以显示在棋盘顶部。有人知道任何技术吗?
我写了一个小例子,它构建了一个 window 和两个 JLabel。
请注意,grey.jpg 和 pawn.png 图像的大小为 128x128,棋子的背景为透明(这样我就避免了棋子图像的背景隐藏灰色矩形框)。
这是构建 window 并添加组件的 ChessFrame class:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ChessFrame extends JFrame {
private JPanel panel;
private JLabel greyBox;
private JLabel pawn;
public ChessFrame() {
super();
/* configure the JFrame */
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addComponents() {
panel = new JPanel();
greyBox = new JLabel(new ImageIcon("images/grey.jpg"));
pawn = new JLabel(new ImageIcon("images/pawn.png"));
/* add the pawn inside the grey box (we have to set a layout for the grey box JLabel) */
greyBox.setLayout(new BorderLayout());
greyBox.add(pawn);
/* add grey box to main JPanel and set its background to white so we observe the result better */
panel.add(greyBox);
panel.setBackground(Color.WHITE);
this.getContentPane().add(panel);
}
@Override
public void setVisible(boolean b) {
super.setVisible(b);
}
}
这里是创建 ChessFrame 对象并显示 window:
的 Main classpublic class Main {
public static void main(String[] args) {
ChessFrame chessFrame = new ChessFrame();
chessFrame.addComponents();
chessFrame.setVisible(true);
}
}