2013-12-12 54 views
0

我創建了一個類是bosex顯示,並且它讀取您單擊的位置,並且該處理程序根據您單擊的位置更改背景色,除了更新JFrame外,一切工作正常。我嘗試了3種不同的代碼,如處理程序所示。 代碼:JFrame不更新與背景更改

public class Dynamic_Bg_Color extends JFrame{ 

private static final long serialVersionUID = 1L; 

JFrame frame; 

Handler handler = new Handler(); 

static Dimension size = new Dimension(500,400); 

public Dynamic_Bg_Color(){ 


    frame = new JFrame("BG Color Changer"); 

    frame = this; 

    setBackground(Color.cyan); 

    frame.addMouseListener(handler); 

} 

public void paint(Graphics g1){ 

    System.out.println("Click"); 

    Graphics g = (Graphics)g1; 

    g.setColor(Color.blue); 
    g.fillRect(20,20,frame.getWidth()-40,100); 

    g.setColor(Color.green); 
    g.fillRect(20,140,frame.getWidth()-40,100); 

    g.setColor(Color.orange); 
    g.fillRect(20,260,frame.getWidth()-40,100); 
} 

public static void main(String[] args){ 

    Dynamic_Bg_Color d = new Dynamic_Bg_Color(); 

    d.frame.setPreferredSize(new Dimension(size)); 
    d.frame.setMinimumSize(new Dimension(size)); 
    d.frame.setMaximumSize(new Dimension(size)); 
    d.frame.setLocationRelativeTo(null); 

    d.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    d.frame.setLayout(new FlowLayout()); 
    d.frame.setVisible(true); 
} 

public class Handler implements MouseListener{ 

    public void mouseClicked(MouseEvent e) { 

    } 
    public void mousePressed(MouseEvent e) { 

     int x = e.getX(); 
     int y = e.getY(); 

     if(x>= 20 && x<=frame.getWidth()-40 && y>=20 && y<= 120){ 
      setBackground(Color.blue); 
     } 
     if(x>= 20 && x<=frame.getWidth()-40 && y>=140 && y<= 240){ 
      getContentPane().setBackground(Color.green); 
     } 
     if(x>= 20 && x<=frame.getWidth()-40 && y>=260 && y<= 360){ 
      frame.getContentPane().setBackground(Color.orange); 
     } 
     repaint(); 
    } 

//Implemented Methods, cut to shorten, no code in them// 

} 

} 
+0

我不明白你爲什麼只從'JFrame'擴展到他們忽略它並創建第二幀,然後想知道爲什麼它不會畫 – MadProgrammer

回答

2
  1. 不要覆蓋頂層容器paint。他們負責維護一個複雜的塗料鏈(現在已經打破),而且它們不是雙重緩衝的,可能會在塗漆時導致閃爍。
  2. 請致電super.paintXxx

代替延伸JFrame,請使用JPanel。而不是覆蓋paint,覆蓋paintComponent。在做任何其他自定義繪畫之前,請致電super.paintComponent。它負責繪製背景

最後,該面板添加到的JFrame一個實例...

看看Performing Custom Painting更多細節

與運行的例子更新

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Dynamic_Bg_Color extends JPanel { 

    private static final long serialVersionUID = 1L; 

    public Dynamic_Bg_Color() { 
     setBackground(Color.cyan); 
     addMouseListener(new Handler()); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(500, 500); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     System.out.println("Click"); 

     g.setColor(Color.blue); 
     g.fillRect(20, 20, getWidth() - 40, 100); 

     g.setColor(Color.green); 
     g.fillRect(20, 140, getWidth() - 40, 100); 

     g.setColor(Color.orange); 
     g.fillRect(20, 260, getWidth() - 40, 100); 
    } 

    public class Handler extends MouseAdapter { 

     @Override 
     public void mousePressed(MouseEvent e) { 

      int x = e.getX(); 
      int y = e.getY(); 

      System.out.println(x + "x" + y); 

      if (x >= 20 && x <= getWidth() - 40 && y >= 20 && y <= 120) { 
       setBackground(Color.blue); 
      } 
      if (x >= 20 && x <= getWidth() - 40 && y >= 140 && y <= 240) { 
       setBackground(Color.green); 
      } 
      if (x >= 20 && x <= getWidth() - 40 && y >= 260 && y <= 360) { 
       setBackground(Color.orange); 
      } 
      repaint(); 
     } 

    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       Dynamic_Bg_Color d = new Dynamic_Bg_Color(); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(d); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

} 
+0

所以我讓它擴展JPa nel,以及其他你所說的變化,除了super.paintComponentXxx,即時通訊新的Java和不知道這是什麼意思。我修復了幾個錯誤,但現在我回到3小時前的位置,除了paintComponent()方法之外的函數程序沒有被調用。雖然背景顏色發生變化,但沒有來自paintComponent()的可視框,謝謝。 –

+0

以可運行示例更新 – MadProgrammer

+0

哇,非常感謝,這解決了這個問題。我試着將其實現到我的代碼中,並且它沒有像EventQueue這樣的一些東西,但是它工作,所以謝謝。我會盡可能地上傳最終的代碼,它說我必須等待。 –