Advertisement
Guest User

Untitled

a guest
May 27th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class GUI17 extends JFrame {
  7.  
  8.     public GUI17() {
  9.         Color tab[] = {
  10.                 Color.BLACK,
  11.                 Color.GREEN,
  12.                 Color.RED,
  13.                 Color.MAGENTA
  14.         };
  15.         JPanel jp = new JPanel();
  16.         jp = new JPanel();
  17.         jp.setPreferredSize(new Dimension(400,400));
  18.         jp.setBackground(Color.white);
  19.         JButton jb = new JButton("Colors");
  20.         jb.addActionListener(
  21.                 new ActionListener() {
  22.                     int index = 0;
  23.                     @Override
  24.                     public void actionPerformed(ActionEvent e) {
  25.                         if(index == tab.length){
  26.                             index = 0;
  27.                         }
  28.                         jb.setBackground(tab[index]);
  29.                         index++;
  30.                     }
  31.                 }
  32.         );
  33.         jp.setLayout(new BorderLayout());
  34.         jp.add(jb,BorderLayout.CENTER);
  35.         this.add(jp);
  36.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.         this.pack();
  38.         this.setVisible(true);
  39.         this.setLocationRelativeTo(null);
  40.         this.setTitle("GUI15");
  41.     }
  42.  
  43.     public static void main(String[] args) {
  44.         SwingUtilities.invokeLater(
  45.                 ()->new GUI17()
  46.         );
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement