I have a Java Swing application. I want to insert a JLabel with png image as background and I want insert another JLabel at the center of this image. So I have build this code:
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);
With this code, I can see the image on the screen but I can't see the secondo Label with text "pippo"
I have try also to insert this code:
labelSfondoTimer.setObaque(true); but not works.
So, how can i fixed my error?
- so, for example printscreens (based on my comment in your question - 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
)
from code in SSCCE / MCVE form
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());
}
}
I want to insert a JLabel with png image as background and I want insert another JLabel at the center of this image
You can have text and an Icon on the same label:
JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Centered Text" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);
The text will be centered over the image.
You can also check out this posting: JButton settext specific position , which contain other approaches that give more flexibility with the text location.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.