2012-08-18 70 views
2

我有兩個JPanel s。第一個包含JButton s,第二個包含鼠標。問題是當我點擊JButton並開始繪製JButton時也在JPanel上畫畫。請向我提供一些方向,關於我不看的地方?當按下按鈕,然後開始繪製按鈕也複製在面板


主類

public class LabelDemo extends JFrame { 

    JPanel p1 = new JPanel(); 
    painter p2 = new painter(); 
    JButton red = new JButton("red"); 
    JButton blue = new JButton(" blue "); 
    JLabel lbl = new JLabel("Label"); 
    ImageIcon icon = new ImageIcon("image/YouTube.png"); 

    public LabelDemo() { 

     setLayout(new BorderLayout()); 
     p1.setBorder(new LineBorder(Color.gray)); 
     //jbt1.setIcon(icon); 

     p1.add(red); 
     p1.add(blue); 
     lbl.setOpaque(true); 
     lbl.setBackground(Color.yellow); 
     p1.add(lbl); 
     p1.setBounds(20, 30, 40, 78); 
     add(p1,BorderLayout.EAST); 
     add(p2,BorderLayout.CENTER); 
    } 

    public static void main(String[] args){ 

     LabelDemo frame = new LabelDemo(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(700, 400); 
     frame.setLocationRelativeTo(null);    
    } 
} 

innr類

class painter extends JPanel { 

    int x , y; 
    boolean isPresed = false; 

    public void setPainter(int x , int y) {  
     this.x = x; 
     this.y = y; 
    } 

    public painter() { 

     addMouseMotionListener(new MouseMotionAdapter() {     
      public void mouseDragged(MouseEvent e) { 
       isPresed = true; 
       setPainter(e.getX(),e.getY()); 
       repaint(); 
      } 
     }); 
    } 

    protected void paintComponent(Graphics g){ 
     Color randomColor = Color.getHSBColor((float)Math.random(), 1.0F, 1.0F); 
     if(isPresed){      
      g.setColor(randomColor); 
      g.fillOval(x-5, y-5, 10, 10); 
     } 
    } 
}//end of painter 

enter image description here

+0

我試圖上傳源代碼,但我不能。我不知道如何上傳 – 2012-08-18 16:01:51

+0

你可以[編輯你的問題](http://stackoverflow.com/posts/12019996/edit)(鏈接下面的問題),並直接在你的問題中複製相關的代碼。 – assylias 2012-08-18 16:02:49

+0

哎呀!你的編輯無法提交,因爲...這就是我得到的 – 2012-08-18 16:08:23

回答

0

好吧,我瞭解你要點擊紅色或藍色按鈕時得到東(P1面板)上去掉面板:

package stack; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 

import javax.swing.*; 
import javax.swing.border.LineBorder; 

public class LabelDemo extends JFrame { 
    JPanel p1 = new JPanel(); 
    Painter p2 = new Painter(); 
    JButton red = new JButton("red"); 
    JButton blue = new JButton(" blue "); 
    JLabel lbl = new JLabel("Label"); 
    ImageIcon icon = new ImageIcon("image/YouTube.png"); 
    public LabelDemo() { 
     setLayout(new BorderLayout()); 
     p1.setBorder(new LineBorder(Color.gray)); 
//  jbt1.setIcon(icon); 
     p2.setPreferredSize(new Dimension(600,400)); 
     p1.add(red); 
     p1.add(blue); 
     lbl.setOpaque(true); 
     lbl.setBackground(Color.yellow); 
     p1.add(lbl); 
     p1.setBounds(20, 30, 40, 78); 
     add(p1,BorderLayout.EAST); 
     add(p2,BorderLayout.CENTER); 
     red.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent e){ 
      remove(p1); 
      repaint(); 
      revalidate(); 
     } 
    }); 
    blue.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent e){ 
      remove(p1); 
      repaint(); 
      revalidate(); 
     } 
    }); 
} 


    public static void main(String[] args){ 
     LabelDemo frame = new LabelDemo(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
class Painter extends JPanel { 
    int x , y; 
    boolean isPresed = false; 
    public void setPainter(int x , int y) { 
    this.x = x; 
    this.y = y; 
} 
    public Painter() { 

    addMouseMotionListener(new MouseMotionAdapter() { 

     public void mouseDragged(MouseEvent e) { 
      isPresed = true; 
      setPainter(e.getX(),e.getY()); 
      repaint(); 
     } 
    }); 
} 

    protected void paintComponent(Graphics g){ 
     Color randomColor = Color.getHSBColor((float)Math.random(), 1.0F, 1.0F); 
    if(isPresed){ 

     g.setColor(randomColor); 
     g.fillOval(x-5, y-5, 10, 10); 
    } 
    } 
}//end of painter 

也不要調用setSize()方法調用包(),並有必要對setPreferredSize( ),因爲那個,我被個人批評了幾次。只是一個註釋。

+0

我仍然無法解決問題,請你參考我的具體源代碼? 我不想按鈕消失... – 2012-08-18 16:55:50

+0

你想達到什麼? – 2012-08-18 16:57:42

+0

我不希望包含該按鈕的面板消失... – 2012-08-18 17:23:35

0

在您的painter窗格的paintComponent方法中,您應該首先調用super.paintComponent

Swing重複使用Graphics,因此可能會將舊內容仍保留在緩衝區中。如果你打電話給super.paintComponent,這會清理它

protected void paintComponent(Graphics g){ 
    // Must call super.paintComponent() so the Graphics is updated correctly... 
    super.paintComponent(); 
    Color randomColor = Color.getHSBColor((float)Math.random(), 1.0F, 1.0F); 
    if(isPresed){      
     g.setColor(randomColor); 
     g.fillOval(x-5, y-5, 10, 10); 
    } 
} 
+0

當我打電話super.paintComponent來了另一個問題,我不能用面板上的鼠標繪圖,因爲super.paintComponent將清除它 – 2012-08-18 22:20:09

+0

在你做之前先調用它你的繪畫 – MadProgrammer 2012-08-18 22:35:45

+0

所以你說我需要在mouseDragged(MouseEvent e)中調用它嗎? – 2012-08-18 22:51:26