JPanel: setBackground(Color c) : JPanel « javax.swing « Java by API






JPanel: setBackground(Color c)

  
import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass {

  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container content = aWindow.getContentPane();
    content.add(new MouseMotionAdapterDemo());
    aWindow.setVisible(true);
  }
}

class MouseMotionAdapterDemo extends JPanel {

  public MouseMotionAdapterDemo() {
    setBackground(Color.white);
    addMouseListener(new Adapter1(this));
    addMouseMotionListener(new Adapter2(this));
  }
}

class Adapter1 extends MouseAdapter {
  MouseMotionAdapterDemo mmad;

  public Adapter1(MouseMotionAdapterDemo mmad) {
    this.mmad = mmad;
  }

  public void mouseReleased(MouseEvent me) {
    mmad.setBackground(Color.white);
    mmad.repaint();
  }
}

class Adapter2 extends MouseMotionAdapter {
  MouseMotionAdapterDemo mmad;

  public Adapter2(MouseMotionAdapterDemo mmad) {
    this.mmad = mmad;
  }

  public void mouseDragged(MouseEvent me) {
    mmad.setBackground(Color.cyan);
    mmad.repaint();
  }
}
           
         
    
  








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: setBorder(Border border)
11.JPanel: setComponentPopupMenu(JPopupMenu popup)
12.JPanel: setFocusCycleRoot(boolean value)
13.JPanel: setFocusTraversalPolicyProvider(boolean value)
14.JPanel: setLayout(null)
15.JPanel: setOpaque(boolean isOpaque)
16.JPanel: setPreferredSize(Dimension preferredSize)
17.JPanel: setToolTipText(String htmlString)
18.extends JPanel