swing - Java JFrame .setSize(x, y) not working? -


when execute bit of code, tiny window pops that, , inside of 116x63, , entire size including border of ~140x100. how set inside need to?

public static void graphics() {     jframe frame = new jframe();      string title = "test window";     frame.settitle(title);      frame.setsize(gridrow, gridcol); //101 x 101     frame.setresizable(true);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setvisible(true); } 

  1. create custom component, extending jpanel, override getpreferredsize method return size of window like.
  2. either add frame or set frame's content pane.
  3. call pack on frame

updated example

enter image description here

on pc, frame size = java.awt.dimension[width=216,height=238]

import java.awt.borderlayout; import java.awt.dimension; import java.awt.eventqueue; import java.awt.fontmetrics; import java.awt.graphics; import java.awt.graphics2d; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class testframesize01 {      public static void main(string[] args) {         new testframesize01();     }      public testframesize01() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.setlayout(new borderlayout());                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);                  system.out.println("frame size = " + frame.getsize());             }         });     }      public class testpane extends jpanel {          public testpane() {         }          @override         public dimension getpreferredsize() {             return new dimension(200, 200);         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             graphics2d g2d = (graphics2d) g.create();             string text = getwidth() + "x" + getheight();             fontmetrics fm = g2d.getfontmetrics();             int x = (getwidth() - fm.stringwidth(text)) / 2;             int y = ((getheight() - fm.getheight()) / 2) + fm.getascent();             g2d.drawstring(text, x, y);             g2d.dispose();         }     }     } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -