Bugs à l'affichage avec set Bounds [java] [Fermé]

-
Bonjour, j'ai utilisé des setBounds pour afficher des composants en java.

Le problème que j'ai rencontré plusieurs fois est qu'à l'affichage, les composants ne s'affichaient pas correctement quand on les faisait bouger.

Par exemple, un composant dont la taille et la position sont définies par rapport à la taille de la fenêtre, change quand on change la taille de la fenêtre.

Dans mon cas, il se dédouble parfois ou ne s'affiche pas comme voulu par les coordonnées du setBound.

Pour remédier à ce problème, j'ai trouvé un astuce qui consiste à redimensionner la fenêtre. Cette astuce ne corrige pas toujours le bug et reste peu pratique à utiliser.

Y'a-t-il une autre façon de corriger ce bug?

Voici un code qui bug de cette manière :


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class ScrollBar extends JFrame {
 public ScrollBar (String titre) {
  super(titre);

  this.add(new P2(this));

  //this.add(new P2(this.getWidth(),this.getHeight()));
  
  //this.setMinimumSize(new Dimension(400, 300));
  this.setPreferredSize(new Dimension(400, 300));

  pack();

  this.setLocationRelativeTo(null);
  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  this.setVisible(true);
 }

 public void reDim1 () {
  boolean dd = (getExtendedState()==this.MAXIMIZED_BOTH);

  int getWidth = this.getWidth();
  int getHeight = this.getHeight();

  resize(new Dimension(getWidth+1,getHeight+1));
  resize(new Dimension(getWidth,getHeight));

  if (dd) {
   this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  }
 }

 public void reDim () {

 }

 public static void main(String [] args) {
  ScrollBar f = new ScrollBar("Fenetre 2");
 }
}

class P1 extends JPanel {
 ScrollBar f;
 int x;
 int y;

 public P1 (ScrollBar f) {
  super();

  this.f = f;

  setLayout(null);

  //this.setPreferredSize(new Dimension(f.getWidth(),f.getHeight()));
 }
 
 public void paintComponent (Graphics g) {
  g.setColor(new Color(-7960954));
  g.fillRect(0,0,this.getWidth(),this.getHeight());
 }
}

class P2 extends JPanel {
 ScrollBar f;
 P1 p1;
 int x;
 int y;

 boolean pressed;
 boolean pressed0;

 ActionListenerSB [] alsb;
 ActionSouris [] as;

 public P2 (ScrollBar f) {
  super();

  int i;

  setLayout(null);

  this.f = f;

  //x=0;
  //y=0;

  pressed = false;

  p1 = new P1(f);

  this.setPreferredSize(new Dimension(f.getWidth(),f.getHeight()));

  alsb = new ActionListenerSB [2];
  as = new ActionSouris[2];

  for (i=0; i<2; i++) {
   alsb [i] = new ActionListenerSB(i);
   as [i] = new ActionSouris(i);
  }


  p1.addMouseListener(alsb[0]);
  p1.addMouseMotionListener(as[0]);

  this.addMouseListener(alsb[1]);
  this.addMouseMotionListener(as[1]);

  add(p1);
 }

 class ActionListenerSB implements MouseListener {
  int n;
  public ActionListenerSB (int n) {
   this.n = n;
  }
  
  public void mousePressed(MouseEvent e)  {
   if (n==1) pressed = true;
   if (n==0) pressed0 = true;

   int getWidth = getWidth();
   int getHeight = getHeight();

   if (pressed) {
    if(n==1) {
     x = (int)(e.getPoint().getX());
     y = (int)(e.getY());
   
     System.out.println("x = "+x+"\ny = "+y);
   
     repaint();
  
     f.reDim();
    }
   }

   if (pressed0) {
    if(n==1) {
     if (x>x-getWidth/15/2) x = x-(x-getWidth/15/2)-(int)(e.getPoint().getX());
     else x = x+(int)(e.getPoint().getX());
     if (y>y-getHeight/3/2) y = y-(y-getHeight/3/2)-(int)(e.getY());
     else y = y+(int)(e.getY());
   
     System.out.println("x = "+x+"\ny = "+y);
   
     repaint();
  
     f.reDim();
    }
   }
  }
  
  public void mouseClicked(MouseEvent e) {
   System.out.print("");
  }

  public void mouseEntered(MouseEvent e) {
   System.out.print("");
  }

  public void mouseExited(MouseEvent e) {
   System.out.print("");
  }

  public void mouseReleased(MouseEvent e) {
   if (n==1) pressed = false;
   if (n==0) pressed0 = false;
  }
 }

 class ActionSouris implements MouseMotionListener {
  int n;

  public ActionSouris (int n) {
   this.n = n;
  }

  public void mouseDragged(MouseEvent e) {
   int getWidth = getWidth();
   int getHeight = getHeight();

   if (pressed) {
    if(n==1) {
     x = (int)(e.getPoint().getX());
     y = (int)(e.getY());
   
     System.out.println("x = "+x+"\ny = "+y);
   
     repaint();
  
     f.reDim();
    }
   }

   if (pressed0) {
    if(n==1) {
     if (x>x-getWidth/15/2) x = x-(x-getWidth/15/2)-(int)(e.getPoint().getX());
     else x = x+(int)(e.getPoint().getX());
     if (y>y-getHeight/3/2) y = y-(y-getHeight/3/2)-(int)(e.getY());
     else y = y+(int)(e.getY());
   
     System.out.println("x = "+x+"\ny = "+y);
   
     repaint();
  
     f.reDim();
    }
   }

   System.out.print("blabla");  
  }

  public void mouseMoved(MouseEvent e) {
   System.out.print("");
  }
 }
 
 public void paintComponent (Graphics g) {
  int getWidth = this.getWidth();
  int getHeight = this.getHeight();

  /*if (x>getWidth) x=getWidth;
  if (y>getHeight) y=getHeight;
  if (x<getWidth) x=0;
  if (y<getHeight) y=0;*/

  g.setColor(new Color(-7960954));
  p1.setBounds(x-getWidth/15/2,y-getHeight/3/2,getWidth/15,getHeight/3);
 }
}

Afficher la suite 

2 réponses

0
Merci
Et voici le code quand on redimensionne dynamiquement la fenêtre :

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class ScrollBar extends JFrame {
	public ScrollBar (String titre) {
		super(titre);

		this.add(new P2(this));

		//this.add(new P2(this.getWidth(),this.getHeight()));
		
		//this.setMinimumSize(new Dimension(400, 300));
		this.setPreferredSize(new Dimension(400, 300));

		pack();

		this.setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public void reDim () {
		boolean dd = (getExtendedState()==this.MAXIMIZED_BOTH);

		int getWidth = this.getWidth();
		int getHeight = this.getHeight();

		resize(new Dimension(getWidth+1,getHeight+1));
		resize(new Dimension(getWidth,getHeight));

		if (dd) {
			this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		}
	}

	public void reDim1 () {

	}

	public static void main(String [] args) {
		ScrollBar f = new ScrollBar("Fenetre 2");
	}
}

class P1 extends JPanel {
	ScrollBar f;
	int x;
	int y;

	public P1 (ScrollBar f) {
		super();

		this.f = f;

		setLayout(null);

		//this.setPreferredSize(new Dimension(f.getWidth(),f.getHeight()));
	}
	
	public void paintComponent (Graphics g) {
		g.setColor(new Color(-7960954));
		g.fillRect(0,0,this.getWidth(),this.getHeight());
	}
}

class P2 extends JPanel {
	ScrollBar f;
	P1 p1;
	int x;
	int y;

	boolean pressed;
	boolean pressed0;

	ActionListenerSB [] alsb;
	ActionSouris [] as;

	public P2 (ScrollBar f) {
		super();

		int i;

		setLayout(null);

		this.f = f;

		//x=0;
		//y=0;

		pressed = false;

		p1 = new P1(f);

		this.setPreferredSize(new Dimension(f.getWidth(),f.getHeight()));

		alsb = new ActionListenerSB [2];
		as = new ActionSouris[2];

		for (i=0; i<2; i++) {
			alsb [i] = new ActionListenerSB(i);
			as [i] = new ActionSouris(i);
		}


		p1.addMouseListener(alsb[0]);
		p1.addMouseMotionListener(as[0]);

		this.addMouseListener(alsb[1]);
		this.addMouseMotionListener(as[1]);

		add(p1);
	}

	class ActionListenerSB implements MouseListener {
		int n;
		public ActionListenerSB (int n) {
			this.n = n;
		}
		
		public void mousePressed(MouseEvent e)  {
			if (n==1) pressed = true;
			if (n==0) pressed0 = true;

			int getWidth = getWidth();
			int getHeight = getHeight();

			if (pressed) {
				if(n==1) {
					x = (int)(e.getPoint().getX());
					y = (int)(e.getY());
			
					System.out.println("x = "+x+"\ny = "+y);
			
					repaint();
		
					f.reDim();
				}
			}

			if (pressed0) {
				if(n==1) {
					if (x>x-getWidth/15/2) x = x-(x-getWidth/15/2)-(int)(e.getPoint().getX());
					else x = x+(int)(e.getPoint().getX());
					if (y>y-getHeight/3/2) y = y-(y-getHeight/3/2)-(int)(e.getY());
					else y = y+(int)(e.getY());
			
					System.out.println("x = "+x+"\ny = "+y);
			
					repaint();
		
					f.reDim();
				}
			}
		}
		
		public void mouseClicked(MouseEvent e) {
			System.out.print("");
		}

		public void mouseEntered(MouseEvent e) {
			System.out.print("");
		}

		public void mouseExited(MouseEvent e) {
			System.out.print("");
		}

		public void mouseReleased(MouseEvent e) {
			if (n==1) pressed = false;
			if (n==0) pressed0 = false;
		}
	}

	class ActionSouris implements MouseMotionListener {
		int n;

		public ActionSouris (int n) {
			this.n = n;
		}

		public void mouseDragged(MouseEvent e) {
			int getWidth = getWidth();
			int getHeight = getHeight();

			if (pressed) {
				if(n==1) {
					x = (int)(e.getPoint().getX());
					y = (int)(e.getY());
			
					System.out.println("x = "+x+"\ny = "+y);
			
					repaint();
		
					f.reDim();
				}
			}

			if (pressed0) {
				if(n==1) {
					if (x>x-getWidth/15/2) x = x-(x-getWidth/15/2)-(int)(e.getPoint().getX());
					else x = x+(int)(e.getPoint().getX());
					if (y>y-getHeight/3/2) y = y-(y-getHeight/3/2)-(int)(e.getY());
					else y = y+(int)(e.getY());
			
					System.out.println("x = "+x+"\ny = "+y);
			
					repaint();
		
					f.reDim();
				}
			}

			System.out.print("blabla");		
		}

		public void mouseMoved(MouseEvent e) {
			System.out.print("");
		}
	}
	
	public void paintComponent (Graphics g) {
		int getWidth = this.getWidth();
		int getHeight = this.getHeight();

		/*if (x>getWidth) x=getWidth;
		if (y>getHeight) y=getHeight;
		if (x<getWidth) x=0;
		if (y<getHeight) y=0;*/

		g.setColor(new Color(-7960954));
		p1.setBounds(x-getWidth/15/2,y-getHeight/3/2,getWidth/15,getHeight/3);
	}
}
0
Merci
Une solution simple est this.revalidate (); appliqué au JPanel.