0

I want to change the frame color with a button, without adding any panels.

How to do this?

This is my code:

import javax.swing.*;
import java.awt.*;

public class demo{

public static void main (String [] args ){

JFrame frame = new JFrame("Gui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400,400,400,300);
frame.setLayout(null);
frame.setVisible(true);

JButton butt = new JButton("Change Color");
butt.setBounds(50,50,150,30);
frame.add(butt);

}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    `frame.setBounds(400,400,400,300);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Sep 09 '15 at 13:41

2 Answers2

2

Add an ActionListener to the button.

butt.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        frame.getContentPane().setBackground(Color.BLACK);
    }
});

Btw, don't use setBounds() and null-layout, instead take a look at layout managers. You should also call setVisible() after you added all components, not before.

Full code:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class demo {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Gui");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton butt = new JButton("Change Color");
        butt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.getContentPane().setBackground(Color.BLACK);
            }
        });

        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(butt);

        frame.setSize(500, 500);
        frame.setVisible(true);

    }

}
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
  • Java Error Line 16 - local variable frame is accesed from withing inner class need to be declared final – Kasun Pradeep Sep 09 '15 at 14:02
  • Java Error Line 16 - local variable frame is accesed from withing inner class need to be declared final :( – Kasun Pradeep Sep 09 '15 at 14:06
  • @KasunPradeep Then try `final JFrame frame = new JFrame("Gui");` – Lukas Rotter Sep 09 '15 at 14:08
  • 3
    @KasunPradeep, `please give me full code bro` - that is not the way it works. You asked the question, so you make the effort to solve the problem. That is you post your code that shows how you attempted to solve the problem using the suggestion. This is called a [SSCCE](http://sscce.org/). We are not here to write code for you, only point you in the right direction. You don't learn anything if you don't try. – camickr Sep 09 '15 at 14:36
1

First you need to add an ActionListener to this. Otherwise it will no know what to do when you click the button. Below is a link that you will find useful.

https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

From there setting the background color is quite easy. Simply call something similar to this..

if(e.getSource() == myButtonName) {
    frame.getContentPane().setBackground(Color.BLUE);
}
Sh4d0wsPlyr
  • 948
  • 12
  • 28