actionListner code doesn't work ????
hi everyone
I am still new at learning java language
and I use eclipse program
but when I run this code it doesn't work
the code is :
Code :
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class coloredPanels extends JFrame implements ActionListener
{
private int width = 300;
private int height = 200;
private JPanel greenPanel;
private JPanel whitePanel;
private JPanel grayPanel;
public static void main(String[] args){
coloredPanels gui =new coloredPanels();
gui.setVisible(true);
}
public coloredPanels()
{
//we created the frame of the GUI
super("Panel for 3 colors");
setSize(width,height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//we created the biggest panel that has the other panels of each colored bars
JPanel biggerPanel = new JPanel();
biggerPanel.setLayout(new GridLayout(1,3));
//the first panel has created
greenPanel = new JPanel();
greenPanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(greenPanel);
//the second panel has created
whitePanel = new JPanel();
whitePanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(whitePanel);
//the third panel has created
grayPanel = new JPanel();
grayPanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(grayPanel);
//place the biggest panel with the 3 other panels at the center of the screen
add(biggerPanel,BorderLayout.CENTER);
//now we created panel for 3 buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.LIGHT_GRAY);
buttonPanel.setLayout(new FlowLayout());
JButton greenButton = new JButton("Green");
greenButton.setBackground(Color.GREEN);
greenButton.addActionlistner(this);
buttonPanel.add(greenButton);
JButton whiteButton = new JButton("White");
whiteButton.setBackground(Color.WHITE);
whiteButton.addActionlistner(this);
buttonPanel.add(whiteButton);
JButton grayButton = new JButton("Gray");
grayButton.setBackground(Color.GRAY);
grayButton.addActionlistner(this);
buttonPanel.add(grayButton);
add(buttonPanel,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e){
String buttonText = e.getActionCommand();
if(buttonText.equalsIgnoreCase("Green"))
greenPanel.setBackground(Color.GREEN);
else if(buttonText.equalsIgnoreCase("White"))
whitePanel.setBackground(Color.WHITE);
else if(buttonText.equalsIgnoreCase("Gray"))
greenPanel.setBackground(Color.GRAY);
else
System.out.println("error");
}
}
and the errors are :
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method addActionlistner(coloredPanels) is undefined for the type JButton
The method addActionlistner(coloredPanels) is undefined for the type JButton
The method addActionlistner(coloredPanels) is undefined for the type JButton
at coloredPanels.<init>(coloredPanels.java:67)
at coloredPanels.main(coloredPanels.java:23)
=======
if you plz help me
Re: actionListner code doesn't work ????
check your spelling against the names of the methods in the API doc for JButton
Re: actionListner code doesn't work ????
Quote:
Originally Posted by
Sean4u
check your spelling against the names of the methods in the API doc for JButton
thank you
but how can I check them ??
Re: actionListner code doesn't work ????
Quote:
but how can I check them ??
JButton (Java Platform SE 7 )
...copy...paste
Your post
...copy...paste
...edit a bit to get one above the other...
Compare the two strings of characters one-character-at-a-time, like a computer would. Are they the same?
Re: actionListner code doesn't work ????
Quote:
Originally Posted by
Sean4u
JButton (Java Platform SE 7 )
...copy...paste
Your post
...copy...paste
...edit a bit to get one above the other...
Compare the two strings of characters one-character-at-a-time, like a computer would. Are they the same?
I will try
thanks alot