Advertisement
cd62131

WhiteBlack

Jul 4th, 2014
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.17 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.event.MouseEvent;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. public class WhiteBlack {
  11.   private final JFrame f;
  12.   private final Container c;
  13.   private final JPanel p;
  14.   private final JButton b;
  15.   private boolean white;
  16.  
  17.   public WhiteBlack() {
  18.     f = new JFrame("WHITE AND BLACK");
  19.     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20.     f.setSize(200, 40);
  21.     f.setLocationRelativeTo(null);
  22.     c = f.getContentPane();
  23.     p = new JPanel();
  24.     b = new JButton(" PUSH ");
  25.     b.setBackground(Color.white);
  26.     white = true;
  27.     b.addMouseListener(new MouseAdapter() {
  28.       @Override
  29.       public void mouseClicked(MouseEvent e) {
  30.         if (white) {
  31.           b.setBackground(Color.black);
  32.           white = false;
  33.           return;
  34.         }
  35.         b.setBackground(Color.white);
  36.         white = true;
  37.       }
  38.     });
  39.     p.add(b);
  40.     c.add(p, BorderLayout.CENTER);
  41.     f.setVisible(true);
  42.   }
  43.  
  44.   public static void main(String[] args) {
  45.     new WhiteBlack();
  46.   }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement