Results 1 to 2 of 2
  1. #1
    trbox is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Question How do I use multiple background colors?

    I want to "split" my frame into two areas, where the background color in the northern area should be blue, and the background in the southern area should be green.

    Is this possible without drawing two filled rectangles with the respective colors?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Decided to to whip up a test class, since I hadn't used JLayeredPane before. The method "createLayeredPane" and the way it is used in this class can both be directly taken and used anywhere (although you will probably want to set and/or paramatarise the colors).

    Java Code:
    package test;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
    
    public class LayeredTest {
    
        private static Container createLayeredPane(JComponent content) {
            // Create a LayeredPane and set the constraints values that remain constant
            JLayeredPane jlp = new JLayeredPane();
            GridBagLayout gbl = new GridBagLayout();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.gridx = 0;
            gbc.insets = new Insets(0, 0, 0, 0);
            gbc.ipadx = 0;
            gbc.ipady = 0;
            gbc.weightx = 1.0;
            gbc.weighty = 0.5;
            jlp.setLayout(gbl);
    
            // Create and add the "top half" background
            JPanel north = new JPanel();
            north.setOpaque(true);
            north.setBackground(Color.BLUE);
            gbc.gridheight = 1;
            gbc.gridy = 0;
            gbl.setConstraints(north, gbc);
            jlp.add(north, gbc);
            jlp.setLayer(north, JLayeredPane.DEFAULT_LAYER);
    
            // Create and add the "bottom half" background
            JPanel south = new JPanel();
            south.setOpaque(true);
            south.setBackground(Color.GRAY);
            gbc.gridheight = GridBagConstraints.REMAINDER;
            gbc.gridy = 1;
            gbl.setConstraints(south, gbc);
            jlp.add(south, gbc);
            jlp.setLayer(south, JLayeredPane.DEFAULT_LAYER);
    
            // Add the "content" Pane
            content.setOpaque(false);
            gbc.gridy = 0;
            gbl.setConstraints(content, gbc);
            jlp.add(content, gbc);
            jlp.setLayer(content, JLayeredPane.PALETTE_LAYER);
    
            return jlp;
        }
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("LayeredTest");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel content = new JPanel();
            frame.setContentPane(createLayeredPane(content));
    
            content.setLayout(new BorderLayout());
            content.add(new JLabel("             North             "), BorderLayout.NORTH);
            content.add(new JLabel("             South             "), BorderLayout.SOUTH);
            content.add(new JLabel("East"), BorderLayout.EAST);
            content.add(new JLabel("West"), BorderLayout.WEST);
            content.add(new JButton("Center"), BorderLayout.CENTER);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Not necessarily the best way to do this, but I found it an interesting and portable one, so decided to try it.

Similar Threads

  1. How to Run a .exe in background???
    By jazz2k8 in forum New To Java
    Replies: 9
    Last Post: 01-21-2011, 11:27 AM
  2. BackGround & ForeGround
    By hungleon88 in forum Advanced Java
    Replies: 3
    Last Post: 08-31-2008, 07:20 AM
  3. change colors
    By calblue in forum New To Java
    Replies: 1
    Last Post: 12-02-2007, 11:52 PM
  4. Set the background in Java
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:13 AM
  5. how to set multiple colors in a JtextArea
    By valery in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2007, 05:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •