Cypher
Cypher

Reputation: 39

How to change Background color when i click a button

My Problem is only just a little part that change color.I want to change the whole background when i click those buttons.I search already in google nothing happens.

I Use Panel but it seems not only a little part that it can changes i want a whole background.

import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import java.awt.event.*;
/**
 *
 * 
 * @author Christopher Porras
 * @Version 0.1
 * @Doing GUI
 */
public class Button extends JFrame {

   private JButton bred;
   private JButton bblue;
   private JButton bgreen;
   private JPanel mousepanel; 


    public Button()
    {
        super("ChangeColor");
        setLayout(new FlowLayout());
        setSize(200,200);

        mousepanel = new JPanel();
        mousepanel.setBackground(Color.white);
        add(mousepanel);

        bred = new JButton("REd");
        add(bred);

        bblue = new JButton("Blue");
        add(bblue);

        bgreen = new JButton("Green");
        add(bgreen);

        thehandler handler = new thehandler();
        bred.addMouseListener(handler);
        bblue.addMouseListener(handler);
        bgreen.addMouseListener(handler);
    }

    private class thehandler implements MouseListener
    {


        public void mouseClicked(MouseEvent e)
        {
            if(e.getSource()==bred)
            {
                mousepanel.setBackground(Color.red);
            }

             else if(e.getSource()==bblue)
            {
                mousepanel.setBackground(Color.blue);
            }

             else if(e.getSource()==bgreen)
            {
                mousepanel.setBackground(Color.green);
            }
        }


        public void mousePressed(MouseEvent e) {

        }

        public void mouseReleased(MouseEvent e) {

        }


        public void mouseEntered(MouseEvent e) {

        }


        public void mouseExited(MouseEvent e) {
              //To change body of generated methods, choose Tools | Templates.
        }



    }

    public static void main(String[]args)
    {
        Button button = new Button();
        button.setVisible(true);
        button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

Upvotes: 0

Views: 14756

Answers (1)

andrewdleach
andrewdleach

Reputation: 2466

You're on the right track, but you need to tie the events to the button components rather than the interrupt signals of the mouse.

Instead of a MouseListener, change theHandler to this:

  public class TheActionHandler implements ActionListener {

      @Override
      public void actionPerformed(final ActionEvent e) {
           if(e.getSource()==bred)
        {
            mousepanel.setBackground(Color.red);
        }

         else if(e.getSource()==bblue)
        {
            mousepanel.setBackground(Color.blue);
        }

         else if(e.getSource()==bgreen)
        {
            mousepanel.setBackground(Color.green);
        }
      }
  }

Upvotes: 5

Related Questions