java - How to make components in a JPanel stretch as far as possible? -
i'm trying create simple swing program lets user play circle.
here code:
import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jbutton; import javax.swing.border.border; import javax.swing.borderfactory; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.color; import java.awt.graphics; import java.awt.flowlayout; import java.awt.borderlayout; public class sixthprogram { public static void main(string[] args) { gui prog=new gui("sixthprogram"); prog.setbounds(350,250,500,250); prog.setvisible(true); } } class gui extends jframe implements mouselistener, mousemotionlistener { jpanel colorpan, color1, color2, color3 ,color4 ,color5; int x=3,y=30; // position of circle public gui(string header) { super(header); setdefaultcloseoperation(jframe.exit_on_close); setlayout(new borderlayout()); maker(); addmouselistener(this); addmousemotionlistener(this); add(colorpan, borderlayout.south); } public void maker() { colorpan = new jpanel(); border raisedbevel = borderfactory.createraisedbevelborder(); border loweredbevel = borderfactory.createloweredbevelborder(); border compound = borderfactory.createcompoundborder(raisedbevel, loweredbevel); colorpan.setborder(compound); colorpan.setlayout(new flowlayout(flowlayout.center)); color1 = new jpanel(); color2 = new jpanel(); color3 = new jpanel(); color4 = new jpanel(); color5 = new jpanel(); color1.setbackground(color.white); color2.setbackground(color.green); color3.setbackground(color.red); color4.setbackground(color.blue); color5.setbackground(color.black); colorpan.add(color1); colorpan.add(color2); colorpan.add(color3); colorpan.add(color4); colorpan.add(color5); } @override public void paint(graphics g) { //g.setcolor(color.white); //g.fillrect(0,0,getwidth(),getheight()); super.paint(g); //do same thing above(clear jframe) g.setcolor(color.red); g.filloval(x,y,50,50); } public void mouseexited(mouseevent e) //mouselistener overrided methods { system.out.println("exit"); } public void mouseentered(mouseevent e) { system.out.println("enter"); } public void mousereleased(mouseevent e) { system.out.println("release"); } public void mousepressed(mouseevent e) { system.out.println("press"); x=e.getx(); y=e.gety(); if(x+50<getwidth() && y+50<getheight()) // preventing out of bounds repaint(); } public void mouseclicked(mouseevent e) //press+release=click { system.out.println("click"); system.err.println(((jpanel)e.getsource()).getname()); } public void mousedragged(mouseevent e) //mousemotionlistener overrided methods { system.out.println("dragged ("+ e.getx() +","+ e.gety() +")"); x=e.getx(); y=e.gety(); if((x>=3 && y>=30) && (x+50<getwidth() && y+50<getheight())) // preventing out of bounds repaint(); } public void mousemoved(mouseevent e) { system.out.println("moved ("+ e.getx() +","+ e.gety() +")"); } }
i'm trying create jpanel different colors. when clicked on color, circle's color change. i've not yet implemented part.
my problem is, when run above program, output as:
but want output be
i've tried removing
colorpan.setlayout(new flowlayout(flowlayout.center));
and resulted in same output.
how stretch components in colorpan
, expected output?
instead of flowlayout
, can use gridlayout
follows:
colorpan.setlayout(new gridlayout(1, 5));
this divides width of element in 1 row , 5 columns, 1 per color.
Comments
Post a Comment