java - Graphic in Panel flashed and then disappear -
recently needed write program draw bar graph when press prepared place. there 2 situation draw graphic, 1 press place in table, other choice region (such kaohsiung or taipei) in typebox enter image description here
but not know why press place in table clear draw graphic on panel, when press typebox, drawing flash gone. here part of code issue
public class google2 extends jframe implements actionlistener{ jpanel panel;//主要畫布 graphics g; int regnm=0,regns=0,regna=0,regn=0,regp=0;//生氣數量,傷心數量,憂慮數量,其他數量,正面數量 string[] stype = { "全部", "基隆", "台北", "桃園", "新竹", "苗栗", "台中", "南投", "彰化", "雲林", "嘉義" , "台南", "高雄", "屏東", "台東", "花蓮", "宜蘭", "外島", "國外"};//所有地區 jcombobox typebox = new jcombobox(stype);//下拉式印出地區 defaulttablemodel tmc = new defaulttablemodel(new object [][] {},new string [] { "place","count"});//選地點用model jtable jt =new jtable(); jscrollpane scrollpane = new jscrollpane(jt);//印出負面地點的滾輪 public google2(){ setlocation(450,50); setlayout(null); setvisible(true); setdefaultcloseoperation(exit_on_close); setsize(1300, 850); typebox.setbounds(125,55,125,30);add(typebox); typebox.addactionlistener(this); scrollpane.setbounds(10, 90, 250, 535);add(scrollpane); jt.setmodel(tmc);jt.setrowheight(30); jt.getselectionmodel().addlistselectionlistener(//選地點 new listselectionlistener() { public void valuechanged(listselectionevent event) { if(!event.getvalueisadjusting()){ if(jt.getselectedrow()>-1){ draw(); } } } }); } public void actionperformed(actionevent e){ if(e.getsource()==typebox){ draw(); } } void draw(){ g.setcolor(white); g.fillrect(0, 0, 379, 329); g.setcolor(black); float linewidth = 6.0f; ((graphics2d)g).setstroke(new basicstroke(linewidth)); g.drawrect(0, 0, 380, 330); g.setfont(font1); g.drawstring("生氣", 30, 300); g.drawstring("傷心", 100, 300); g.drawstring("憂慮", 170, 300); g.drawstring("其他", 240, 300); g.drawstring("正面", 310, 300); g.setcolor(red); if(regnm>0){ int md=25; md=md*regnm; if(md>250) md=250; g.fillrect(35, 275-md, 35, md); } g.setcolor(orange); if(regns>0){ int md=25; md=md*regns; if(md>250) md=250; g.fillrect(105, 275-md, 35, md); } g.setcolor(color.magenta); if(regna>0){ int md=25; md=md*regna; if(md>250) md=250; g.fillrect(175, 275-md, 35, md); } g.setcolor(color.cyan); if(regn>0){ int md=25; md=md*regn; if(md>250) md=250; g.fillrect(245, 275-md, 35, md); } g.setcolor(blue); if(regp>0){ int md=25; md=md*regp; if(md>250) md=250; g.fillrect(315, 275-md, 35, md); } regnm=0;regns=0;regna=0;regn=0;regp=0; } //main function public static void main(string[] args) throws exception{ google2 gg = new google2(); } }
i had tried many methods repaint, update can not solved, there still can drawing when press table, unable drawing when press typebox hope can me, thank you
this not how painting in swing should done. start taking @ painting in awt , swing , performing custom painting more details how painting should done.
basically, want override paintcomponent
method of component extending jcomponent
(jpanel
preferred) , perform ever custom painting want/need perform there, remember call super.paintcomponent
before own custom painting.
avoid using null
layouts, pixel perfect layouts illusion within modern ui design. there many factors affect individual size of components, none of can control. swing designed work layout managers @ core, discarding these lead no end of issues , problems spend more , more time trying rectify
Comments
Post a Comment