JPanel: setOpaque(boolean isOpaque) : JPanel « javax.swing « Java by API






JPanel: setOpaque(boolean isOpaque)

  
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JFrame {

  public MainClass() {
    super("Opaque JPanel Demo");
    setSize(400, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel opaque = createNested(true);
    JPanel notOpaque = createNested(false);

    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(opaque);
    getContentPane().add(notOpaque);
  }

  public static void main(String[] args) {
    MainClass oe = new MainClass();
    oe.setVisible(true);
  }

  public JPanel createNested(boolean opaque) {
    JPanel outer = new JPanel(new FlowLayout());
    JPanel inner = new JPanel(new FlowLayout());
    outer.setBackground(Color.white);
    inner.setBackground(Color.black);

    inner.setOpaque(opaque);
    inner.setBorder(BorderFactory.createLineBorder(Color.gray));

    inner.add(new JButton("Button"));
    outer.add(inner);

    return outer;
  }
}

           
         
    
  








Related examples in the same category

1.JPanel: addAncestorListener(AncestorListener lis)
2.JPanel: addContainerListener(ContainerListener l)
3.JPanel: addMouseListener(MouseListener mous)
4.JPanel: addMouseMotionListener(MouseMotionListener lis)
5.JPanel: addKeyListener(KeyListener l)
6.JPanel: getFocusTraversalKeys(int id)
7.JPanel: isFocusCycleRoot()
8.JPanel: paintComponent(Graphics g)
9.JPanel: repaint()
10.JPanel: setBackground(Color c)
11.JPanel: setBorder(Border border)
12.JPanel: setComponentPopupMenu(JPopupMenu popup)
13.JPanel: setFocusCycleRoot(boolean value)
14.JPanel: setFocusTraversalPolicyProvider(boolean value)
15.JPanel: setLayout(null)
16.JPanel: setPreferredSize(Dimension preferredSize)
17.JPanel: setToolTipText(String htmlString)
18.extends JPanel