java - won't repaint a different Month after pressing button in my calendar -
i'm trying build calendar in java little project thought of, can't seem change name of month every time click next button. here's code!
package drawing; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class drawing_something extends jpanel{ int[] calender_squares = {1, 2, 3, 4, 5, 6, 7}; string[] month = {"january", "february", "march", "april","may","june","july", "august","september","october","november","december"}; int = 0; graphics c; @override public void paintcomponent(graphics c){ super.paintcomponent(c); this.setbackground(color.white); int width = 55, height = 65; (int in: calender_squares) { (int counter = 0; counter < 7; counter++){ c.drawrect(50, 50, 100, 100); c.drawrect(50, 50, 700, 500); c.copyarea(50, 50, 600, 500, 100, 0); c.copyarea(50, 50, 600, 400, 0, 100); } } (int date = 1; date <= 30; date++) { string s = string.valueof(date); c.drawstring(s, width, height); if (date <= 6){ width += 100; } else if (date == 7){ width = 55; height = 165; }else if (date <= 13){ width += 100; }else if (date == 14){ width = 55; height = 265; }else if (date <= 20){ width += 100; }else if (date == 21){ width = 55; height = 365; }else if (date <= 27){ width += 100; }else if (date == 28){ width = 55; height = 465; }else if (date <= 30){ width += 100; } } c.setfont(new font("default", font.bold, 40)); c.drawstring(month[i], 320, 45); } public drawing_something(){ setlayout(new borderlayout()); jbutton n = new jbutton("next"); jbutton b = new jbutton("back"); jpanel p = new jpanel(); p.add(b); p.add(n); add(p, borderlayout.south); b.addactionlistener(new handlerclass()); n.addactionlistener(new nextclass()); } public class handlerclass implements actionlistener{ public void actionperformed(actionevent e){ } } public class nextclass implements actionlistener{ public void actionperformed(actionevent e){ if (i == 11){ = 0; } = + 1; c.drawstring(month[i], 320, 45); } } public static void main(string[] args){ jframe mainframe = new jframe("calender"); mainframe.add(new drawing_something()); mainframe.setsize(850, 650); mainframe.setdefaultcloseoperation(jframe.exit_on_close); mainframe.setvisible(true); } }
if appreciated!!
thanks in advance!!
don't maintain reference graphics
context used paint component, not how custom painting done.
instead, once update i
value, call repaint
you shouldn't adding components draw_something
component, these cover been painted.
instead, add components separate container , use setters , getters change state of calendar pane
Comments
Post a Comment