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

📄 testmain.java

📁 一个用Java实现的记录本。用用看看 一个用Java实现的记录本。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			JButton jb4 = new JButton("取消");
			JCheckBox jr = new JCheckBox("区分大小写(C)");
			JPanel jp = new JPanel();
			
			Myfind mf=new Myfind();
			jb1.addActionListener(mf);
			jb2.addActionListener(mf);
			jb3.addActionListener(mf);
			jb4.addActionListener(mf);
			jf.setLayout(null);
			jl1.setBounds(20, 20, 100, 27);
			jt1.setBounds(100, 20, 120, 27);
			jl2.setBounds(20,50,100,30);
			jt2.setBounds(100,50,120,27);
			jb1.setBounds(225,20,120,27);
			jb2.setBounds(225,50,120,27);
			jb3.setBounds(225,80,120,27);
			jb4.setBounds(225,110,120,27);
			jr.setBounds(20,110,120,27);
			jf.add(jl1);
			jf.add(jl2);
			jf.add(jt1);
			jf.add(jt2);
			jf.add(jb1);
			jf.add(jb2);
			jf.add(jb3);
			jf.add(jb4);
			jf.add(jr);
			jf.setResizable(false);
			jf.setLocation(300,300);
			jf.setSize(370,180);
			jf.setVisible(true);
		}else if (e.getActionCommand().equals("全选(A)")){
			//testFrame.jta.selectAll();
		}else if (e.getActionCommand().equals("时间/日期(D)")){
			SimpleDateFormat sd = new SimpleDateFormat("HH:mm yyyy-MM-dd");	
			testFrame.jta.append(sd.format(new Date())); 
		}
	}	
}
class testCX implements UndoableEditListener{
	public void undoableEditHappened(UndoableEditEvent e) {
		UndoableEdit  um=e.getEdit();
		if(um.canUndo()){
			um.undo();
		}
	}
}
class testFile{
	public void testOpenFile(String fileName){
		try{
			FileReader fr=new FileReader(fileName);
			BufferedReader br=new BufferedReader(fr);
			String line=br.readLine();
			StringBuffer ss=new StringBuffer(line);
			while(line!=null)
			{
				line=br.readLine();
				ss.append("\r\n"+line);
			}
			String rr=new String(ss);
			StringBuffer rs=new StringBuffer(rr.length()-4);
			for(int i=0;i<rr.length()-4;i++)
				rs.append(rr.charAt(i));
			testFrame.jta.setText(rs.toString());//将打开的文件写入到文本框programe中
			fr.close();
			br.close();
		}catch (IOException e){
		}
	}
	public void testSaveFile(String fileName){
		String s = testFrame.jta.getText();
		try{
			File f = new File(fileName);
			if(f.exists()){
				FileWriter fw = new FileWriter(fileName);
					fw.write(s,0,s.length());
					fw.flush();
					fw.close();
			}else{
				f.createNewFile();
				FileWriter fw = new FileWriter(fileName);
					fw.write(s,0,s.length());
					fw.flush();
					fw.close();
			}
		}catch(Exception e){
		}
	}
}
class Myfind implements ActionListener{
	public void actionPerformed(ActionEvent e){
		String str=e.getActionCommand().toString();
		if(str.equals("查找")||str.equals("查找下一个")||str.equals("替换")){
			fun(str);
		}else if(str.equals("全部替换")){
			fun1(str);
		}
	}	
		public void fun(String str){
			char chr[]=testFrame.jta.getText().toCharArray();
			String sin=null,s=null;
			char c1[]=null,c2[]=null;
			int cha=0;
			if(str.equals("查找")){
				c1=testEditItem.jtf.getText().toCharArray();
			}else{
				c1=testEditItem.jt2.getText().toCharArray();
				sin=testEditItem.jt1.getText();
				c2=s.toCharArray();
				cha=c2.length-c1.length;
			}
			int x=0,tot=0;
			boolean b=false,b2=false;
			for(int i=testEditItem.f_int;i<chr.length;i++){
				if(chr[i]==c1[x]){
					x++;
					if(x==c1.length){
							tot=i+1;
							break;
					}
				}else{
					x=0;
					tot=0;	
					if(i==chr.length-1){
						JOptionPane.showMessageDialog(null,"已到文件末尾!")	;
						b=true;
						//TestFrame.bool=false;
					}
				}			
			}
				testFrame.jta.select(tot-c1.length,tot);
				try{
					if(str.equals("替换")&&b==false){
						testFrame.jta.replaceRange(s,tot-c1.length,tot);	
						testEditItem.f_int=tot+cha;
					}else{
						testEditItem.f_int=tot;	
					}
				}catch(Exception ex){}
	}
		public void fun1(String str){
			String s=testEditItem.jt2.getText();
			char chr[]=testFrame.jta.getText().toCharArray();
			char c1[]=testEditItem.jt1.getText().toCharArray();
			char c2[]=testEditItem.jt2.getText().toCharArray();
			int cha=c2.length-c1.length;
			int x=0,tot=0;
			for(int i=0;i<chr.length;i++){
				if(chr[i]==c1[x]){
					x++;
					if(x==c1.length){
							tot=i+1;
							try{
								testFrame.jta.select(tot-c1.length,tot);
								testFrame.jta.replaceRange(s,tot-c1.length,tot);	
								i=tot+cha-1;
								chr=testFrame.jta.getText().toCharArray();
								c1=testEditItem.jt1.getText().toCharArray();
								x=0;
								tot=0;
							}catch(Exception e){}
					}
				}else{
					x=0;
					tot=0;	
				}
				if(i==chr.length-1){	
						break;
				}	
			}
			JOptionPane.showMessageDialog(null,"已到文件末尾!")	;
	}
}
class testFontFrame{
	public static JTextField jt1,jt2,jt3,jt4;
	public static JList fontNames;
	public static List fl1,fl2;
	public static JFrame jf;
	public testFontFrame(){
		jf = new JFrame("字体设置");
		JLabel jl1 = new JLabel("字体(F) :");
		JLabel jl2 = new JLabel("字形(Y) :");
		JLabel jl3 = new JLabel("大小(S) :");
		fl1 = new List(6,false); 
		fl1.add("常规");
		fl1.add("斜体");
		fl1.add("粗体");
		fl1.add("粗斜体");
		fl2 = new List(6,false);
		for(int i=12;i<=64;i++){
			fl2.add(Integer.toString(i));
		}
		jt1 = new JTextField("Arial");
		jt2 = new JTextField("常规");
		jt3 = new JTextField("20");
		jt4 = new JTextField();
		TitledBorder tb = new TitledBorder("样式");
		JButton jb1 = new JButton("确定");
		JButton jb2 = new JButton("取消");
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
        fontNames = new JList(ge.getAvailableFontFamilyNames()); 	
		JScrollPane js1 = new JScrollPane(fontNames);
		jl1.setBounds(10,10,100,20);
		jt1.setBounds(10,30,130,20);
		jl2.setBounds(150,10,100,20);
		jt2.setBounds(150,30,100,20);
		jl3.setBounds(260,10,100,20);
		jt3.setBounds(260,30,70,20);
		jb1.setBounds(340,30,60,20);
		jb2.setBounds(340,55,60,20);
		js1.setBounds(10,55,130,120);
		fl1.setBounds(150,55,100,120);
		fl2.setBounds(260,55,70,120);
		jt4.setBounds(150,180,180,50);
		jt4.setBorder(tb);
		jt4.setText("AaBbYyZz");
		jt4.setHorizontalAlignment(JTextField.CENTER);
		jt4.setEditable(false);
		jf.add(jl1);
		jf.add(jt1);
		jf.add(jl2);
		jf.add(jt2);
		jf.add(jl3);
		jf.add(jt3);
		jf.add(jb1);
		jf.add(jb2);
		jf.add(js1);	
		jf.add(fl1);
		jf.add(fl2);
		jf.add(jt4);
		fontNames.addListSelectionListener(new testAction());
		fl1.addItemListener(new testList1());
		fl2.addItemListener(new testList2());
		jb1.addActionListener(new buttonAction());
		jb2.addActionListener(new buttonAction());
		jf.setLayout(null);
		jf.setSize(430,270);
		jf.setVisible(true);
	}
}
class buttonAction implements ActionListener{
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand().equals("确定")){
			//JTexdArea文本换字体
			setFont sf = new setFont();	
			Font ft = sf.getFont();
			testFrame.jta.setFont(ft);
			testFontFrame.jf.setVisible(false);
		}else if (e.getActionCommand().equals("取消")){
			testFontFrame.jf.setVisible(false);
		}
	}
}
class testAction implements ListSelectionListener{
	public void valueChanged(ListSelectionEvent e) {
		Object selectedFont = testFontFrame.fontNames.getSelectedValue(); 
		String st=(String)selectedFont;
		testFontFrame.jt1.setText(st);
		setFont sf = new setFont();	
		Font ft = sf.getFont();
		testFontFrame.jt4.setFont(ft);
	}
}
class testList1 implements ItemListener{
	public void itemStateChanged(ItemEvent e){
		testFontFrame.jt2.setText(testFontFrame.fl1.getSelectedItem());
		setFont sf = new setFont();	
		Font ft = sf.getFont();
		testFontFrame.jt4.setFont(ft);
	}	
}
class testList2 implements ItemListener{
	public void itemStateChanged(ItemEvent e){
		testFontFrame.jt3.setText(testFontFrame.fl2.getSelectedItem());
		setFont sf = new setFont();	
		Font ft = sf.getFont();
		testFontFrame.jt4.setFont(ft);
	}	
}
class setFont{
	public Font getFont(){
		String name = testFontFrame.jt1.getText();
		int size = Integer.parseInt(testFontFrame.jt3.getText());
		int style = 0;
		if(testFontFrame.jt2.getText().equals("常数")){
			style = 0;
		}else if (testFontFrame.jt2.getText().equals("粗体")){
			style = 1;
		}else if (testFontFrame.jt2.getText().equals("斜体")){
			style = 2;
		}else if(testFontFrame.jt2.getText().equals("粗斜体")){
			style = 3;
		}	
		Font ft = new Font(name,style,size);
		return ft;
	}
}
class PageableText implements Pageable, Printable {
  public static String FONTFAMILY = "Monospaced";
  public static int FONTSIZE = 10;
  public static int FONTSTYLE = Font.PLAIN;
  public static float LINESPACEFACTOR = 1.1f;
  PageFormat format;  
  Vector lines;      
  Font font;          
  int linespacing;   
  int linesPerPage;  
  int numPages;       
  int baseline = -1;   
  public PageableText(String text, PageFormat format) throws IOException { 
    this(new StringReader(text), format); 
  }
  public PageableText(File file, PageFormat format) throws IOException { 
    this(new FileReader(file), format); 
  }
  public PageableText(Reader stream, PageFormat format) throws IOException {
    this.format = format;
    BufferedReader in = new BufferedReader(stream);
    lines = new Vector();
    String line;
    while((line = in.readLine()) != null) 
      lines.addElement(line);
    font = new Font(FONTFAMILY, FONTSTYLE, FONTSIZE);
    linespacing = (int) (FONTSIZE * LINESPACEFACTOR);
    linesPerPage = (int)Math.floor(format.getImageableHeight()/linespacing);
    numPages = (lines.size()-1)/linesPerPage + 1;
  }
  public int getNumberOfPages() { return numPages; }
  public PageFormat getPageFormat(int pagenum) { return format; }
  public Printable getPrintable(int pagenum) { return this; }
  public int print(Graphics g, PageFormat format, int pagenum) {
    if ((pagenum < 0) | (pagenum >= numPages)) 
      return NO_SUCH_PAGE;
    if (baseline == -1) {
      FontMetrics fm = g.getFontMetrics(font);
      baseline = fm.getAscent();
    }
    g.setColor(Color.white);
    g.fillRect((int)format.getImageableX(), (int)format.getImageableY(),
               (int)format.getImageableWidth(), 
               (int)format.getImageableHeight());
    g.setFont(font);
    g.setColor(Color.black);
    int startLine = pagenum * linesPerPage;
    int endLine = startLine + linesPerPage - 1;
    if (endLine >= lines.size()) 
      endLine = lines.size()-1;
    int x0 = (int) format.getImageableX();
    int y0 = (int) format.getImageableY() + baseline;
    for(int i=startLine; i <= endLine; i++) {
      String line = (String)lines.elementAt(i);
      if (line.length() > 0) 
        g.drawString(line, x0, y0);
      y0 += linespacing;  
    }
    g.setColor(Color.red);
    g.drawString("test", 20, 20);
    return PAGE_EXISTS;
  }
  public static void dayin() {
   try{
	    PrinterJob job = PrinterJob.getPrinterJob();
	    PageFormat format = job.pageDialog(job.defaultPage());
	    //得到要打印的文件名
	    File f=new File(testFileItem.fileName);
	    System.out.print(f.getName().toString());
	    job.setPageable(new PageableText(f, format));
	    if (job.printDialog()) 
	      job.print();
	 }catch(Exception e5){}
  }
}

⌨️ 快捷键说明

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