java - JPanel and JButton, cannot figure how to layout 2 simple buttons -


i starting jpanel , trying put 2 simple buttons on frame, able put buttons not position them, here code:

jframe frame = new jframe(); frame.setsize(500,500); frame.setdefaultcloseoperation(jframe.exit_on_close);         jbutton = new jbutton("text"); jbutton but2 = new jbutton("list");  jpanel panel= new jpanel(new gridlayout(1, 2)); panel.setsize(100, 100); panel.add(but); panel.add(but2);      frame.add(panel); frame.setvisible(true); 

and here sketch of want:

what want

look layout padding , borders solve this.

enter image description here

import java.awt.*; import javax.swing.*; import javax.swing.border.emptyborder;  class twobuttonlayout {      public static void main(string[] args) {         runnable r = new runnable() {              @override             public void run() {                 // adjust numbers need..                 jpanel panel = new jpanel(new gridlayout(1, 2, 40, 40));                 // adjust numbers need..                 panel.setborder(new emptyborder(20,30,20,30));                 panel.setbackground(color.white);                  jbutton = new jbutton("text");                 jbutton but2 = new jbutton("list");                  panel.add(but);                 panel.add(but2);                  joptionpane.showmessagedialog(null, panel);             }         };         // swing guis should created , updated on edt         // http://docs.oracle.com/javase/tutorial/uiswing/concurrency         swingutilities.invokelater(r);     } } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -