⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fristwindow.java

📁 有打开文件、保存文件、自动换行、复制、剪切、设置字体等功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	   con=getContentPane();       //取得一个面板的引用
	   con.add(pane,BorderLayout.SOUTH);
	   con.add(new JScrollPane(text1),BorderLayout.CENTER);
	   con.add(tbar,BorderLayout.NORTH);	                          
	   con.validate();
	   validate();                                         //刷新
	   setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  //设置单击窗体右上关闭图标后程序所做的处理
       
   }
   /*public void times()
   {	   
	    nowTime=new Date();
		matter=new SimpleDateFormat("现在时刻: yyyy年MM月Edd日 HH:mm:ss EE");
		timeS=matter.format(nowTime);
		
   }*/
   public void actionPerformed(ActionEvent e)
   {
     if(e.getSource()==itemCopy||e.getSource()==item2_1||e.getSource()==button[6])       //复制
	     text1.copy();
     else if(e.getSource()==itemCut||e.getSource()==item2_2||e.getSource()==button[7])  //剪切
	     text1.cut();
     else if(e.getSource()==itemPaste||e.getSource()==item2_3||e.getSource()==button[9]) //粘贴
	     text1.paste();
     else if(e.getSource()==item3_1)
     {
    	 text1.append("\n");
     }
     else if(e.getSource()==itemCalender)
     {
    	 new CalenderDialog();
     }
     else if(e.getSource()==itemPrint)
     {
    	new PrintDialog("打印"); 
     }
     else if(e.getSource()==itemIE)
     {
    	 new IEBorrower("IE");
     }
     else if(e.getSource()==timers)
     {
    	nowTime=new Date();
 		matter=new SimpleDateFormat("现在时刻: yyyy年MM月Edd日 HH:mm:ss EE");
 		timeS=matter.format(nowTime);
 		labelTime.setText(timeS);
     }
     //保存文件 
     else if(e.getSource()==item1_2||e.getSource()==button[2])
     {  
  	   int m=0;
  	   File file=new File("Text1");
  	   if(file.exists())
  		  m=JOptionPane.showConfirmDialog(con,"文件已存在,是否覆盖?","系统信息",JOptionPane.YES_NO_OPTION);
  	   if(m==JOptionPane.YES_OPTION) 
  	   {
  		  try
  		  {                            
  		      FileWriter tofile=new FileWriter(file+".txt");
  		      BufferedWriter out=new BufferedWriter(tofile);
  			  text1.write(out);   
  			  out.flush();		
  			  out.close();		         
  			  tofile.close();
  			
  			  JOptionPane.showMessageDialog(con,"文件已保存!","系统信息",JOptionPane.INFORMATION_MESSAGE);
  		   }
  		   catch(IOException ee)    
  		      {  JOptionPane.showMessageDialog(con,"该文件不存在","警告",JOptionPane.WARNING_MESSAGE);  }
          }
  	   else 
  	   {   
  	      int n=fileChooser.showSaveDialog(con);
	          if(n==JFileChooser.APPROVE_OPTION)
	          {      
	              File file1=fileChooser.getSelectedFile(); 
		          try
		          {  
		              FileWriter tofile=new FileWriter(file1+".txt");
			          BufferedWriter out=new BufferedWriter(tofile);
			          text1.write(out);         	out.flush();		
			          out.close();		        tofile.close();
		          }
		          catch(IOException ee)    
			      {
			          text1.setText("你没有保存文件");
			      }
            }  
         }
     }  
  //另存为文件  
      else if(e.getSource()==itemSaveAs||e.getSource()==button[3])
  	{                                                  
  	   int n=fileChooser.showSaveDialog(con);
  	   if(n==JFileChooser.APPROVE_OPTION)
  	   {      
  	      File file=fileChooser.getSelectedFile(); 
  		  try
  		  {  
  		      FileWriter tofile=new FileWriter(file+".txt");
  			  BufferedWriter out=new BufferedWriter(tofile);
  			  text1.write(out);         	
  			  out.flush();		
  			  out.close();		        
  			  tofile.close();
  		  }
  		  catch(IOException ee)    
  			  {  text1.append("你没有保存文件");  }	
  	      }
      } 
      else if(e.getSource()==item1_3)
      {
    	  exit.setVisible(true);
      }
      else if(e.getSource()==button[0])
      {
    	
    	  if(text1.getText()!=null){
    		  int n=fileChooser.showSaveDialog(con);
    	  	   if(n==JFileChooser.APPROVE_OPTION)
    	  	   {      
    	  	      File file=fileChooser.getSelectedFile(); 
    	  		  try
    	  		  {  
    	  		      FileWriter tofile=new FileWriter(file+".txt");
    	  			  BufferedWriter out=new BufferedWriter(tofile);
    	  			  text1.write(out);         	
    	  			  out.flush();		
    	  			  out.close();		        
    	  			  tofile.close();
    	  		  }
    	  		  catch(IOException ee)    
    	  			  {  
    	  			  text1.append("你没有保存文件");  
    	  			  }	
    	  	      }
    	  	 text1.setText("");
    	  }else 
    		  text1.setText("");
      }
      else if(e.getSource()==titletext)
     {
    	 this.setTitle(titletext.getText());                  //使文本框内容为窗体标题
     }
     else if(e.getSource()==passwordtext)
     {
    	 char c[]=passwordtext.getPassword();                //使密码框内容在文本框中显示
    	 titletext.setText(new String(c));
     }
     else if(e.getSource()==button[4])                     //颜色对话框
     {
    	Color newColor=JColorChooser.showDialog(this,"调色板",text1.getForeground());    	
    	text1.setForeground(newColor);
     }
     else if(e.getSource()==button[1]||e.getSource()==item1_1)                    //“打开文件”对话框
     {
    	 
    	 text1.setText(null);
    	 int n=fileChooser.showOpenDialog(con);
    	 if(n==JFileChooser.APPROVE_OPTION)
    	 {
    		 File file=fileChooser.getSelectedFile();
    		 try
    		 {
    			 FileReader readfile=new FileReader(file);    			 
    			 BufferedReader in=new BufferedReader(readfile);
    			 String  s=null;
    			 while((s=in.readLine())!=null)
    			 {
    				 text1.append(s+"\n");
    			 }
    		 }
    		 catch(IOException ee)
    		 {
    			 text1.setText("你没有选择文件");
    		 }
    	 }
    	 
     }
     
    /* else if(e.getSource()==buttonPlay)               //消息框
     {
    	 JOptionPane.showMessageDialog(this,"嘿嘿,正在开发中~!~!","娱乐框",JOptionPane.WARNING_MESSAGE);
     }
     else if(e.getSource()==buttonDialog)            //测试对话框 
     {
    	 dialog.setVisible(true);
    	 if(dialog.getMessage()==MyDialog.YES)
    	 {
    		 text1.append("\n 你点击了对话框");
    	 }
    	 else if(dialog.getMessage()==MyDialog.NO)
    	 {
    		 text1.append("\n 你点击了对话框的NO按钮");
    	 }
    	 else if(dialog.getMessage()==MyDialog.CLOSE)
    	 {
    		 text1.append("\n 你关闭了对话框");
    	 }
     }*/
     else if(e.getSource()==button[5]||e.getSource()==item3_2)          //字体对话框
     {
    	 fontdialog.setVisible(true);    	 
    	 text1.setFont(fontdialog.getfont());
     }
    }
}

/**
 * 退出窗体
 * 
 * 
 * 
 * */

class ExitDialog extends JDialog implements ActionListener
{    
    
	private static final long serialVersionUID = 8907481437601754735L;
	JButton  byes,bno;
	JLabel  labelMes1=new JLabel("是否真的退出");
	
    ExitDialog()
    {    
    	
    	 Container con=getContentPane();
    	 con.setLayout(null);
    	 setBounds(60,60,200,200);
    	 labelMes1=new JLabel("是否真的退出",JLabel.CENTER);
    	 byes=new JButton("是");
    	 byes.addActionListener(this);
    	 bno=new JButton("否");
    	 bno.addActionListener(this);
    	 con.add(byes);
    	 con.add(bno);
    	 con.add(labelMes1);
    	 labelMes1.setBounds(20, 20, 160, 30);
    	 byes.setBounds(10, 60, 80, 30);
    	 bno.setBounds(110, 60, 80, 30);
    	 con.validate();
    	 validate();
    	 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	
    	 
    }
  public void actionPerformed(ActionEvent e) {
		if(e.getSource()==byes)
		{
			System.exit(0);
		}
		else if(e.getSource()==bno)
		{
			setVisible(false);
		}
	}

	
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -