Example usage for javax.swing JPanel setBackground

List of usage examples for javax.swing JPanel setBackground

Introduction

In this page you can find the example usage for javax.swing JPanel 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) {
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice vc = env.getDefaultScreenDevice();
    JFrame window = new JFrame();
    JPanel comp = new JPanel();
    comp.setBackground(Color.RED);
    window.add(comp);//from   w  ww.j av  a2s.  c o  m
    window.setUndecorated(true);
    window.setResizable(false);
    vc.setFullScreenWindow(window);
}

From source file:BorderExample.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new BorderLayout());
    JPanel top = new JPanel();

    top.setBackground(Color.gray);
    top.setPreferredSize(new Dimension(250, 150));
    panel.add(top);//from www  . j a  v  a  2  s .  c  o  m

    panel.setBorder(new EmptyBorder(new Insets(10, 20, 30, 40)));
    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:Main.java

public static void main(String... args) throws Exception {
    JPanel panel = new JPanel();
    panel.setOpaque(true);//w  w w . ja  v a2s .c  o m
    panel.setBackground(Color.RED);

    java.net.URL url = new java.net.URL("http://www.java2s.com/style/download.png");
    ImageIcon image = new ImageIcon(url);
    JLabel label = new JLabel("LABEL", image, JLabel.RIGHT);
    panel.add(label);

    JOptionPane.showMessageDialog(null, panel, "Modified JOptionPane : ", JOptionPane.PLAIN_MESSAGE);
}

From source file:Main.java

public static void main(final String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 300);// w  w w  .  ja  va  2  s . c  o m
    f.setLocationRelativeTo(null);

    f.setUndecorated(true);
    f.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

    JPanel panel = new JPanel();
    panel.setBackground(java.awt.Color.white);
    f.setContentPane(panel);

    MetalLookAndFeel.setCurrentTheme(new MyDefaultMetalTheme());
    try {
        UIManager.setLookAndFeel(new MetalLookAndFeel());
    } catch (Exception e) {
        e.printStackTrace();
    }

    SwingUtilities.updateComponentTreeUI(f);

    f.setVisible(true);
}

From source file:Main.java

public static void main(String... args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPane = new JPanel();
    contentPane.setOpaque(true);//from  w  w  w. jav  a 2s  . c om
    contentPane.setBackground(Color.WHITE);
    contentPane.setLayout(null);

    JLabel label = new JLabel("This JPanel uses Absolute Positioning", JLabel.CENTER);
    label.setSize(300, 30);
    label.setLocation(5, 5);

    JButton button = new JButton("USELESS");
    button.setSize(100, 30);
    button.setLocation(95, 45);

    contentPane.add(label);
    contentPane.add(button);

    frame.setContentPane(contentPane);
    frame.setSize(310, 125);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame("This is BOX LAYOUT");
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
    panel1.setBackground(Color.yellow);

    JPanel panel2 = new JPanel();
    panel2.setLayout(new FlowLayout());
    panel2.setBackground(Color.ORANGE);

    for (int i = 0; i < 5; i++)
        panel1.add(new JCheckBox("CheckBox " + (i + 1)));

    f.add(panel1, BorderLayout.WEST);
    f.add(panel2, BorderLayout.CENTER);
    f.setVisible(true);//  www  . jav a 2s . c  o  m
    f.setSize(300, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

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);/*  w  ww. jav a 2s . c  om*/
    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) {
    JPanel contentPane;/* w  w  w .  jav a2s.co  m*/
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());

    JPanel centerPanel = new JPanel();
    centerPanel.setOpaque(true);
    centerPanel.setBackground(Color.CYAN);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    gbc.weightx = 1.0;
    gbc.weighty = 0.9;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.BOTH;

    contentPane.add(centerPanel, gbc);

    JButton leftButton = new JButton("Left");
    JButton rightButton = new JButton("Right");
    gbc.gridwidth = 1;
    gbc.gridy = 1;
    gbc.weightx = 0.3;
    gbc.weighty = 0.1;

    contentPane.add(leftButton, gbc);

    gbc.gridx = 1;
    gbc.weightx = 0.7;
    gbc.weighty = 0.1;

    contentPane.add(rightButton, gbc);

    frame.setContentPane(contentPane);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JPanel topPanel = new JPanel();
    topPanel.setPreferredSize(new Dimension(200, 200));
    topPanel.setBackground(Color.WHITE);

    JTextArea chatArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(chatArea);

    JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    mainPanel.add(topPanel, BorderLayout.CENTER);
    mainPanel.add(scrollPane, BorderLayout.SOUTH);

    chatArea.getDocument().addDocumentListener(new DocumentListener() {

        @Override// ww w.j a  v  a 2 s.  c  o m
        public void insertUpdate(DocumentEvent e) {
            updateLineCount();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updateLineCount();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updateLineCount();
        }

        private void updateLineCount() {
            int lineCount = chatArea.getLineCount();
            if (lineCount <= 4) {
                chatArea.setRows(lineCount);
                mainPanel.revalidate();
            }
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(mainPanel);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int specificX = 40;
    int specificY = 20;

    JPanel gui = new JPanel(new BorderLayout());
    JTextField tf = new JTextField(10);
    JPanel borderPanel = new JPanel(new GridLayout());
    borderPanel.add(tf);/*from ww  w.j a v a 2 s.  c o m*/
    borderPanel.setBorder(new EmptyBorder(specificX, specificY, specificX, specificY));
    borderPanel.setBackground(Color.GREEN);
    gui.add(borderPanel);

    JOptionPane.showMessageDialog(null, gui);

}