的問題是其中ActionListener
的通知順序。一般情況下,搖擺電話LIFO順序聽
因此,使用下面的代碼,有一些額外的System.out
小號...
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;
}
}
}
打印...
Get color
null
Choose color
這意味着ActionListener
用於從按鈕「獲取」所選顏色的操作先被調用,然後用ActionListener
實際選擇顏色
一,可能的解決辦法可能是使用PropertyChangeListener
並引發propertyChanged
事件時selectedColor
改變,例如...
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);
}
}
}
}
也許一個線程問題?讓你的顏色變量爲volatile,然後重試。不知道,它的工作原理... – Seelenvirtuose
我試過了,仍然是同樣的問題。 – edaddou
然後,您還必須顯示您正在使用顏色變量進行繪畫的代碼。顯示的代碼似乎沒問題。 – Seelenvirtuose