swing - Trying to set the location of a Java Button using JFrame isn't working? -
please below edits.
so i've looking on numerous "solutions" fix problem, can't seem working.
this application looks code below:
basically, want set location of button, can't manage so. here code:
package me.cervinakuy.application; import java.awt.borderlayout; import java.awt.color; import java.awt.gridlayout; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class controlpanel3 extends jframe { jpanel panel = new jpanel(); jbutton startrobo = new jbutton(); jbutton stoprobo = new jbutton(); jbutton restartrobo = new jbutton(); public controlpanel3() { // setlayout(null); setsize(1000, 700); setresizable(false); setlocation(450, 150); setdefaultcloseoperation(jframe.exit_on_close); getcontentpane().setbackground(new color(45, 48, 55)); settitle("espin software | control panel"); setvisible(true); startrobo.seticon(new imageicon(getclass().getresource("/resources/startrobo.png"))); stoprobo.seticon(new imageicon(getclass().getresource("/resources/stoprobo.png"))); restartrobo.seticon(new imageicon(getclass().getresource("/resources/restartrobo.png"))); startrobo.setborder(null); stoprobo.setborder(null); restartrobo.setborder(null); startrobo.setlocation(100, 100); panel.add(startrobo); panel.add(stoprobo); panel.add(restartrobo); panel.setopaque(false); add(panel); validate(); } }
edit: i have managed create gui of looking for, however, have new problem. buttons pressable different parts of gui, rather on image. interested, here have been able accomplish:
updated code:
package me.cervinakuy.application; import java.awt.color; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class controlpanel3 extends jframe { jpanel panel = new jpanel(); jbutton startrobo = new jbutton(); jbutton stoprobo = new jbutton(); jbutton restartrobo = new jbutton(); public controlpanel3() { // setlayout(null); setsize(1000, 700); setresizable(false); setlocation(450, 150); setdefaultcloseoperation(jframe.exit_on_close); getcontentpane().setbackground(new color(45, 48, 55)); settitle("espin software | control panel"); setvisible(true); startrobo.seticon(new imageicon(getclass().getresource("/resources/startrobo.png"))); stoprobo.seticon(new imageicon(getclass().getresource("/resources/stoprobo.png"))); restartrobo.seticon(new imageicon(getclass().getresource("/resources/restartrobo.png"))); startrobo.setborder(null); stoprobo.setborder(null); restartrobo.setborder(null); panel.setlayout(null); startrobo.setlocation(200, 200); startrobo.setbounds(5, -95, 300, 300); stoprobo.setbounds(5, 0, 300, 300); restartrobo.setbounds(5, 95, 300, 300); panel.add(startrobo); panel.add(stoprobo); panel.add(restartrobo); panel.setopaque(false); add(panel); validate(); } }
there typically number of ways layout components end same effect. in example, use panel contain buttons in column (buttoncontainer
using gridlayout
) panel restrict container top (buttonconstrainpanel
using borderlayout
) container put panel on left (ui
borderlayout
).
it achieved using single gridbaglayout
or grouplayout
, though logic of achieving might not simple.
the focus border seen on blue button indicates limits of mouse click activate button.
import java.awt.*; import java.net.malformedurlexception; import java.net.url; import javax.swing.*; import javax.swing.border.emptyborder; public class threebuttonalignedleft { private jcomponent ui = null; private string prefix = "http://i.stack.imgur.com/"; private string[] suffix = {"gjmej.png","t5uta.png","wcf8s.png"}; threebuttonalignedleft() { try { initui(); } catch (malformedurlexception ex) { ex.printstacktrace(); } } public void initui() throws malformedurlexception { if (ui!=null) return; ui = new jpanel(new borderlayout(4,4)); ui.setborder(new emptyborder(4,4,4,4)); jpanel buttoncontainer = new jpanel(new gridlayout(0, 1, 5, 5)); (int ii=0; ii<suffix.length; ii++) { jbutton b = new jbutton(new imageicon(new url(prefix + suffix[ii]))); b.setborderpainted(false); b.setmargin(new insets(0,0,0,0)); b.setcontentareafilled(false); buttoncontainer.add(b); } jpanel buttonconstrainpanel = new jpanel(new borderlayout(0, 0)); buttonconstrainpanel.add(buttoncontainer, borderlayout.page_start); ui.add(buttonconstrainpanel, borderlayout.line_start); } public jcomponent getui() { return ui; } public static void main(string[] args) { runnable r = new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception usedefault) { } threebuttonalignedleft o = new threebuttonalignedleft(); jframe f = new jframe(o.getclass().getsimplename()); f.setdefaultcloseoperation(jframe.dispose_on_close); f.setlocationbyplatform(true); f.setcontentpane(o.getui()); f.pack(); f.setminimumsize(f.getsize()); f.setvisible(true); } }; swingutilities.invokelater(r); } }
Comments
Post a Comment