java - How can I align elements in JPanels / JFrames? -



java - How can I align elements in JPanels / JFrames? -

i'm new using gui in java, i'm having bit of problem figuring out how align need to. have panels in jframe need align (one left, 1 right) , few buttons in 1 of panels need centered in panel. here code.

package application; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.io.*; import java.nio.*; import java.util.*; public class main extends jpanel { public static void main(string[] args) { //set ui native os seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); }catch(classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception e) { } jframe frame = new jframe("application name"); menu menu = new menu(); jpanel iconpanel = new jpanel(); final jpanel grid = new jpanel(new flowlayout()); jbutton firewallbutton = new jbutton("firewall"); jbutton networkbutton = new jbutton("network"); jbutton printerbutton = new jbutton("printer"); int iconpanelsizex; int iconpanelsizey; int gridsizex; int gridsizey; int gridposition; //frame setting frame.setsize(800, 600); frame.setminimumsize(new dimension(800, 600)); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationrelativeto(null); frame.setvisible(true); //add grid , iconpanel jpanels frame frame.add(iconpanel); iconpanel.add(firewallbutton); iconpanel.add(networkbutton); iconpanel.add(printerbutton); frame.add(grid); //iconpanel settings iconpanel.setborder(borderfactory.createloweredsoftbevelborder()); iconpanel.setbackground(color.gray); iconpanel.setlayout(new flowlayout()); iconpanel.setsize(new dimension(100, 600)); iconpanel.setvisible(true); //grid setting grid.setbackground(color.red); grid.setsize(new dimension(700, 600)); grid.setvisible(true); //this resizing components when user resizes window int counter = 0; while(counter == 0) { firewallbutton.setsize(new dimension(iconpanel.getwidth(), 50)); networkbutton.setsize(new dimension(iconpanel.getwidth(), 50)); printerbutton.setsize(new dimension(iconpanel.getwidth(), 50)); iconpanelsizex = frame.getwidth() / 10; iconpanelsizey = frame.getheight(); gridsizex = (frame.getwidth() / 10) * 9; gridsizey = frame.getheight(); iconpanel.setsize(new dimension(iconpanelsizex, iconpanelsizey)); grid.setsize(new dimension(gridsizex, gridsizey)); } } }

as can see, sec jpanel (grid) doesn't line right side of frame, , buttons within icontray don't center either. realize these both simple layout fixes, have no clue start.

for simple splitting of jframe can utilize gridlayout 1 row , 2 colums.

frame.setlayout(new gridlayout(1,2,3,3)); //3,3 gaps frame.add(grid); frame.add(iconpanel);

for centering components in panels can utilize flowlayout default set on jpanels:

doing manualy:

grid.setlayout(new flowlayout()); //centered components grid.setlayout(new flowlayout(flowlayout.left,3,3)); //components aligned left grid.setlayout(new flowlayout(flowlayout.right,3,3)); //components aligned right

this how looks:

also, few observations:

never phone call setxxxsize() methods components;

try avoid calling setsize(); jframe, phone call pack(); instead;

call setvisible(true); in end of code;

all huge code can "stripped" this:

import javax.swing.*; import java.awt.*; public class main extends jpanel { public static void main(string[] args) { jframe frame = new jframe("application name"); jpanel iconpanel = new jpanel(); jpanel grid = new jpanel(new flowlayout()); jbutton firewallbutton = new jbutton("firewall"); jbutton networkbutton = new jbutton("network"); jbutton printerbutton = new jbutton("printer"); frame.add(iconpanel); iconpanel.add(firewallbutton); iconpanel.add(networkbutton); iconpanel.add(printerbutton); grid.setbackground(color.green); frame.setlayout(new gridlayout(1,2,3,3)); frame.add(grid); frame.add(iconpanel); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }

java swing jframe jpanel layout-manager

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -