c.setText放到CFrame构造函数里面就有用,放到ActionPerformed里面就没用,在ActionPerformed里面改变颜色可以,这是什么问题?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFirstClass
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
CFrame frame=new CFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

});
}
}class CFrame extends JFrame
{
public CFrame()
{
setSize(500,500);

JButton aButton = new JButton ("a");
JButton bButton =new JButton ("b");

c = new CComponent();
 
c.add(aButton);
c.add(bButton);

add(c); CAction aAction = new CAction ("a");
CAction bAction = new CAction ("b");

aButton.addActionListener(aAction);
bButton.addActionListener(bAction);
}
private class CAction implements ActionListener
{
public CAction (String text_Temp_CA)
{
text_CA = text_Temp_CA;
}

public void actionPerformed(ActionEvent event)
{
c.setText(text_CA);
}
private  String text_CA;
}
private CComponent c;
}class CComponent extends JPanel
{
public CComponent ()
{
text_CComponent="Defualt";
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(text_CComponent, 20, 20);
}
public void setText(String text_Temp_CC)
{
text_CComponent=text_Temp_CC;
}
public void setBackground(Color color)
{
super.setBackground(color);
}
private String text_CComponent;

}