[問題] repaint沒能即時印出

看板java作者 (sn153246)時間13年前 (2011/04/01 20:04), 編輯推噓0(005)
留言5則, 3人參與, 最新討論串1/1
最近練習GUI 我用按鍵改變panel 可是沒辦法即時印出 要改變frame大小才可以 以下是大概的程式碼 public class DrawPolygon extends JFrame { private PolygonsPanel Poly = new PolygonsPanel(); //呼叫Panel class increasePoly implements ActionListener //假設呼叫了這兩個class { public void actionPerformed(ActionEvent e) { Poly.increase(); } } class decreasePoly implements ActionListener { public void actionPerformed(ActionEvent e) { Poly.decrease(); } } } class PolygonsPanel extends JPanel { private static int times = 5; public void increase() //這邊改了times 可是要改frame大小才會重印 { times++; repaint(); } public void decrease() { times--; repaint(); } protected void paintComponent(Graphics g) //N邊形的Polygon { super.paintComponent(g); /* 以下不重要 */ } } 為什麼不能即時印出來呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.193.0.248

04/01 23:08, , 1F
validate(); ??
04/01 23:08, 1F

04/01 23:16, , 2F
加過了...沒有效
04/01 23:16, 2F

04/02 01:17, , 3F
你的PolygonsPanel是直接放在JFrame裡嗎 還是還有一層?
04/02 01:17, 3F

04/02 10:33, , 4F
是另一層,因為我想把times設成靜態
04/02 10:33, 4F

04/02 10:46, , 5F
先謝謝各位的回答,我已經成功了
04/02 10:46, 5F
文章代碼(AID): #1DbRzT34 (java)