Partage
  • Partager sur Facebook
  • Partager sur Twitter

Des JButton invisible avant mouse over

    20 juin 2017 à 19:55:57

    Bonjour à tous/toutes!

    Je suis un humble apprenti programmeur essayant de réaliser une interface graphique pour un jeu d'Othello.

    J'ai donc réalisé un gridLayout rempli de boutons et pour l'instant tout à l'air de bien fonctionner, mis à part le fait que tout mes boutons sont invisibles avant que je passe ma souris dessus.

    Je ne saisis pas bien pourquoi et ai essayer de mettre des setvisible(ru) un peu partout, de change l'ordre d'addition de mes JPanel mais rien n'y fait.

    Voici mon code actuel :

    package ihm;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    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;
    
    import jeu.OthelloProf;
    import jeu.OthelloProf.*;
    
    @SuppressWarnings("serial")
    public class FrameOthello extends JFrame {
    	
    	private OthelloProf jeu;
    	private JPanel Aled = new JPanel();
    
    	
    	public FrameOthello(int nbLig, int nbCol) {
    
    		super("Othello "+nbLig+"x"+nbCol);
    		
    		this.jeu = new OthelloProf(nbLig, nbCol);
    		
    		Aled.setLayout(new BorderLayout());
    		
    		JPanel panel = new JPanel();
    		panel.setBorder( 
    				BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("") ,
    													BorderFactory.createEmptyBorder(50, 50, 50, 50)    ) );
    		panel.setLayout(new BorderLayout());
    		
    		JLabel LeTour = new JLabel(CAKi());
    		
    		Aled.add(LeTour, BorderLayout.SOUTH);
    		Aled.add(nbNoir(nbLig), BorderLayout.EAST);
    		Aled.add(nbBlanc(nbLig), BorderLayout.WEST);
    		
    		panel.add(MAJ(nbLig, nbCol));
    		Aled.add( panel );
    		
    		this.add(Aled);		
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		this.pack();
    		
    	}
    	
    	public JLabel nbNoir(int nbLig){
    		
    		int nb = 0;
    		Pion LePion;
    		
    		for(int i = 0; i < nbLig; i++){
    			
    			for(int j = 0; j < nbLig; j++){
    				
    				LePion = jeu.getCase(i, j);
    				
    				if(LePion == Pion.NOIR){
    					nb ++;
    				}
    			}
    		}
    		
    		JLabel LeNoir = new JLabel("Score des noirs :"+nb);
    		
    		return LeNoir;
    		
    	}
    	
    public JLabel nbBlanc(int nbLig){
    		
    		int nb = 0;
    		Pion LePion;
    		
    		for(int i = 0; i < nbLig; i++){
    			
    			for(int j = 0; j < nbLig; j++){
    				
    				LePion = jeu.getCase(i, j);
    				
    				if(LePion == Pion.BLANC){
    					nb ++;
    				}
    			}
    		}
    		
    		JLabel LeBlanc = new JLabel("Score des blancs : "+nb);
    		
    		return LeBlanc;
    		
    	}
    	
    	public String CAKi(){
    		
    		if(jeu.isJoueurBlanc()){
    			return("Tour des blancs");
    		} else {
    			return("Tour des noirs");
    		}
    		
    	}
    	
    	public JPanel MAJ(int nbLig, int nbCol){
    		
    		JPanel grid = new JPanel();
    		grid.setLayout(new GridLayout(nbLig, nbCol));
    		
    		Dimension Dim = new Dimension(50,50);
    		
    		Color blanc = new Color(255,255,255);
    		Color noir = new Color(100,100,100);
    		Color neutre = new Color(175,175,175);
    		Color Test = new Color(255,0,0);
    		
    		Pion LePion;
    		
    		for(int i = 0; i < nbLig; i++){
    			
    			for(int j = 0; j < nbLig; j++){
    				
    				LePion = jeu.getCase(i, j);
    				
    				if(LePion == null){
    					if (jeu.testerCase(i, j)){
    						Butt cmoa = new Butt(i,j, nbLig);
    						cmoa.setBackground(Test);
    						cmoa.setPreferredSize(Dim);
    						cmoa.addActionListener(new Progres());
    						grid.add(cmoa);
    						cmoa.setVisible(true);
    					} else {
    						Butt cmoa = new Butt(i,j, nbLig);
    						cmoa.setBackground(neutre);
    						cmoa.setPreferredSize(Dim);
    						cmoa.addActionListener(new Progres());
    						grid.add(cmoa);
    						cmoa.setVisible(true);
    					}
    				}
    				if(LePion == Pion.NOIR){
    					Butt cmoa = new Butt(i,j, nbLig);
    					cmoa.setBackground(noir);
    					cmoa.setPreferredSize(Dim);
    					cmoa.addActionListener(new Progres());
    					grid.add(cmoa);
    					cmoa.setVisible(true);
    				}
    				if(LePion == Pion.BLANC){
    					Butt cmoa = new Butt(i,j, nbLig);
    					cmoa.setBackground(blanc);
    					cmoa.setPreferredSize(Dim);
    					cmoa.addActionListener(new Progres());
    					grid.add(cmoa);
    					cmoa.setVisible(true);
    				}	
    			}
    		}
    		
    		return grid;
    		
    	}
    	
    	public void LeDeluge(int nbLig, int nbCol){
    		
    		Aled.removeAll();
    		
    		JPanel panel = new JPanel();
    		panel.setBorder( 
    				BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("") ,
    													BorderFactory.createEmptyBorder(50, 50, 50, 50)    ) );
    		panel.setLayout(new BorderLayout());
    		
    		JLabel LeTour = new JLabel(CAKi());
    		
    		
    		Aled.add(LeTour, BorderLayout.SOUTH);
    		Aled.add(nbNoir(nbLig), BorderLayout.EAST);
    		Aled.add(nbBlanc(nbLig), BorderLayout.WEST);
    		Aled.add( panel );
    		panel.add(MAJ(nbLig, nbCol));
    		
    		this.add(Aled);
    		
    	}
    	
    	class Progres implements ActionListener{
    		
    		private int x, y, slt;
    		
    		public void actionPerformed(ActionEvent e){
    			Butt tmp = (Butt) e.getSource();
    			x = tmp.getX();
    			y = tmp.getY();
    			slt = tmp.getLig();
    						
    			jeu.jouer(x, y);
    			
    			LeDeluge(slt, slt);
    			
    		}
    		
    	}
    
    }
    

    Je remercie en avance ceux qui pourront m'apporter d'éventuelles solutions.
    • Partager sur Facebook
    • Partager sur Twitter
      20 juin 2017 à 20:54:48

      Bonjour,

      Il manque la classe concernée (Butt).

      • Partager sur Facebook
      • Partager sur Twitter
        20 juin 2017 à 21:34:55

        Je ne l'ai pas mise car elle ne me semblait pas importante, mais la voici :

        package ihm;
        
        import javax.swing.JButton;
        
        public class Butt extends JButton{
        	
        	private int x, y, lig;
        	
        	public Butt(int a, int b){
        		
        		this.x = a;
        		this.y = b;
        		
        	}
        	
        public Butt(int a, int b, int nb){
        		
        		this.x = a;
        		this.y = b;
        		this.lig = nb;
        		
        	}
        	
        	public int getX(){
        		
        		return this.x;
        		
        	}
        	
        	public int getY(){
        		
        		return this.y;
        		
        	}
        	
        	public int getLig(){
        		
        		return this.lig;
        		
        	}
        
        }
        
        X et y représentent les coordonnées du boutons et lig me sert à garder les dimensions quelques part.

        • Partager sur Facebook
        • Partager sur Twitter
          23 juin 2017 à 17:34:18

          Problème résolu, le programme mettait les boutons dans un endroit farfelu hors champ, les rendant donc "invisible" à vause de ma Classe de bouton Butt, en particulier els méthodes getX et getY que j'avais redefini sans le savoir. En changeant le nom de ces méthodes tout est redevenu normal.
          • Partager sur Facebook
          • Partager sur Twitter

          Des JButton invisible avant mouse over

          × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
          × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
          • Editeur
          • Markdown