السلام عليكم
هذا كود يقوم بتصميم واجهة مكونة من ثلاثة اعمدة كل منها بلون مختلف
وبثلاثة ازرار لها نفس الوان الاعمدة
ولكن ظهرت لي مشاكل في التنفيذ
هذا هو الكود
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
{
public static final int width = 300;
public static final 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()
{
super("Panel for 3 colors");
setSize(width,height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel biggerPanel = new JPanel();
biggerPanel.setLayout(new GridLayout(1,3));
greenPanel = new JPanel();
greenPanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(greenPanel);
whitePanel = new JPanel();
whitePanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(whitePanel);
grayPanel = new JPanel();
grayPanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(grayPanel);
add(biggerPanel,BorderLayout.CENTER);
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");
}
}استخدم برنامج eclipse
وهذه هي الاخطاء التي ظهرت لنا :
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)
كيف اقوم بتعريف دالة addActionListner من نوع الكلاس JButton
وجزاكم الله خير