2016-03-28 21 views
0

Java'da basit bir boya uygulaması üzerinde çalışıyorum, bir renk seçtiğimde düğme arka planı mükemmel bir şekilde değişir, ancak geri dönen renk her zaman bir öncekidir.JColorChooser daha önce seçilen renklere geri dön

Örneğin, siyahı mavi sonra seçtiğimde, siyah renkte çizilir. Ayrıca maviden sonra başka bir renk seçersem Mavi renkte boyar.

public class ColorChooserBtn extends JButton { 

    private Color color; 

    public ColorChooserBtn() { 
     super(); 
     this.setBackground(Color.BLACK); 
     this.setPreferredSize(new Dimension(16, 16)); 
     this.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       Color c = JColorChooser.showDialog(null, "Choose a Color", color); 
       if (c != null){ 
        setSelectedColor(c); 
        setBackground(color); 
       } 

      } 
     }); 
    } 
    public Color getSelectedColor() { 
     return color; 
    } 

    public void setSelectedColor(Color newColor) { 
     color = newColor; 
    } 

} 

public class Paint { 
DrawArea drawArea; 
JButton clearBtn; 
ColorChooserBtn colorBtn; 
ActionListener actionListener = new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == clearBtn){ 
      drawArea.clear(); 
     } else if(e.getSource() == colorBtn){ 
      drawArea.coloring(colorBtn.getSelectedColor()); 
     } 

    } 
}; 

public Paint() { 
    JFrame frame = new JFrame("Paint"); 
    frame.getContentPane().setLayout(new BorderLayout()); 
    drawArea = new DrawArea(); 
    frame.getContentPane().add(drawArea, BorderLayout.CENTER); 
    JPanel controls = new JPanel(); 
    clearBtn = new JButton("Clear"); 
    clearBtn.addActionListener(actionListener); 
    colorBtn = new ColorChooserBtn(); 
    colorBtn.addActionListener(actionListener); 

    controls.add(clearBtn); 
    controls.add(colorBtn); 

    frame.getContentPane().add(controls,BorderLayout.NORTH); 
    frame.setSize(600, 600); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 
public static void main(String[] args) { 

    new Paint(); 

} 

}

+0

Belki bir iş parçacığı sorunu selectedColor örneğin değiştiğinde bir propertyChanged olayı bir PropertyChangeListener kullanmak ve tetiklemek için olabilir? Renk değişkeninizi 'volatile' yapın ve tekrar deneyin. Emin değilim, işe yarıyor ... – Seelenvirtuose

+0

Yine de aynı problemi denedim. – edaddou

+0

Ardından, boyama için renk değişkenini kullandığınız kodu da göstermeniz gerekir. Gösterilen kod tamam görünüyor. – Seelenvirtuose

cevap

1

sorun ActionListener sitesindeki bildirilir sıradır. Genellikle, Salıncak aramaları ... bazı ek System.out s ile, bu ActionListener demektir

Get color 
null 
Choose color 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JColorChooser; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

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

    public class TestPane extends JPanel { 

     public TestPane() { 
      ColorChooserBtn btn = new ColorChooserBtn(); 
      add(btn); 
      btn.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        System.out.println("Get color"); 
        System.out.println(btn.getSelectedColor()); 
       } 
      }); 
     } 

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

    } 

    public class ColorChooserBtn extends JButton { 

     private Color color; 

     public ColorChooserBtn() { 
      super(); 
      this.setBackground(Color.BLACK); 
      this.setPreferredSize(new Dimension(16, 16)); 
      this.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        System.out.println("Choose color"); 
        Color c = JColorChooser.showDialog(null, "Choose a Color", color); 
        if (c != null) { 
         setSelectedColor(c); 
         setBackground(color); 
        } 
       } 
      }); 
     } 

     public Color getSelectedColor() { 
      return color; 
     } 

     public void setSelectedColor(Color newColor) { 
      color = newColor; 
     } 

    } 
} 

baskılar ... Aşağıdaki kodu kullanarak,

Yani LIFO sırayla dinler seçili rengi düğmeden "almak için" kullanılır, ilk olarak ActionListener'dan önce kullanılır; bu, gerçekte renk seçmek için kullanılır.

mümkün biri, çözüm ...

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JColorChooser; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

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

    public class TestPane extends JPanel { 

     public TestPane() { 
      ColorChooserBtn btn = new ColorChooserBtn(); 
      add(btn); 
      btn.addPropertyChangeListener("selectedColor", new PropertyChangeListener() { 
       @Override 
       public void propertyChange(PropertyChangeEvent evt) { 
        System.out.println("Changed"); 
        System.out.println(evt.getNewValue()); 
       } 
      }); 
     } 

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

    } 

    public class ColorChooserBtn extends JButton { 

     private Color color; 

     public ColorChooserBtn() { 
      super(); 
      this.setBackground(Color.BLACK); 
      this.setPreferredSize(new Dimension(16, 16)); 
      this.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        System.out.println("Choose color"); 
        Color c = JColorChooser.showDialog(null, "Choose a Color", color); 
        if (c != null) { 
         setSelectedColor(c); 
         setBackground(color); 
        } 
       } 
      }); 
     } 

     public Color getSelectedColor() { 
      return color; 
     } 

     public void setSelectedColor(Color newColor) { 
      if (newColor != color) { 
       Color oldColor = color; 
       color = newColor; 
       firePropertyChange("selectedColor", oldColor, newColor); 
      } 
     } 

    } 
} 
+0

Teşekkürler – edaddou

İlgili konular