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

📄 kpad.java

📁 WINDOWS记事本的代码,在JAVA下运行
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	  			bColor=JColorChooser.showDialog(colorf,"改变背景颜色",new Color(204,204,204));
	  			theArea.setBackground(bColor);
	  		}
	  		
	  		if(cmd.equals("wCalculator"))
	  		{
	  			
	  		}
	
	      }
    }
	
	
	
    class MyUndoManager extends UndoManager
	{
		public void undoableEditHappened(UndoableEditEvent e)
		{
			UndoManager undo=getUndoManager();
			
			undo.addEdit(e.getEdit());
			new ActionSet("撤销",null).updateUndoState();
			new ActionSet("恢复",null).updateRedoState();
			
		}
	}
	
	class PopupPanel1 extends JPanel implements MouseListener,PopupMenuListener,ActionListener{
		
		public PopupPanel1(){
			
			Popup1=new JPopupMenu();
			
			undoPop=new JMenuItem (new ActionSet("撤消(U)",null));
			redoPop=new JMenuItem (new ActionSet("恢复(R)",null));
			JMenuItem cut=Popup1.add(new ActionSet("剪切(T)",null));
			JMenuItem copy=Popup1.add(new ActionSet("复制(C)",null));
			JMenuItem paste=Popup1.add(new ActionSet("粘贴(P)",null));			
			JMenuItem selectAll=Popup1.add(new ActionSet("全选(A)",null));
			
			undoPop.setEnabled(false);
			redoPop.setEnabled(false);			

			undoPop.setMnemonic('U');
			redoPop.setMnemonic('R');
			cut.setMnemonic('T');
			copy.setMnemonic('C');
			paste.setMnemonic('P');			
			selectAll.setMnemonic('A');
			
			undoPop.setActionCommand("Undo");
			redoPop.setActionCommand("Redo");
			cut.setActionCommand("Cut");
			copy.setActionCommand("Copy");
			paste.setActionCommand("Paste");			
			selectAll.setActionCommand("SelectAll");
			
			Popup1.add(undoPop);
			Popup1.add(redoPop);
			Popup1.addSeparator();
			Popup1.add(cut);
			Popup1.add(copy);
			Popup1.add(paste);			
			Popup1.addSeparator();
			Popup1.add(selectAll);		
			
			Popup1.setBorder(new BevelBorder(BevelBorder.RAISED));
			
			Popup1.addPopupMenuListener(this);
			theArea.addMouseListener(this);
			
		}
		
		public void mouseClicked(MouseEvent me){checkPopup(me);}
		public void mousePressed(MouseEvent me){checkPopup(me);}
		public void mouseReleased(MouseEvent me){checkPopup(me);}
		public void mouseEntered(MouseEvent me){}
		public void mouseExited(MouseEvent me){}
		
		private void checkPopup(MouseEvent me){
			if(me.isPopupTrigger()){
				Popup1.show(me.getComponent(),me.getX(),me.getY());
			}
		}
		
		public void popupMenuWillBecomeVisible(PopupMenuEvent e){}
		public void popupMenuWillBecomeInvisible(PopupMenuEvent e){}
		public void popupMenuCanceled(PopupMenuEvent e){}
		
		public void actionPerformed(ActionEvent e){}
		
		
	}
/*	
	class PopupPanel2 extends JPanel implements MouseListener,PopupMenuListener,ActionListener{
		
		public PopupPanel2(){
			
			Popup2=new JPopupMenu();
			JCheckBoxMenuItem lockToolBar=new JCheckBoxMenuItem(new ActionSet("锁定工具栏(T)",null));			

			lockToolBar.setMnemonic('T');			
			
			lockToolBar.setActionCommand("LockToolBar");
						
			Popup2.add(lockToolBar);
						
			Popup2.setBorder(new BevelBorder(BevelBorder.RAISED));
			
			Popup2.addPopupMenuListener(this);
			toolBar.addMouseListener(this);
			
		}
		
		public void mouseClicked(MouseEvent me){checkPopup(me);}
		public void mousePressed(MouseEvent me){checkPopup(me);}
		public void mouseReleased(MouseEvent me){checkPopup(me);}
		public void mouseEntered(MouseEvent me){}
		public void mouseExited(MouseEvent me){}
		
		private void checkPopup(MouseEvent me){
			if(me.isPopupTrigger()){
				Popup2.show(me.getComponent(),me.getX(),me.getY());
			}
		}
		
		public void popupMenuWillBecomeVisible(PopupMenuEvent e){}
		public void popupMenuWillBecomeInvisible(PopupMenuEvent e){}
		public void popupMenuCanceled(PopupMenuEvent e){}
		
		public void actionPerformed(ActionEvent e){}
		
		
	}
*/	
	class FontChooser extends JDialog implements ActionListener,ListSelectionListener,DocumentListener{
		    
		JDialog fc=new JDialog();
		JTextField customSize,preview=new JTextField("AaBbCc");
		JTextArea preWords=new JTextArea("AaBbCc",1,1);
		JButton ok=new JButton("确定");
		JButton cancel=new JButton("取消");
		JList font,attribute,sizeList;
		int oldFontIndex,oldAttributeIndex,oldSizeIndex;
		
		public FontChooser(){
		    
	     	
			Container fContentPane=fc.getContentPane();
			Box baseBox=Box.createHorizontalBox();
			fContentPane.add(baseBox);
			
			Box vBox1=Box.createVerticalBox();
			JLabel lb=new JLabel("字体");
			font=new JList(fontStr);
			font.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			oldFontIndex=getOldFontIndex();
			font.setSelectedIndex(oldFontIndex);
			font.addListSelectionListener(this);
			vBox1.add(Box.createVerticalStrut(10));
			vBox1.add(lb);			
			vBox1.add(new JScrollPane(font));	
			vBox1.add(Box.createVerticalStrut(15));
			
			Box vBox2=Box.createVerticalBox();
			Box hBox1=Box.createHorizontalBox();
			Box vBox3=Box.createVerticalBox();
			lb=new JLabel("字形");
			attribute=new JList(fontAttributeStr);
			attribute.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			oldAttributeIndex=getOldAttributeIndex();
			attribute.setSelectedIndex(oldAttributeIndex);
			attribute.addListSelectionListener(this);
			attribute.setMaximumSize(new Dimension(10,20));
			vBox3.add(Box.createVerticalStrut(10));
			vBox3.add(lb);
			vBox3.add(new JScrollPane(attribute));
			
			Box vBox4=Box.createVerticalBox();
			lb=new JLabel("字号");			
			sizeList=new JList(fontSizeStr);
			sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			oldSizeIndex=getOldSizeIndex();
			sizeList.setSelectedIndex(oldSizeIndex);
			sizeList.addListSelectionListener(this);
			sizeList.setMaximumSize(new Dimension(10,20));
			customSize=new JTextField(fontSizeStr[oldSizeIndex]);
			customSize.setMaximumSize(new Dimension(150,5));
			customSize.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					int custom=0;
					try{
					custom=new Integer(customSize.getText());					
					}catch(NumberFormatException ex){
						JOptionPane.showMessageDialog(null, "必须输入整数!", "输入错误", JOptionPane.ERROR_MESSAGE);
						custom=Integer.valueOf(fontSizeStr[oldSizeIndex]);
						customSize.setText(Integer.toString(custom));
					}
					if (custom>500||custom<1)
					{
						JOptionPane.showMessageDialog(null, "必须输入1~500之间的整数!", "输入错误", JOptionPane.ERROR_MESSAGE);
						custom=Integer.valueOf(fontSizeStr[oldSizeIndex]);
						customSize.setText(Integer.toString(custom));
					}
					Font newFont=new Font(fontStr[oldFontIndex],oldAttributeIndex,custom);
					customSize.selectAll();
					preview.setFont(newFont);					
				}
			});
			
			vBox4.add(Box.createVerticalStrut(10));
			vBox4.add(lb);
			vBox4.add(customSize);
			vBox4.add(new JScrollPane(sizeList));
			Box vBox5=Box.createVerticalBox();
			ok.addActionListener(this);
			vBox5.add(ok);
			vBox5.add(Box.createVerticalStrut(20));
			vBox5.add(cancel);
			cancel.addActionListener(this);
			Box hBox2=Box.createHorizontalBox();
			hBox2.setMinimumSize(new Dimension(500,20));
			Box vBox6=Box.createVerticalBox();
			
			preview.setFont(new Font(fontStr[oldFontIndex],oldAttributeIndex,new Integer(fontSizeStr[oldSizeIndex])));
			preview.setEditable(false);
			preview.setMinimumSize(new Dimension(500,20));
			preview.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,2),"预览",TitledBorder.CENTER,TitledBorder.TOP),BorderFactory.createBevelBorder(BevelBorder.LOWERED)));
			vBox6.add(preview);
			lb=new JLabel("输入预览字符");
			lb.setAlignmentX(Component.CENTER_ALIGNMENT);
			preWords.setMaximumSize(new Dimension(500,5));
			preWords.getDocument().addDocumentListener(this);		
			vBox6.add(lb);
			vBox6.add(preWords);	
			vBox6.add(Box.createVerticalStrut(20));
			
			
			
			
			
			baseBox.add(Box.createHorizontalStrut(20));
			baseBox.add(vBox1);
			baseBox.add(Box.createHorizontalStrut(30));
			baseBox.add(vBox2);
			baseBox.add(Box.createHorizontalStrut(20));
			vBox2.add(hBox1);
			vBox2.add(Box.createVerticalStrut(10));
			vBox2.add(hBox2);			
			hBox1.add(vBox3);
			hBox1.add(Box.createHorizontalStrut(20));
			hBox1.add(vBox4);
			hBox1.add(Box.createHorizontalStrut(30));
			hBox1.add(vBox5);
			hBox2.add(vBox6);
			
			
			fc.setTitle("字体选择");
			fc.setBounds(150,150,500,400);
			fc.setResizable(false);
			fc.setVisible(true);
			fc.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					close();
				}
			});
			
			
		}
		
		public void save(){
			newFont=preview.getFont();
			theArea.setFont(newFont);
			jcbfont.setSelectedIndex(getOldFontIndex());
			jcbfontSize.setSelectedIndex(getOldSizeIndex());
			switch(getOldAttributeIndex()){
			    case 0:
			    	boldJtb.setSelected(false);
			    	italicJtb.setSelected(false);			    	
			    	break;
		    	case 1:
		    		boldJtb.setSelected(true);
			    	italicJtb.setSelected(false);
		    		break;
		    	case 2:
		    		boldJtb.setSelected(false);
			    	italicJtb.setSelected(true);
		    		break;
		    	case 3:
		    		boldJtb.setSelected(true);
			    	italicJtb.setSelected(true);
		    	    break;
		    }
			
			
		}
        
		public void close(){
			fc.setVisible(false);
		}
							
		public void previewChanged(DocumentEvent de){
			preview.setText(preWords.getText());
		}
		
		public void changedUpdate(DocumentEvent de){
			previewChanged(de);
		}
		
		public void insertUpdate(DocumentEvent de){
			previewChanged(de);
		}
		
		public void removeUpdate(DocumentEvent de){
			previewChanged(de);
		}
		
		
		public void valueChanged(ListSelectionEvent le){
			Font newFont;			
			int index1,index2,index3;
			index1=font.getSelectedIndex();
			index2=attribute.getSelectedIndex();
			index3=sizeList.getSelectedIndex();
			customSize.setText(fontSizeStr[index3]);
			newFont=new Font(fontStr[index1],index2,new Integer(fontSizeStr[index3]));
			preview.setFont(newFont);
		}
		public void actionPerformed(ActionEvent e){
			
			
			if (e.getSource()==cancel){
				close();
			}
			
			if (e.getSource()==ok){
				save();
				close();
			}
		}
		
	}
	
	class JAVAFileFilter extends FileFilter
    {
    	String ext;
    	public JAVAFileFilter(String ext)
    	{
    		this.ext=ext;
    	}
    	
    	public boolean accept(File file)
    	{
    		if (file.isDirectory())
    			return true;
    		String fileName=file.getName();
    		int index=fileName.lastIndexOf('.');
    		if(index>0&&index<fileName.length()-1){
    			String extension=fileName.substring(index+1).toLowerCase();
    			if(extension.equals(ext))
    				return true;
    		}
    		return false;
    	}
    	
    	public String getDescription(){
    		if(ext.equals("txt"))
    			return "文本文件(*.txt)";
    		return "";
    	}
    }
}

    
    

⌨️ 快捷键说明

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