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();
}
}
Belki bir iş parçacığı sorunu
selectedColor
örneğin değiştiğinde birpropertyChanged
olayı birPropertyChangeListener
kullanmak ve tetiklemek için olabilir? Renk değişkeninizi 'volatile' yapın ve tekrar deneyin. Emin değilim, işe yarıyor ... – SeelenvirtuoseYine de aynı problemi denedim. – edaddou
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