如何使用图标将JLabel插入另一个JLanel的中心

时间:2016-03-03 10:11:46

标签: java swing jlabel centering

我有一个Java Swing应用程序。我想插入一个带有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标签

我还尝试插入此代码:

  

labelSfondoTimer.setObaque(真);   但不起作用。

2 个答案:

答案 0 :(得分:5)

So, how can i fixed my error? - 所以,例如printcreens(基于我在你的问题中的评论 - JLabel hasn't any LayoutManager in API, JLabel has method for centering, JLabel is transparent by default, GBC without any constraints centering JComponent by default

enter image description hereenter image description hereenter image description here

来自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());
    }
}

答案 1 :(得分:3)

  

我想插入一个带有png图像的JLabel作为背景,我想在此图像的中心插入另一个JLabel

您可以在同一标签上放置文字和图标:

JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Centered Text" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);

文本将以图像为中心。

您还可以查看此帖子:JButton settext specific position,其中包含其他方法,可以更灵活地处理文本位置。