2011-11-08 47 views
1

我有一個問題,如果按下「c」鍵,我只需要重繪/重建繪圖區域。 我正在使用repaint()的方式,結果導致繪製區域的位置。 我還注意到,每當我重新調整框架的大小時,keylistener不再工作。Java如何重繪JPanel(重新創建對象)?

  1. 無法正確重繪:

    問題。

  2. 在重新調整幀大小後,keylistener無法工作。

喜歡附加顯示,但好像它被阻止,因爲我是新手。

以下代碼是調用類「newZone」的主要函數。

frame.addComponentListener(new ComponentAdapter() { 

     public void componentResized(ComponentEvent e){ 
      System.out.println("component Rebuild"); 
      frame.getContentPane().removeAll(); 
      frame.getContentPane().invalidate(); 
      JComponent newContentPane = new newZone(frame.getSize()); 
      newContentPane.setOpaque(true); 
      frame.getContentPane().add(newContentPane); 
      frame.getContentPane().revalidate(); 
      frame.setContentPane(newContentPane); 
     } 
    }); 

以下是名爲newzone類的,它包含塗料&的KeyListener:爲什麼你正在使用的ComponentListener

public class newZone extends JComponent implements MouseListener, MouseMotionListener, KeyListener { 

JPanel panel1; 
JTextArea textArea; 
JScrollPane scrollPane; 
MyDrawingTool Drawing; 
static int firsttimer = 0; 
static int preposX = 0; 
static int preposY = 0; 
static int widthPercentage = 80 , heightPercentage = 93; 
static int numberOfYboxes,numberOfXboxes; 
static Dimension currentPanelSize; 
static final String NEWLINE = System.getProperty("line.separator"); 
static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 


public newZone(Dimension currentPanelSize1) { 

    currentPanelSize = currentPanelSize1; 

    Drawing = new MyDrawingTool(); 
    Drawing.setBackground(Color.WHITE); 
    Drawing.setBounds( 10, 10, 
         (int) currentPanelSize.getWidth()*(widthPercentage)/100, 
         (int) currentPanelSize.getHeight()*(heightPercentage)/100); 
    Drawing.setPreferredSize(new Dimension((int) currentPanelSize.getWidth()*(widthPercentage)/100, 
              (int) currentPanelSize.getHeight()*(heightPercentage)/100)); 

    Drawing.addMouseListener(this); 
    Drawing.addMouseMotionListener(this); 

    add(Drawing); 

    addKeyListener(new KeyAdapter() 
    { 
     public void keyPressed(KeyEvent e){ 
      System.out.println("Key type: "+e.getKeyChar()); 

      if(e.getKeyChar() == 'c'){ 
       Drawing.redraw(); 
      } 
     } 
    }); 
    setFocusable(true); 
} 


class MyDrawingTool extends JPanel{ 

    void redraw(){ 

     repaint(); 
    } 

    @Override 
    public void paint(Graphics q){ 

     //super.paint(q); 

     int j,k, width, height; 
     int startX = 10, startY = 10; 
     int boxSize = 50; 

     width = (int)currentPanelSize.getWidth()*(widthPercentage)/100; 
     height = (int)currentPanelSize.getHeight()*(heightPercentage)/100; 

     numberOfYboxes = (height-20)/50; 
     numberOfXboxes = (width-20)/50; 

     for (j = 0; j < numberOfYboxes; j++) 
     { 
      startX = 10; 
      for (k = 0; k < numberOfXboxes; k++) 
      { 
       q.setColor(Color.WHITE); 
       q.fillRect(startX, startY, boxSize, boxSize); 
       q.setColor(Color.BLUE); //Set line color 
       q.drawRect(startX, startY, boxSize, boxSize); 
       startX+=boxSize; 
      } 
      startY+=boxSize; 

     } 
    } 
} 

}

+2

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

2

我不知道。我沒有看到任何理由刪除/添加/ invalidate/revalidat並做所有其他的東西。

您只需將面板添加到框架內容窗格的中心。隨着框架大小調整,面板會自動增加/減小尺寸。不需要ComponentListener。

自定義繪畫應該在paintComponent()方法中完成,並且不要忘記在開始時調用super.paintComponent(...)。

KeyListener不起作用,因爲在調整幀大小後焦點現在位於JFrame(不是面板)上。你不應該爲此使用KeyListener。相反,您應該是Key Bindings即使面板沒有焦點時也可以工作。

+0

camickr謝謝..耶看起來像paintComponent完美的工作〜!我在使用絕對位置的原因,我覺得在更多的控制比較中心。 – humpingpanda

0

看來,您根本不需要切換內容窗格。 如果您在默認內容窗格上使用某種佈局,如@camickr建議的那樣,您將不需要手動處理調整大小和其他內容。 祝你好運。