Em có 2 đoạn code như sau và không hiểu tại sao cái thứ nhất lại sai
Nó báo lỗi tại phương thức JColorChooser.showDialog(); và nguyên nhân là do cái this em truyền vào
Nhưng theo em thì trong cả 2 đoạn code trên thì nó đều là đối tượng thuộc kiểu ActionListener.
Em nói sai chỗ nào mong mọi người chỉ giáo ạ!
#Code1
public class JColorChooserDemo extends JFrame{
JButton b;
Container c;
public JColorChooserDemo() {
c = getContentPane();
c.setLayout(new FlowLayout());
b = new JButton("Color");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color init = Color.RED;
Color color = JColorChooser.showDialog(this, "Chooser a color", init);
c.setBackground(color);
}
});
}
}
#Code2
public class JColorChooserDemo extends JFrame implements ActionListener{
JButton b;
Container c;
public JColorChooserDemo() {
c = getContentPane();
c.setLayout(new FlowLayout());
b = new JButton("Color");
b.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Color init = Color.RED;
Color color = JColorChooser.showDialog(this, "Chooser a color", init);
c.setBackground(color);
}
}