我编写了两个类,一个是实现计算器的功能,一个是颜色选择ColorChooserDemo,我现在想把颜色选择的类添加到计算器类中,就是说通过计算器中的一个按钮进入颜色选择器,进入颜色选择器后选择了颜色后,返回到计算器,并使计算器文本框的内容变成了选择的颜色,该怎么做呢,请高手解惑万分感激,为你祈祷一生。
计算器类:
package com.wwq.www;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;import java.util.Calendar;
public class myframe extends JFrame implements ActionListener {
    

private static final long serialVersionUID = 1L;

public JTextField tf0,tf1;
    private double arg=0;
    private String op="=";
    private boolean start=true;
    private Calendar now=Calendar.getInstance();

private JFrame f=new JFrame("我的计算器");
 
 public  void init()
{
    f.setLayout(new BorderLayout()); 
            tf0=new JTextField(10);
            tf0.setEditable(false);
            tf1=new JTextField(10);
            tf1.setEditable(false);
           
           f.add(tf0,"North");
           JPanel p1=new JPanel();
           p1.setLayout(new GridLayout(5,5));
           String str[]={"In2","cos","sin","tan","-","sqrt","1","2","3",
                  "+","N[2]","4","5","6","*","log","7","8","9","/",
                   "%","0","=","CE","背景色"};
           JButton btn0[]=new JButton[str.length];
        for(int i=0;i<str.length;i++)
        {
        btn0[i]= new JButton(str[i]);
        p1.add(btn0[i]);
        btn0[i].addActionListener(this);
        
      
        }
       btn0[23].addActionListener(new ActionListener()
   {
   public void actionPerformed(ActionEvent e)
   {   
   tf0.setText("");
   op="=";
   start=true;
   }
   });
       btn0[24].addActionListener(new ActionListener()
   {
       public void actionPerformed(ActionEvent evt)
           {       
        JFrame.setDefaultLookAndFeelDecorated(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane=new ColorChooserDemo();
        newContentPane.setOpaque(true);
        f.setContentPane(newContentPane);
                   
        }           
           });
       
   f.add(p1,"South");
           JPanel p2=new JPanel();
           p2.setLayout(new GridLayout(1,1));
           JButton btn1=new JButton("当前时间");
           btn1.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent evt)
            {
            ;
            Calendar now=Calendar.getInstance();
        
         if(start)
            { try
          {
         int hh,mm,ss,dd;
         hh=now.get(Calendar.HOUR_OF_DAY);
         mm=now.get(Calendar.MINUTE);
         ss=now.get(Calendar.SECOND);
         dd=now.get(Calendar.DATE);
         String k[]={"一","二","三","四","五","六","日"};
        
                tf1.setText(hh+":"+mm+":"+ss+" "+"星期"+k[dd-1]);
                start=false;
         //tf2.setText(ap+"年"+m+"月"+d+"日");
        
         }
        
         catch(Exception e)
         {
         System.out.println(e);
         }
         }
         else
         {
         try
         {
         int yy,mon,da;
         yy=now.get(Calendar.YEAR);
         mon=now.get(Calendar.MONTH)+1;
         da=now.get(Calendar.DAY_OF_MONTH);
         tf1.setText(" "+yy+"年"+mon+"月"+da+"日"+"");
         start=true;
         }
         catch(Exception e)
             {
             System.out.println(e);
             }
         }
            } 
            
            });
                       p2.add(btn1,"West");
        p2.add(tf1,"East");
        f.add(p2,"Center");
      
      
     try
    {
    int hh1,mm1,ss1,dd1;
    hh1=now.get(Calendar.HOUR_OF_DAY);
    mm1=now.get(Calendar.MINUTE);
    ss1=now.get(Calendar.SECOND);
    dd1=now.get(Calendar.DATE);
    String k[]={"一","二","三","四","五","六","日"};
   
    String x=hh1+":"+mm1+":"+ss1+" "+"星期"+k[dd1-1];
       tf1.setText(x);
     }
   
    catch(Exception e)
    {
    System.out.println(e);
    } 
  
           f.setVisible(true);
           f.pack();
           f.addWindowListener(new MyWindowListener()); 

           
}

public static void main(String[] s)
{  

myframe mf=new myframe();
mf.init();

}

public void actionPerformed(ActionEvent arg0) {

String s=arg0.getActionCommand();
   
if('0'<=s.charAt(0)&&s.charAt(0)<='9')
{
if(start) tf0.setText(s);
else tf0.setText(tf0.getText()+s);
start=false;
}

else{
if(start){
if(s.equals(".")){
tf0.setText(tf0.getText()+s); 
start=false;
}
else op=s;
}
else{
calculate(Integer.parseInt(tf0.getText()));
op=s;
start=true;
}
}

}
  


public void calculate(double n){
if(op.equals("+")) 
arg+=n;
else if(op.equals("%"))
arg%=n;
else  if(op.equals("-"))
arg-=n;
else if(op.equals("*"))
arg*=n;
else if(op.equals("/"))
arg/=n;
else if(op.equals("="))
arg=n;
else if(op.equals("sqrt"))
arg=Math.sqrt(n);
else if(op.equals("cos"))
arg=Math.cos((n*Math.PI)/180);
else if(op.equals("sin"))
arg=Math.sin((n*Math.PI)/180);
else if(op.equals("tan"))
arg=Math.tan((n*Math.PI)/180);
else if(op.equals("In2"))
arg=Math.exp(n);
else if(op.equals("N[2]"))
arg=Math.pow(arg,n);
else if(op.equals("log"))
arg=Math.log(n);
tf0.setText(""+arg);
}
}
颜色类
package com.wwq.www;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class ColorChooserDemo extends JPanel implements ChangeListener { /**
 * 
 */
private static final long serialVersionUID = 1L;
public  JColorChooser tcc;
protected JLabel banner;

public ColorChooserDemo() {
// TODO Auto-generated method stub
super(new BorderLayout());
banner=new JLabel("颜色变换",JLabel.CENTER);
banner.setForeground(Color.black);
banner.setBackground(Color.blue);
banner.setOpaque(true);
banner.setPreferredSize(new Dimension(20,20));
JPanel bannerPanel=new JPanel(new BorderLayout());
bannerPanel.add(banner,BorderLayout.CENTER);
bannerPanel.setBorder(BorderFactory.createTitledBorder("效果图"));
tcc=new JColorChooser(banner.getForeground());
tcc.getSelectionModel().addChangeListener(this);
tcc.setBorder(BorderFactory.createTitledBorder("选择颜色"));
add(bannerPanel,BorderLayout.CENTER);
add(tcc,BorderLayout.PAGE_START);
JButton jb1=new JButton("确定");
JButton jb2=new JButton("取消");
add(jb1,BorderLayout.LINE_START);
add(jb2,BorderLayout.AFTER_LINE_ENDS);
jb1.addActionListener(new ActionListener()
   {
         public void actionPerformed(ActionEvent evt)
         {
         System.exit(0);
     
            
              
         }
   }
);
}
public void stateChanged(ChangeEvent e)
{
Color newColor=tcc.getColor();
    banner.setBackground(newColor);
}
   
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame=new JFrame("颜色选择框");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane=new ColorChooserDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);

}
}