JLabel: setOpaque(boolean isOpaque)
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Main extends JFrame {
public Main() {
super("JLabel Demo");
setSize(600, 100);
JPanel content = new JPanel(new GridLayout(1, 4, 4, 4));
JLabel label = new JLabel("Java2s");
label.setBackground(Color.white);
content.add(label);
label = new JLabel("Java2s", SwingConstants.CENTER);
label.setOpaque(true);
label.setBackground(Color.white);
content.add(label);
label = new JLabel("Java2s");
label.setFont(new Font("Helvetica", Font.BOLD, 18));
label.setOpaque(true);
label.setBackground(Color.white);
content.add(label);
ImageIcon image = new ImageIcon("java2sLogo.gif");
label = new JLabel("Java2s", image, SwingConstants.RIGHT);
label.setVerticalTextPosition(SwingConstants.TOP);
label.setOpaque(true);
label.setBackground(Color.white);
content.add(label);
getContentPane().add(content);
setVisible(true);
}
public static void main(String args[]) {
new Main();
}
}
Related examples in the same category