[JAVA] probleme affichage/suppression JPanel

Fermé
poblin - 25 août 2008 à 18:06
mat. Messages postés 150 Date d'inscription vendredi 27 juin 2008 Statut Membre Dernière intervention 1 septembre 2009 - 14 sept. 2008 à 19:01
Bonjour,
j'ai construit une application Java avec Eclipse qui affiche des JPanel de couleurs différentes en fonction du bouton que l'on presse.
Le comportement que je souhaite avoir est:
- si on appuie sur le bouton1, l'affichage alterne entre un JPanel rouge et un JPanel vert
- si on appuie sur le bouton2, l'affichage alterne entre un JPanel jaune et un JPanel noir
Le bouton1 marche bien.
Par contre, le bouton2 parfois ne répond pas ou bien affiche des JPanel jaune et noir à la fois. Pourtant mon code est completement symetrique entre les bouton 1 et 2.
Pour info, j'utilise un GridBagLayout.

Voici mon code (une classe test1):



import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test1 implements ActionListener {

private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="17,19"
private JPanel jContentPane = null;
private JPanel jPanelPassenger = null;
private JPanel jEmptyPanelAirSegment = new JPanel();
private JPanel jEmptyPanelPassenger = new JPanel();
private JPanel jPanelAirSegment = null;
private JButton jButtonPassenger = null;
private GridBagLayout jGridBagLayout = null;
private JButton jButtonAirSegment = null;
private GridBagConstraints gridBagConstraintsPassenger = null;
private GridBagConstraints gridBagConstraintsAirSegment = null;

boolean aPassengerTableIsVisible = true;
boolean aAirSegmentTableIsVisible = true;


public test1()
{
jEmptyPanelAirSegment.setPreferredSize(new Dimension(300, 20));
jEmptyPanelAirSegment.setBackground(Color.black);
jEmptyPanelPassenger.setPreferredSize(new Dimension(300, 20));
jEmptyPanelPassenger.setBackground(Color.green);
}

/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new java.awt.Dimension(413,236));
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {

GridBagConstraints gridBagConstraintsJButtonAirSegment = new GridBagConstraints();
gridBagConstraintsJButtonAirSegment.gridx = 0;
gridBagConstraintsJButtonAirSegment.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraintsJButtonAirSegment.gridy = 2;

GridBagConstraints gridBagConstraintsJButtonPassenger = new GridBagConstraints();
gridBagConstraintsJButtonPassenger.gridx = 0;
gridBagConstraintsJButtonPassenger.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraintsJButtonPassenger.gridy = 0;

gridBagConstraintsAirSegment = new GridBagConstraints();
gridBagConstraintsAirSegment.gridx = 1;
gridBagConstraintsAirSegment.gridy = 3;
gridBagConstraintsAirSegment.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraintsAirSegment.anchor = java.awt.GridBagConstraints.CENTER;
gridBagConstraintsAirSegment.weightx = 1.0D;
gridBagConstraintsAirSegment.weighty = 1.0D;


gridBagConstraintsPassenger = new GridBagConstraints();
gridBagConstraintsPassenger.gridx = 1;
gridBagConstraintsPassenger.gridy = 1;
gridBagConstraintsPassenger.fill = java.awt.GridBagConstraints.HORIZONTAL;
//gridBagConstraintsPassenger.gridheight = 1;
//gridBagConstraintsPassenger.gridwidth = 1;



jContentPane = new JPanel();
jGridBagLayout = new GridBagLayout();
jContentPane.setLayout(jGridBagLayout);
jContentPane.add(getJPanelPassenger(), gridBagConstraintsPassenger);
jContentPane.add(getJPanelAirSegment(), gridBagConstraintsAirSegment);
jContentPane.add(getJButtonPassenger(), gridBagConstraintsJButtonPassenger);
jContentPane.add(getJButtonAirSegment(), gridBagConstraintsJButtonAirSegment);
jContentPane.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

}
return jContentPane;
}

/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanelPassenger() {
if (jPanelPassenger == null) {
jPanelPassenger = new JPanel();
jPanelPassenger.setBackground(new java.awt.Color(255,51,51));
jPanelPassenger.setPreferredSize(new java.awt.Dimension(300,100));
}
return jPanelPassenger;
}

/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanelAirSegment() {
if (jPanelAirSegment == null) {
jPanelAirSegment = new JPanel();
jPanelAirSegment.setBackground(new java.awt.Color(255,255,51));
}
return jPanelAirSegment;
}

private static void createAndShowGUI() {
//Create and set up the window.
test1 instance = new test1();
JFrame frame = instance.getJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the content pane.
//addComponentsToPane(frame.getContentPane());

//Display the window.
frame.pack();
frame.setVisible(true);
}



/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButtonPassenger() {
if (jButtonPassenger == null) {
jButtonPassenger = new JButton("1");
jButtonPassenger.setPreferredSize(new java.awt.Dimension(44,30));
jButtonPassenger.setVerticalAlignment(javax.swing.SwingConstants.TOP);
jButtonPassenger.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jButtonPassenger.addActionListener(this);
}
return jButtonPassenger;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jButtonPassenger)
{
System.out.println("aPassengerTableIsVisible valait: "+aPassengerTableIsVisible);
if(aPassengerTableIsVisible == true)
{
jContentPane.remove(jPanelPassenger);
jContentPane.add(jEmptyPanelPassenger, gridBagConstraintsPassenger);
jContentPane.validate();
aPassengerTableIsVisible = false;
System.out.println("aPassengerTableIsVisible vaut: "+aPassengerTableIsVisible);

}

else
{
jContentPane.remove(jEmptyPanelPassenger);
jContentPane.add(jPanelPassenger, gridBagConstraintsPassenger);
jContentPane.validate();
aPassengerTableIsVisible = true;
System.out.println("aPassengerTableIsVisible vaut: "+aPassengerTableIsVisible);

}

}
if (e.getSource() == jButtonAirSegment)
{
System.out.println("aAirSegmentTableIsVisible valait: "+aAirSegmentTableIsVisible);
if(aAirSegmentTableIsVisible == true)
{
jContentPane.remove(jPanelAirSegment);
jContentPane.add(jEmptyPanelAirSegment, gridBagConstraintsAirSegment);
jContentPane.validate();
aAirSegmentTableIsVisible = false;
System.out.println("aAirSegmentTableIsVisible vaut: "+aAirSegmentTableIsVisible);

}
//aAirSegmentTableIsVisible = !aAirSegmentTableIsVisible;

else
{
jContentPane.remove(jEmptyPanelAirSegment);
jContentPane.add(jPanelAirSegment, gridBagConstraintsAirSegment);
jContentPane.validate();
aAirSegmentTableIsVisible = true;
System.out.println("aAirSegmentTableIsVisible vaut: "+aAirSegmentTableIsVisible);

}

}
}

/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButtonAirSegment() {
if (jButtonAirSegment == null) {
jButtonAirSegment = new JButton("2");
jButtonAirSegment.setPreferredSize(new java.awt.Dimension(44,30));
jButtonAirSegment.setMnemonic(java.awt.event.KeyEvent.VK_UNDEFINED);
jButtonAirSegment.addActionListener(this);
}
return jButtonAirSegment;
}

public static void main(String[] args)
{
createAndShowGUI();
}

}
A voir également:

1 réponse

mat. Messages postés 150 Date d'inscription vendredi 27 juin 2008 Statut Membre Dernière intervention 1 septembre 2009 21
14 sept. 2008 à 19:01
Bonjour,
C'est mieux comme ça :

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

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

@SuppressWarnings("serial")
public class test1 extends JFrame implements ActionListener {

	JPanel p1;
	JPanel p2;

	JButton b1;
	JButton b2;

	boolean colorP1;
	boolean colorP2;

	public test1() {
		p1 = new JPanel();
		p1.setBackground(Color.RED);
		b1 = new JButton("b1");
		b1.addActionListener(this);
		p1.add(b1);
		add(p1, "North");

		p2 = new JPanel();
		p2.setBackground(Color.YELLOW);
		b2 = new JButton("b2");
		b2.addActionListener(this);
		p2.add(b2);
		add(p2, "South");

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(200, 200);
		setVisible(true);
	}

	public void colorPanel1() {
		if (colorP1) {
			p1.setBackground(Color.GREEN);
		} else {
			p1.setBackground(Color.RED);
		}
	}

	public void colorPanel2() {
		if (colorP2) {
			p2.setBackground(Color.BLACK);
		} else {
			p2.setBackground(Color.YELLOW);
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b1) {
			if (colorP1) {
				colorP1 = false;
			} else {
				colorP1 = true;
			}
			colorPanel1();
		} else if (e.getSource() == b2) {
			if (colorP2) {
				colorP2 = false;
			} else {
				colorP2 = true;
			}
			colorPanel2();
		}
	}

	public static void main(String[] args) {
		new test1();
	}
}
1