Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class WhiteBlack {
- private final JFrame f;
- private final Container c;
- private final JPanel p;
- private final JButton b;
- private boolean white;
- public WhiteBlack() {
- f = new JFrame("WHITE AND BLACK");
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setSize(200, 40);
- f.setLocationRelativeTo(null);
- c = f.getContentPane();
- p = new JPanel();
- b = new JButton(" PUSH ");
- b.setBackground(Color.white);
- white = true;
- b.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if (white) {
- b.setBackground(Color.black);
- white = false;
- return;
- }
- b.setBackground(Color.white);
- white = true;
- }
- });
- p.add(b);
- c.add(p, BorderLayout.CENTER);
- f.setVisible(true);
- }
- public static void main(String[] args) {
- new WhiteBlack();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement