2012-08-31 95 views
5

我有一個JFrame其中有2 JPanel其中:a PaintPanel(與paint()方法)和ButtonPanel(帶按鈕)。當我調用PaintPanelrepaint()(但單擊按鈕)ButtonPanel的按鈕正在繪製在PaintPanel!它不是可點擊或任何東西,它只是在那裏。重繪時JButton被複制?

我試圖重新使用這段代碼的問題:

public class Main { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("frame"); 
     frame.setSize(400,400); 
     frame.setLayout(new GridLayout(2,1)); 
     PaintPanel paint = new PaintPanel(); 
     ButtonPanel buttons = new ButtonPanel(paint); 
     frame.add(paint); 
     frame.add(buttons); 
     frame.setVisible(true); 
    } 
} 

public class PaintPanel extends JPanel{ 
    public void paint(Graphics g){ 
     g.drawRect(10, 10, 10, 10); 
    } 
} 

public class ButtonPanel extends JPanel implements ActionListener{ 

    private PaintPanel paintPanel; 

    public ButtonPanel(PaintPanel paintPanel){ 
     this.paintPanel=paintPanel; 
     JButton button = new JButton("button"); 
     button.addActionListener(this); 
     add(button); 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     paintPanel.repaint();   
    } 
} 

這重新前人的精力我有問題(對不起,奇碼標記,似乎無法得到它的權利)。

我真的希望你一個人知道這裏發生了什麼,因爲我不...

+1

這種渲染物體往往不能兌現[透明度]出現( http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props)屬性。此外,「Swing程序應該重寫'paintComponent()',而不是重寫'paint()'。」 - [* AWT和Swing中繪製:繪製方法*](http://java.sun.com/products/jfc /tsc/articles/painting/index.html#callbacks)。 – trashgod

回答

6

首先,你應該重寫paintComponent()而不是paint()。這是Swing最佳實踐的一部分,當涉及到一些面板定製。

其次,這裏是爲我的作品(我不知道爲什麼你沒有,但:S):代碼

public class Main { 

    public static void main(String[] args) { 

     JFrame frame = new JFrame("frame"); 
     frame.setSize(400, 400); 
     // frame.setLayout(new GridLayout(2, 1)); 
     PaintPanel paint = new PaintPanel(); 
     ButtonPanel buttons = new ButtonPanel(paint); 
     // frame.add(paint); 
     // frame.add(buttons); 
     frame.setVisible(true); 

     JPanel pan = new JPanel(new BorderLayout()); 
     pan.add(paint); 
     pan.add(buttons, BorderLayout.SOUTH); 
     frame.add(pan); 

    } 
} 

class PaintPanel extends JPanel { 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(new Color(new Random().nextInt())); 
     g.drawRect(10, 10, 10, 10); 
    } 
} 

class ButtonPanel extends JPanel implements ActionListener { 

    private final PaintPanel paintPanel; 

    public ButtonPanel(PaintPanel paintPanel) { 

     this.paintPanel = paintPanel; 
     JButton button = new JButton("button"); 
     button.addActionListener(this); 
     add(button); 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     if (getParent() != null) { 
      getParent().repaint(); 
     } 
    } 
} 
+1

通常你想擁有super.paintComponent(g);作爲重寫的paintComponent方法的第一行。它將清除您在特定調用中未繪製的所有內容以重新繪製。 – mrranstrom

+0

@mrranstrom沒錯!我修正了我的例子,謝謝! – aymeric

+2

+1表示paintComponent&super.paintCompont。 @aymeric你必須記住,圖形對象被重用,所以如果你沒有花時間確定你已經清理了它,那麼最終會出現意想不到的paint artefact – MadProgrammer