java - Making a JPanel square -


if have jpanel multiple subcomponents, how make jpanel remains square despite how parent resized? have tried variations on following code, not result in subcomponents being square.

public void paint(graphics g){     if(this.issquare()){         dimension d = this.getsize();         if(d.height > d.width){             this.setsize(d.width, d.width);         } else {             this.setsize(d.height, d.height);     }     super.paint(g); } 

here sscce.

the containing parent:

import javax.swing.jframe;  public class testframe extends jframe{      public testframe(){         this.add(new testpanel());     }      public static void main(string[] args){         testframe tf = new testframe();          tf.setsize(500, 500);         tf.setvisible(true);     } } 

what should square jpanel:

import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.graphics;  import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.border.lineborder;   public class testpanel extends jpanel{     private boolean issquare;      public testpanel(){         this.setsquare(true);         this.setlayout(new borderlayout());          jlabel panel1 = new jlabel();         panel1.setborder(new lineborder(color.red, 4));         panel1.setbackground(color.cyan);          jlabel panel2 = new jlabel();         panel2.setborder(new lineborder(color.blue, 4));         panel2.setbackground(color.cyan);           this.setborder(new lineborder(color.green, 4));         this.setbackground(color.cyan);          this.add(panel1, borderlayout.west);         this.add(panel2, borderlayout.east);     }      public void paint(graphics g){         if(this.issquare()){             dimension d = this.getsize();             if(d.height > d.width){                 this.setsize(d.width, d.width);             } else {                 this.setsize(d.height, d.height);             }             super.paint(g);         }     }      private boolean issquare() {         return issquare;     }      private void setsquare(boolean issquare) {         this.issquare = issquare;     } } 

import java.awt.*; import javax.swing.*; import javax.swing.border.emptyborder;  public class sosquare {      public static void main(string[] args) {         runnable r = new runnable() {              @override             public void run() {                 // gui seen user (without frame)                 // single component added gbl no constraint                 // centered.                 jpanel gui = new jpanel(new gridbaglayout());                  gui.setbackground(color.white);                  squarepanel p = new squarepanel();                 p.setbackground(color.red);                 gui.add(p);                  jframe f = new jframe("demo");                 f.add(gui);                 // ensures jvm closes after frame(s) closed ,                 // non-daemon threads finished                 f.setdefaultcloseoperation(jframe.dispose_on_close);                 // see http://stackoverflow.com/a/7143398/418556 demo.                 f.setlocationbyplatform(true);                  // ensures frame minimum size needs                 // in order display components within                 f.pack();                 f.setsize(400,100);                 // should done last, avoid flickering, moving,                 // resizing artifacts.                 f.setvisible(true);             }         };         // swing guis should created , updated on edt         // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html         swingutilities.invokelater(r);     } }  /**  * square panel rendering. note: work correctly, must  * component in parent layout allows child decide size.  */ class squarepanel extends jpanel {      @override     public dimension getpreferredsize() {         dimension d = super.getpreferredsize();         container c = getparent();         if (c != null) {             d = c.getsize();         } else {             return new dimension(10, 10);         }         int w = (int) d.getwidth();         int h = (int) d.getheight();         int s = (w < h ? w : h);         return new dimension(s, s);     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -