java - JPanel fill the whole JFrame -


i want place jpanel 300,200 size @ 100,50 location in jframe, following code makes whole jframe filled jpanel, wrong? code:

public class class1  {      public static void main(string[] args) {           jframe frame = new jframe();          jpanel panel = new jpanel();           frame.setsize(700, 500);                        panel.setsize(300, 200);          panel.setbackground(color.green);          panel.setcursor(new cursor(java.awt.cursor.hand_cursor));           frame.setdefaultcloseoperation(windowconstants.exit_on_close);           frame.add(panel, borderlayout.center);                    frame.setvisible(true);          frame.setlocationrelativeto(null);     }   } 

if change borderlayout.south or north, jpanel looks thin line @ bottom or top of jframe. tried use preferred size still same result.

you need understand basics of layout managers before can attempt swing.

study tutorial here https://docs.oracle.com/javase/tutorial/uiswing/layout/howlayoutworks.html

if trying construct form various swing controls, never use absolute coordinates position them. resizing window should dynamically reposition controls according type of layout want use. if using panel drawing canvas (which think after), @ example below.

try master couple of fundamental layouts first such borderlayout , gridlayout. once mastered can use few achieve kind of layout desire nesting them together. learn basics of scrollpane make efficient use of screen real-estate

as original question, created example of how borderlayout works , how draw specific region on panel. added code can set cursor differently depending on whether in smaller region within given panel.

try this:

import java.awt.borderlayout; import java.awt.color; import java.awt.cursor; import java.awt.dimension; import java.awt.graphics; import java.awt.rectangle; import java.awt.event.mouseevent; import java.awt.event.mousemotionlistener;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.swingutilities; import javax.swing.windowconstants;  public class framedemo extends jframe {   private static final long serialversionuid = 1l;    public framedemo() {     super("frame demo");     setdefaultcloseoperation(windowconstants.exit_on_close);      getcontentpane().add(new custompanel(color.red), borderlayout.north);     getcontentpane().add(new custompanel(color.green), borderlayout.south);     getcontentpane().add(new custompanel(color.blue), borderlayout.east);     getcontentpane().add(new custompanel(color.yellow), borderlayout.west);     getcontentpane().add(new jscrollpane(new custompanel(color.black)), borderlayout.center);      pack();   }    public static void main(string[] args) {     swingutilities.invokelater(new runnable() {       public void run() {         jframe frame = new framedemo();         frame.setvisible(true);       }     });   } }  class custompanel extends jpanel implements mousemotionlistener {   private static final long serialversionuid = 1l;    private static final dimension panel_size = new dimension(200, 100);   private static final int hand_cursor_index = 1;   private static final int default_cursor_index = 0;   private static final cursor[] _cursors = new cursor[] {     cursor.getdefaultcursor(),     cursor.getpredefinedcursor(cursor.hand_cursor)     };    // want place jpanel 300,200 size @ 100,50 location in jframe   private static final rectangle _rectangle = new rectangle(50, 10, 40, 50);    public custompanel(color color) {   setbackground(color);     addmousemotionlistener(this);   }    public dimension getpreferredsize() {     return panel_size;   }    public dimension getminimumsize() {     return panel_size;   }    public dimension getmaximumsize() {     return panel_size;   }     protected void paintcomponent(graphics g) {     super.paintcomponent(g);     g.setcolor(color.white);     g.fillrect(         (int)_rectangle.getx(),         (int)_rectangle.gety(),         (int)_rectangle.getwidth(),         (int)_rectangle.getheight());   }    @override   public void mousedragged(mouseevent e) {     // todo auto-generated method stub    }    @override   public void mousemoved(mouseevent e) {     int cursorindex = _rectangle.contains(e.getpoint()) ?         hand_cursor_index :         default_cursor_index;         setcursor(_cursors[cursorindex]);   } } 

enter image description here


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -