Example usage for javax.swing JLabel setBackground

List of usage examples for javax.swing JLabel setBackground

Introduction

In this page you can find the example usage for javax.swing JLabel setBackground.

Prototype

@BeanProperty(preferred = true, visualUpdate = true, description = "The background color of the component.")
public void setBackground(Color bg) 

Source Link

Document

Sets the background color of this component.

Usage

From source file:Main.java

public static void main(String[] args) {
    JLabel label = new JLabel("Hello");
    label.setOpaque(true);/*from   w w  w  . ja  va2  s.  c  om*/
    label.setBackground(Color.red);

    JPanel bottomPanel = new JPanel(new BorderLayout());
    bottomPanel.add(label, BorderLayout.LINE_END);

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
    mainPanel.setPreferredSize(new Dimension(400, 400));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    Main t = new Main();

    JPanel mainPanel = new JPanel(new BorderLayout());

    JLabel l = new JLabel("hello world");
    l.setOpaque(true);//from w ww  .  j  av a  2 s .  c  o m
    l.setBackground(Color.RED);

    JPanel extraPanel = new JPanel(new FlowLayout());
    l.setPreferredSize(new Dimension(100, 100));
    extraPanel.setBackground(Color.GREEN);

    extraPanel.add(l);

    mainPanel.add(extraPanel, BorderLayout.CENTER);

    t.setContentPane(mainPanel);

    t.setDefaultCloseOperation(EXIT_ON_CLOSE);
    t.setSize(400, 200);
    t.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Color colors[] = { Color.GREEN, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };
    JFrame frame = new JFrame("Color JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JComboBox comboBox = new JComboBox(colors);
    comboBox.setMaximumRowCount(5);//ww  w .  j  ava 2 s .c o  m
    comboBox.setEditable(true);
    comboBox.setRenderer(new ColorCellRenderer());
    frame.add(comboBox, BorderLayout.NORTH);

    final JLabel label = new JLabel();
    label.setOpaque(true);
    label.setBackground((Color) comboBox.getSelectedItem());
    frame.add(label, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color selectedColor = (Color) comboBox.getSelectedItem();
            label.setBackground(selectedColor);
        }
    };
    comboBox.addActionListener(actionListener);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JLabel label = new JLabel("Full Name :", JLabel.LEFT);

    label.setOpaque(true);/* w w w .j  a  va2  s . co  m*/
    label.setForeground(new Color(150, 150, 25));
    label.setBackground(new Color(50, 50, 25));

    JOptionPane.showMessageDialog(null, label);

}

From source file:ColorComboBox.java

public static void main(String args[]) {
    Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green,
            Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow };
    JFrame frame = new JFrame("Color JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JComboBox comboBox = new JComboBox(colors);
    comboBox.setMaximumRowCount(5);// w w  w .  ja  v a  2  s .  c o m
    comboBox.setEditable(true);
    comboBox.setRenderer(new ColorCellRenderer());
    Color color = (Color) comboBox.getSelectedItem();
    ComboBoxEditor editor = new ColorComboBoxEditor(color);
    comboBox.setEditor(editor);
    contentPane.add(comboBox, BorderLayout.NORTH);

    final JLabel label = new JLabel();
    label.setOpaque(true);
    label.setBackground((Color) comboBox.getSelectedItem());
    contentPane.add(label, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color selectedColor = (Color) comboBox.getSelectedItem();
            label.setBackground(selectedColor);
        }
    };
    comboBox.addActionListener(actionListener);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:components.TopLevelDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.//from   w  ww .  j  a v a2 s .  c  om
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TopLevelDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create the menu bar.  Make it have a green background.
    JMenuBar greenMenuBar = new JMenuBar();
    greenMenuBar.setOpaque(true);
    greenMenuBar.setBackground(new Color(154, 165, 127));
    greenMenuBar.setPreferredSize(new Dimension(200, 20));

    //Create a yellow label to put in the content pane.
    JLabel yellowLabel = new JLabel();
    yellowLabel.setOpaque(true);
    yellowLabel.setBackground(new Color(248, 213, 131));
    yellowLabel.setPreferredSize(new Dimension(200, 180));

    //Set the menu bar and add the label to the content pane.
    frame.setJMenuBar(greenMenuBar);
    frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:TopLevelDemo.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 *//* w ww  .j  ava 2  s  .  c  o m*/
private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("TopLevelDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create the menu bar. Make it have a cyan background.
    JMenuBar cyanMenuBar = new JMenuBar();
    cyanMenuBar.setOpaque(true);
    cyanMenuBar.setBackground(Color.cyan);
    cyanMenuBar.setPreferredSize(new Dimension(200, 20));

    //Create a yellow label to put in the content pane.
    JLabel yellowLabel = new JLabel();
    yellowLabel.setOpaque(true);
    yellowLabel.setBackground(Color.yellow);
    yellowLabel.setPreferredSize(new Dimension(200, 180));

    //Set the menu bar and add the label to the content pane.
    frame.setJMenuBar(cyanMenuBar);
    frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:max.hubbard.Factoring.Graphing.java

public static void makeGraphingInterface() {

    Interface.mainInterface();/*  w ww  .  j  a v a2s  .  c om*/

    panel = new JPanel(new BorderLayout(3, 225));

    JPanel pan = new JPanel();
    JLabel area = new JLabel("Graphing");
    area.setBackground(Color.lightGray);
    area.setFont(new Font("Times New Roman", Font.BOLD, 20));
    pan.add(area, BorderLayout.CENTER);

    panel.add(pan, BorderLayout.NORTH);

    final JTextField field = new JTextField();
    panel.add(field, BorderLayout.CENTER);
    field.setFont(new Font("Times New Roman", Font.BOLD, 25));
    field.setText("x^4-2x^2-8");

    JButton start = new JButton("GO!");
    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            panel.updateUI();
            Main.label.setText("");
            graph(field.getText());
        }
    });

    panel.add(start, BorderLayout.SOUTH);

    Main.getFrame().add(panel, BorderLayout.EAST);

    Main.getFrame().pack();

}

From source file:au.com.jwatmuff.eventmanager.gui.scoreboard.ScoreboardDisplayPanel.java

static void clearBackground(JLabel... labels) {
    for (JLabel label : labels) {
        label.setBackground(CLEAR);
        label.setOpaque(false);/* w ww. j a  va  2s. com*/
    }
}

From source file:LabelDemo.java

public LabelDemo() {
    super("JLabel Demo");
    setSize(600, 100);//from  ww  w  .j  a  v  a2  s .  co m

    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);
}