Java 如何在另一个带有图标的JLanel的中心插入JLabel

Java 如何在另一个带有图标的JLanel的中心插入JLabel,java,swing,jlabel,centering,Java,Swing,Jlabel,Centering,我有一个JavaSwing应用程序。我想插入一个以png图像为背景的JLabel,并在该图像的中心插入另一个JLabel。因此,我构建了以下代码: ImageIcon icon = new ImageIcon(getClass().getResource("/resources/cornometro_white.png")); labelSfondoTimer = new JLabel(icon); labelTempoGara = new JLabel(); labelTempoGara.t

我有一个JavaSwing应用程序。我想插入一个以png图像为背景的JLabel,并在该图像的中心插入另一个JLabel。因此,我构建了以下代码:

ImageIcon icon = new ImageIcon(getClass().getResource("/resources/cornometro_white.png"));

labelSfondoTimer = new JLabel(icon);
labelTempoGara = new JLabel();
labelTempoGara.text("pippo");
GridBagConstraints GBC2 = new GridBagConstraints();
Container CR2 = new Container();
GridBagLayout GBL2 = new GridBagLayout();
CR2.setComponentOrientation(ComponentOrientation.UNKNOWN);
CR2.setLayout(GBL2);     
labelSfondoTimer.add(CR2);

GBC2 = new GridBagConstraints();
CR2.add(labelTempoGara);
GBC2.gridx=2;
GBC2.gridy=0;
GBC2.anchor= GridBagConstraints.CENTER;
GBL2.setConstraints(labelTempoGara,GBC2);
有了这段代码,我可以看到屏幕上的图像,但看不到带有文本pippo的secondo标签

我还尝试插入以下代码:

ImageIcon icon = new ImageIcon(getClass().getResource("/resources/cornometro_white.png"));

labelSfondoTimer = new JLabel(icon);
labelTempoGara = new JLabel();
labelTempoGara.text("pippo");
GridBagConstraints GBC2 = new GridBagConstraints();
Container CR2 = new Container();
GridBagLayout GBL2 = new GridBagLayout();
CR2.setComponentOrientation(ComponentOrientation.UNKNOWN);
CR2.setLayout(GBL2);     
labelSfondoTimer.add(CR2);

GBC2 = new GridBagConstraints();
CR2.add(labelTempoGara);
GBC2.gridx=2;
GBC2.gridy=0;
GBC2.anchor= GridBagConstraints.CENTER;
GBL2.setConstraints(labelTempoGara,GBC2);
labelsfondoimer.setObaquetrue; 但这不管用


那么,我如何纠正我的错误呢因此,例如,基于我在您的问题中的评论的打印屏幕-JLabel在API中没有任何LayoutManager,JLabel有居中方法,JLabel默认是透明的,GBC没有任何约束默认居中JComponent

来自SSCCE/MCVE格式的代码

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class JLabelInJLabel {

    private JFrame frame = new JFrame();
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    private JLabel parentJLabel = new JLabel();
    private JLabel childJLabel = new JLabel(infoIcon, SwingConstants.CENTER);
    private JLabel imageInJLabel = new JLabel(errorIcon, SwingConstants.CENTER);
    private JLabel simpleJLabel = new JLabel(warnIcon);

    public JLabelInJLabel() {
        parentJLabel.setLayout(new GridBagLayout());
        parentJLabel.add(childJLabel);
        parentJLabel.setBorder(BorderFactory.createLineBorder(java.awt.Color.RED));        
        imageInJLabel.setBorder(BorderFactory.createLineBorder(java.awt.Color.BLUE));        
        simpleJLabel.setBorder(BorderFactory.createLineBorder(java.awt.Color.ORANGE));        
        frame.setLayout(new GridLayout(0, 1));
        frame.add(parentJLabel);
        frame.add(imageInJLabel);
        frame.add(simpleJLabel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(100, 100);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new JLabelInJLabel());
    }
}
我想插入一个以png图像为背景的JLabel,并在该图像的中心插入另一个JLabel

您可以在同一标签上显示文本和图标:

JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Centered Text" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);
文本将居中于图像上方


您还可以查看此帖子:,其中包含其他方法,可为文本位置提供更大的灵活性。

JLabel在API中没有任何LayoutManager,JLabel有居中方法,JLabel默认情况下是透明的,GBC没有任何约束默认情况下居中JComponent所以,我如何修正我的错误?@bircastri看看这是否有帮助:请看:好,但我的答案是,我如何插入一个PNG图像并显示一个动态文本?我更确定的是,动态文本在@Hovercraft Full Eels的帖子中被张贴了几次,那么在这种情况下,在这个帖子中,没有任何理由重新发明轮子