我有兩個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
我試圖上傳源代碼,但我不能。我不知道如何上傳 – 2012-08-18 16:01:51
你可以[編輯你的問題](http://stackoverflow.com/posts/12019996/edit)(鏈接下面的問題),並直接在你的問題中複製相關的代碼。 – assylias 2012-08-18 16:02:49
哎呀!你的編輯無法提交,因爲...這就是我得到的 – 2012-08-18 16:08:23