Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class GUI17 extends JFrame {
- public GUI17() {
- Color tab[] = {
- Color.BLACK,
- Color.GREEN,
- Color.RED,
- Color.MAGENTA
- };
- JPanel jp = new JPanel();
- jp = new JPanel();
- jp.setPreferredSize(new Dimension(400,400));
- jp.setBackground(Color.white);
- JButton jb = new JButton("Colors");
- jb.addActionListener(
- new ActionListener() {
- int index = 0;
- @Override
- public void actionPerformed(ActionEvent e) {
- if(index == tab.length){
- index = 0;
- }
- jb.setBackground(tab[index]);
- index++;
- }
- }
- );
- jp.setLayout(new BorderLayout());
- jp.add(jb,BorderLayout.CENTER);
- this.add(jp);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.pack();
- this.setVisible(true);
- this.setLocationRelativeTo(null);
- this.setTitle("GUI15");
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(
- ()->new GUI17()
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement