How to change the background colour by clicking buttons?
Hi guys
Below code mean to change the background colour by clicking the redButton or blueButton, but the code doesn't work, (only can show the frame and two buttons), does any anyone know why?
Thanks heaps
Code java:
package brainydraw;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PictureButton extends JFrame
{
private JButton redButton;
private JButton blueButton;
public PictureButton()
{
setSize(200, 200);
setLocation(200, 200);
redButton = new JButton("RED");
blueButton = new JButton("BLUE");
Container content = getContentPane();
content.setLayout(new FlowLayout());
content.add(redButton);
content.add(blueButton);
//register the current panel as listener for the buttons
ActionListener rBListener = new RedAndBlueButtonListener();
redButton.addActionListener(rBListener);
blueButton.addActionListener(rBListener);
}
public class RedAndBlueButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Color color = getBackground(); // color will be set
Object source = ae.getSource();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
setBackground(color);
repaint();
}
}
public static void main(String[] args)
{
JFrame f = new PictureButton();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
f.setVisible(true);
}
}
Re: How to change the background colour by clicking buttons?
Can you elaborate on what you mean by 'code doesn't work'?
Re: How to change the background colour by clicking buttons?
You're setting the background of a JFrame. But the background of a JFrame is covered by its contentPane, which is by default opaque. You should be setting the background of the contentPane.
Oh, and it's never necessary to explicitly call repaint() after changing the background of a Swing component.
db
Re: How to change the background colour by clicking buttons?
Hi db
Thank you for your suggestion, it makes great sense, I guess below part of code caused the colour to be set on the frame instead of the contenPanel, am I right? But how exactly do I modify it?
Thanks again
Code java:
public class RedAndBlueButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Color color = getBackground(); // color will be set
Object source = ae.getSource();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
setBackground(color);
repaint();
}
}
Re: How to change the background colour by clicking buttons?
Quote:
Originally Posted by
copeg
Can you elaborate on what you mean by 'code doesn't work'?
Hi copeg
I got a problem that I when I clicked the buttons, the colours of the background is not changed accordingly as it suposed to be, db suggest the colour is set on the frame which is hidden behind the panel, so is there any way I can dispaly colour without using panel?
Thanks heaps
Re: How to change the background colour by clicking buttons?
Code :
frame.getContentPane().setBackground(...)
db
Re: How to change the background colour by clicking buttons?
Thanks db
I have added getContentPane(). in front of setBackground(color) asp below code, then ran it, but got a compiling error:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java: 431)
at java.awt.Container.addImpl(Container.java:1039)
at java.awt.Container.add(Container.java:365)
at brainydraw.PictureButton.main(PictureButton.java:7 8)
Java Result: 1
Any more thought?
Code java:
class RedAndBlueButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Color color = getBackground(); // color will be set
Object source = ae.getSource();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
getContentPane().setBackground(color);
repaint();
}
}
Re: How to change the background colour by clicking buttons?
If you want help, you'll have to post an SSCCE that demonstrates the problem. The code you posted doesn't seem to have anything to do with the error you posted.
Re: How to change the background colour by clicking buttons?
Hi Kevinworkman
Thanks for the advice, yes I need the help if you can give some. My whole code is asp below, it doesn't compile after I added " getContentPane" into the method "public class RedAndBlueButtonListener implements ActionListener". All I wanted to do is to click the buttons to change background colour.
Any idea?
Code java:
package brainydraw;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PictureButton extends JFrame
{
private JButton redButton;
private JButton blueButton;
public PictureButton()
{
setSize(200, 200);
setLocation(200, 200);
redButton = new JButton("RED");
blueButton = new JButton("BLUE");
Container content = getContentPane();
content.setLayout(new FlowLayout());
content.add(redButton);
content.add(blueButton);
//register the current panel as listener for the buttons
ActionListener rBListener = new RedAndBlueButtonListener();
redButton.addActionListener(rBListener);
blueButton.addActionListener(rBListener);
}
public class RedAndBlueButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Color color = getBackground(); // color will be set
Object source = ae.getSource();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
getContentPane.setBackground(color);
repaint();
}
}
public static void main(String[] args)
{
JFrame f = new PictureButton();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
f.setVisible(true);
}
}
Re: How to change the background colour by clicking buttons?
Hi guys
Thank you all, I just solved this problem. hehe...
:-bd
Re: How to change the background colour by clicking buttons?
getContentPane is a method, but you are writing code that treats it as a variable. Go back to the one line of code I posted and see how your code differs from it.
edit The earlier method call in the constructor is correct. The mistake I refer to is in the actionPerformed(...)
Also, learn to respect Swing's single threaded rule and create and modify Swing components only on the EDT.
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db