هنا استكمالا على code آخر مشاركة ليَّ - لكن لأن المشكلة مختلفة فضلت وضعها فى موضوع جديد -
---------------------------------------------------------------
عندنا أضغط على الزر level يعرض لى ال levelPanel و لكن لا تكون ملونة باللون الأحمر لماذا ذلك فى حين أنى فى الدالة actionPerformed عملت call للدوال المختصة بتلوين ال panel
ال code :
-----------------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainFram extends JFrame {
private JPanel mainPanel, helpPanel;
private JButton[] buttons;
private JLabel[] labels;
private String[] labelNames = {"Levels", "Help", "About"};
private String[] label = new String[labelNames.length];
private static final int BUTTON_DISTANCE_X = 20, BUTTON_DISTANCE_Y = 20, LABELS_DISTANCE_X = 100, LABEL_DISTANCE_Y = 20;
private static final int BUTTON_WIDTH = 50, BUTTON_HEIGHT = 50, LABEL_WIDTH = 75, LABEL_HEIGHT = 50;
private MyActionListener myActionListener = new MyActionListener();
private LevelPanel levelPanel = new LevelPanel();
public MainFram() {
intializingPanel();
intializingButtons();
intializingLabel();
getContentPane().add(mainPanel);
}
public static void main(String[] args) {
MainFram mainFram = new MainFram();
mainFram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFram.setSize(new Dimension(600, 300));
mainFram.setLocationRelativeTo(null);
mainFram.setVisible(true);
// mainFram.setExtendedState(MAXIMIZED_BOTH);
}
private void intializingPanel() {
mainPanel = new JPanel(null);
mainPanel.setBounds(100, 10, 600, 300);
mainPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
mainPanel.setBackground(Color.GREEN);
/////////////////levelPanel/////////////////////////////
mainPanel.add(levelPanel);
/////////////////helpPanel//////////////////////////////
helpPanel = new JPanel();
helpPanel.setBounds(100, 10, 600, 300);
helpPanel.setBackground(Color.BLUE);
helpPanel.setVisible(false);
mainPanel.add(helpPanel);
////////////////////////////////////////////////////////
}
private void intializingButtons() {
buttons = new JButton[labelNames.length];
for (int i = 0; i < labelNames.length; ++i) {
buttons = new JButton();
buttons.setBounds(BUTTON_DISTANCE_X, ((BUTTON_DISTANCE_Y + BUTTON_HEIGHT) * i) + BUTTON_DISTANCE_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
buttons.addActionListener(myActionListener);
mainPanel.add(buttons);
}
}
private void intializingLabel() {
labels = new JLabel[labelNames.length];
for (int i = 0; i < labelNames.length; ++i) {
label = labelNames;
String htmlLabel = "<html><body><h1>" + label + "</h1></body></html>";
labels = new JLabel(htmlLabel);
labels.setBounds(LABELS_DISTANCE_X, ((LABEL_DISTANCE_Y + LABEL_HEIGHT) * i) + LABEL_DISTANCE_Y, LABEL_WIDTH, LABEL_HEIGHT);
mainPanel.add(labels);
}
}
private class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttons[0]) {
levelPanel.setSizePanel();
levelPanel.setColorPanel(Color.RED);
Color c = levelPanel.getColorPanel();
levelPanel.setBackground(c);
levelPanel.setVisiblePanel();
mainPanel.setVisible(false);
}
}
}
}ال class levelPane;
--------------------------------
import java.awt.Color;
import javax.swing.JPanel;
public class LevelPanel extends JPanel {
private boolean visible = false;
private Color color;
public void setSizePanel() {
setBounds(100, 10, 600, 300);
}
public void setColorPanel(Color color) {
this.color = color;
}
public Color getColorPanel() {
return color;
}
public boolean setVisiblePanel() {
visible = true;
return visible;
}
}شكراً