import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JColorChooser extends JFrame implements ActionListener
{
JColorChooser()
{
setSize(500,500);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton b=new JButton("color");
add(b);
b.addActionListener(this);
setVisible(true);
}

public void ActionPerformed(ActionEvent e)
{
Color initialcolor=Color.WHITE;
Color y=JColorChooser.showDialog(this,"Select a color",initialcolor);
setBackground(y);
}

public static void main(String args[])
{
JColorChooser x=new JColorChooser();
}
}


我收到以下两个错误:

1.)

C:\Users\g0ku\Desktop\New folder\swing>javac JColorChooser.java
JColorChooser.java:5: error: JColorChooser is not abstract and does not override
 abstract method actionPerformed(ActionEvent) in ActionListener
public class JColorChooser extends JFrame implements ActionListener
       ^


2.)

JColorChooser.java:21: error: cannot find symbol
Color y=JColorChooser.showDialog(this,"Select a color",initialcolor);
                     ^
  symbol:   method showDialog(JColorChooser,String,Color)
  location: class JColorChooser
2 errors

最佳答案

1。

public void ActionPerformed(ActionEvent e)


需要是

public void actionPerformed(ActionEvent e)


2。
    JFrame没有showDialog方法。我的猜测是您想要这样:

 JOptionPane.showInputDialog();

07-24 09:27