class ChoiceListener implements ItemListener
{  public void itemStateChanged( ItemEvent e)
   {
  if(aComboBox.getSelectedItem()=="TimesRoman")
   {
    aTextArea.setFont(new  Font("TimesRoman",Font.PLAIN+Font.ITALIC,16));
   } 
else if(aComboBox.getSelectedItem()=="Courier")
   {
    aTextArea.setFont(new  Font("Courier",Font.ITALIC,5));
   } 
else if(aComboBox.getSelectedItem()=="楷体")
   {
    aTextArea.setFont(new  Font("楷体",Font.ITALIC,30));
   } 
else if(aComboBox.getSelectedItem()=="宋体")
   {
    aTextArea.setFont(new  Font("宋体",Font.ITALIC,20));
   } 
else if(aComboBox.getSelectedItem()=="Helvetica")
   {
    aTextArea.setFont(new  Font("Helvetica",Font.ITALIC,10));
   } 
aTextArea.requestFocus();
}class Reminder{
Timer timer;
JLabel jl;

public Reminder(open op) {  ////////////////////将open对象由此传入
jl=op.aLabel;      ////////////////////将aLabel赋值给jl
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), new Date(),1000);
}

class RemindTask extends TimerTask {
public void run() {
jl.setText((new Date()).toLocaleString());
}
}
}}
 public void actionPerformed(ActionEvent evt)
{
         String cmd=evt.getActionCommand();
if(cmd=="new")
{
aTextArea.setText("");
}
        else if(cmd=="open")
{
JFileChooser JFileChooser1 = new JFileChooser("D:\\");//启动一个文件选择器
if(JFileChooser1.APPROVE_OPTION==JFileChooser1.showOpenDialog(this))//如果文件选择完毕
{
openFile(JFileChooser1.getSelectedFile().getPath());//作为将来的接口
String filepath=JFileChooser1.getSelectedFile().getPath();//获取被选择文件的路径
System.out.println(filepath);//输出文件路径
}
}
//加上的保存功能
        else if (cmd=="save")
        {
          JFileChooser JFileChooser2 = new JFileChooser("D:\\");
          if(JFileChooser2.APPROVE_OPTION==JFileChooser2.showSaveDialog(this))//如果文件选择完毕
{
saveFile(JFileChooser2.getSelectedFile().getPath());//作为将来的接口
String filepath=JFileChooser2.getSelectedFile().getPath();//获取被选择文件的路径
System.out.println(filepath);//输出文件路径
}       }
 else if(cmd=="exit")
         {
           System.out.println("Exit");
           System.exit(0);
 
        } 
else if(cmd=="help")
         {
           JOptionPane.showMessageDialog(null,"我的第一个小程序!!!!","帮助",JOptionPane.INFORMATION_MESSAGE); 
        }   else if(cmd=="color")
  {
  Color color =JColorChooser.showDialog(this,"Foreground Color",aTextArea.getForeground());
   if(color!= null){
   aTextArea.setForeground(color);
  }
 } 
else if(cmd=="copy")
{
  String selection = aTextArea.getSelectedText();
StringSelection clipString = new StringSelection(selection);
aClipboard.setContents(clipString,clipString);}
else if(cmd=="paste")
  {
  Transferable clipData =
        aClipboard.getContents(open.this);
      try {
        String clipString = 
          (String)clipData.
            getTransferData(
              DataFlavor.stringFlavor);
        aTextArea.replaceRange(clipString, 
          aTextArea.getSelectionStart(), 
          aTextArea.getSelectionEnd());
      } catch(Exception ex) {
        System.out.println("not String flavor");
      }
}
else if(cmd=="cut")
{
String selection = aTextArea.getSelectedText();
      StringSelection clipString = 
        new StringSelection(selection);
      aClipboard.setContents(clipString, clipString);
      aTextArea.replaceRange("", 
        aTextArea.getSelectionStart(), 
        aTextArea.getSelectionEnd());
}aTextArea.requestFocus();
}
//打开方法
void openFile(String fileName) 
   {
try 
{
File file=new File(fileName);
int size =(int)file.length();
int chars_read=0;
FileReader in=new FileReader(file);
char[] data=new char[size];
while(in.ready())
{
chars_read+=in.read(data,chars_read,size-chars_read);
//read(目标数组、文件起始位置、文件结束位置)
//返回读入的数据量
}
in.close();
aTextArea.setText(new String(data,0,chars_read));
}
catch(IOException e)
{
System.out.println(e.toString());
  }
  }

解决方案 »

  1.   

    //保存方法
    void saveFile(String fileName)
       {
           try 
           {
            File file = new File(fileName);      
            BufferedWriter out = new BufferedWriter(new FileWriter(file)); 
          out.write(aTextArea.getText());
          out.flush();
          }
          catch (Exception ex)
          {
           System.out.println(ex.getMessage());
          }
       }       public static void main(String[] args)
        {
         new open();
        }
    }
      

  2.   

    new Reminder(this); //这一句什么意思-------------------------------------------this的作用生成Reminder类的一个对象啊,Reminder的构造函数不是public Reminder(open op) {}吗
    this 就是代表一个open类对象的引用!明白了吗我也是个初学者,能看懂人家程序,但编不出来啊!
    楼主,做个朋友吧,我们的水平差不多,看样有共同语言啊,我的qq:239202525
      

  3.   

    new Reminder(this); //这一句什么意思-------------------------------------------this的作用是应该是传递参数用的……这一句是new Reminder(this)  创建 Reminder方法……(this)应该是参数具体的请看下类的继承方面的书。super和this 的区别用法……