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

📄 mytext.java

📁 java开发的类似于windows写字板的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			text.setFont(f);
		}
	}

	public void newF(){

		if(!(t.getText().equals("文档已更改,注意保存!"))){
			text.setText("");
			t.setText("");
			this.setTitle("未命名文档");
		}
		else{

			int n=JOptionPane.showConfirmDialog(this,"文件已更改,是否保存?","确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
			if(n==JOptionPane.YES_OPTION) {
				save();
				text.setText("");
				t.setText("");
				this.setTitle("未命名文档");
			}
			else if(n==JOptionPane.NO_OPTION) {
				text.setText("");
				t.setText("");
				this.setTitle("未命名文档");
			}
			else if(n==JOptionPane.CANCEL_OPTION) {}
		}
	}

	public void open(){
		if(!(t.getText().equals("文档已更改,注意保存!"))){
			open0();
		}
		else{

			int n=JOptionPane.showConfirmDialog(this,"文件已更改,是否保存?","确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
			if(n==JOptionPane.YES_OPTION) {
				save();
				open0();
			}
			else if(n==JOptionPane.NO_OPTION) {
				open0();
			}
			else if(n==JOptionPane.CANCEL_OPTION) {}
		}
	}

	public void open0(){
		String t=new String("未命名文档");
		JFileChooser fileC;
		text.setText("");
		fileC=new JFileChooser("c:/");
		int n=fileC.showOpenDialog(null);
		if(n==JFileChooser.APPROVE_OPTION){
			File fp=fileC.getSelectedFile();
			t=fp.toString();
			try{
				FileReader readf=new FileReader(fp);
				BufferedReader in= new BufferedReader(readf);
				String s=null;
				while((s=in.readLine())!=null)
					text.append(s+"\n");

			}
			catch(IOException ee){}

		}
		this.setTitle(t);

	}

	public void save(){
		if(t.getText()!=null){
			if(!(this.getTitle().equals("未命名文档"))){
				File f=new File(this.getTitle());
				save0(f);

			}
			else{
				saveAs();
			}
		}
	}

	public void saveAs(){
		JFileChooser fileC;
		fileC=new JFileChooser("c:/");
		int option=fileC.showSaveDialog(null);
		if(option==JFileChooser.APPROVE_OPTION){
			File f=fileC.getSelectedFile();
			save0(f);
		}else if(option==JFileChooser.CANCEL_OPTION){}
	}

	public void save0(File f){

		BufferedWriter bw=null;
		try {
			bw=new BufferedWriter(new FileWriter(f));
			bw.write(text.getText());
			bw.flush();
			bw.close();
			this.setTitle(f.toString());
			t.setText("");

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public void font(){

		String name=(String)listFont.getSelectedItem();
		Font f=new Font(name,text.getFont().getStyle(),text.getFont().getSize());
		text.setFont(f);


	}

	public void fontSize(){
		int s=(int)(listFSize.getSelectedIndex()+1);
		Font f=new Font(text.getFont().getName(),Font.PLAIN,s);
		text.setFont(f);
	}

	public void huanH(){
		if(itHuanh.getLabel().equals("      自动换行")){
			itHuanh.setLabel(" √自动换行" );
			text.setLineWrap(true);
		}
		else {
			itHuanh.setLabel("      自动换行" );
			text.setLineWrap(false);
		}
	}

	public void colorF(){
		Color newColor=JColorChooser.showDialog(this,"调色板",text.getForeground());
		if(newColor!=null)
			text.setForeground(newColor);
	}
	public void colorB(){
		Color newColor=JColorChooser.showDialog(this,"调色板",text.getBackground());
		if(newColor!=null)
			text.setBackground(newColor);
	}

	public void keyP(){

		if(!(t.getText().equals("文档已更改,注意保存!"))){
			t.setText("文档已更改,注意保存!");
		}
	}


	public void itemStateChanged(ItemEvent e) {
		if(e.getSource()==listFont)
			font();
		else if(e.getSource()==listFSize)
			fontSize();

	}

}

class fontText extends JDialog implements ActionListener,ListSelectionListener{
	JDialog  fontDialog;
	JList fontList,sizeList,styleList;
	JButton queding,quxiao;
	FWindow window;
	JTextField fontF,sytleF,sizeF;
	JScrollPane fontP,sytleP,sizeP;
	JTextField fontExample;
	Font fonts;
	Color setColor;

	Scrollbar redSlider,greenSlider,blueSlider;
	JTextField colorLabel;

	public fontText(FWindow window) {
		this.window=window;

		fontDialog=new JDialog(window,"字体设置");
		this.setVisible(true);
		this.setResizable(false);
		this.setModal(true);	
		this.setBounds(200,100,550,400);
		this.setLayout(null);
		queding=new JButton("确定");
		this.add(queding);
		queding.setBounds(460, 70, 60, 30);///////////////
		quxiao=new JButton("取消");
		this.add(quxiao);
		quxiao.setBounds(460, 120, 60, 30);

		setFontList();

		validate();
		queding.addActionListener(this);
		quxiao.addActionListener(this);

	}
	void setFontList(){
		JLabel fontLab,styleLab,sizeLab,exampleLab;
		JButton fColorB,bColorB;


		fontLab=new JLabel("字体:");
		styleLab=new JLabel("字形:");
		sizeLab=new JLabel("大小:");
		exampleLab=new JLabel("示例:");


		this.add(fontLab);
		fontLab.setBounds(20,30,140,20);
		this.add(styleLab);
		styleLab.setBounds(180,30,120,20);
		this.add(sizeLab);
		sizeLab.setBounds(320,30,120,20);
		this.add(exampleLab);
		exampleLab.setBounds(250,220,50,20);

		fColorB=new JButton("前景色");
		bColorB=new JButton("背景色");
		this.add(fColorB);
		fColorB.setBounds(151,240,80,30);

		this.add(bColorB);
		bColorB.setBounds(151,300,80,30);


		Color color=new Color(200,200,200);
		GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
		String fontName[]=ge.getAvailableFontFamilyNames();
		String ff="宋体";
		int j;
		j=fontName.length;
		for(int i=0;i<j/2;i++){
			ff=fontName[i];
			fontName[i]=fontName[j-i-1];
			fontName[j-i-1]=ff;
		}
		fontList=new JList(fontName);
		fontList.setSelectionBackground(color);
		fontList.setSelectedIndex(0);


		fontP=new JScrollPane(fontList);
		fontP.setPreferredSize(new Dimension(140,200));
		this.add(fontP);
		fontP.setBounds(20, 80, 140,100);

		String fontStyles[]={"常规","粗体","斜体","粗斜体"};
		styleList=new JList(fontStyles);
		styleList.setSelectionBackground(color);
		styleList.setSelectedIndex(0);
		sytleP=new JScrollPane(styleList);
		sytleP.setPreferredSize(new Dimension(140,200));
		this.add(sytleP);
		sytleP.setBounds(180, 80, 120,100);


		String fontSizes[]={"8", "10", "12", "14", "16", 
				"18", "20", "24", "30", "36", "40", 
				"48", "60", "72"};

		sizeList=new JList(fontSizes);
		sizeList.setSelectionBackground(color);
		sizeList.setSelectedIndex(2);
		sizeP=new JScrollPane(sizeList);
		sizeP.setPreferredSize(new Dimension(140,200));
		this.add(sizeP);
		sizeP.setBounds(320, 80, 120,100);

		fontF=new JTextField();
		fontF.setSize(120, 30);
		fontF.setText(fontList.getSelectedValue().toString());
		this.add(fontF);
		fontF.setBounds(20, 58, 140, 20);

		sytleF=new JTextField();
		sytleF.setSize(100, 30);
		sytleF.setText(styleList.getSelectedValue().toString());
		this.add(sytleF);
		sytleF.setBounds(180, 58, 120, 20);

		sizeF=new JTextField();
		sizeF.setSize(100, 30);
		sizeF.setText(sizeList.getSelectedValue().toString());
		this.add(sizeF);
		sizeF.setBounds(320, 58, 120, 20);



		fontExample=new JTextField("我的写字板",JLabel.CENTER);
		fontExample.setBackground(new Color(255,255,255));
		fontExample.setEditable(false);
		fontExample.setHorizontalAlignment(JTextField.CENTER);

		this.add(fontExample);
		fontExample.setBounds(250, 250,230, 80);

		bColorB.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				fontExample.setBackground(colorLabel.getBackground());
			}
		});
		fColorB.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				fontExample.setForeground(colorLabel.getBackground());
			}
		});

		fontList.addListSelectionListener(this);
		styleList.addListSelectionListener(this);
		sizeList.addListSelectionListener(this);

		colorBar();

	}

	private void colorBar(){
		colorLabel=new JTextField();
		add(colorLabel);
		colorLabel.setBounds(20,200,75,30);
		colorLabel.setBackground(new Color(0,0,0));
		colorLabel.setEnabled(false);		

		redSlider=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
		add(redSlider);
		redSlider.setBounds(20,240,15,100);
		greenSlider=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
		add(greenSlider);
		greenSlider.setBounds(50,240,15,100);
		blueSlider=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
		add(blueSlider);
		blueSlider.setBounds(80,240,15,100);

		redSlider.addAdjustmentListener(new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent arg0) {
				bfColor();
			}
		});
		greenSlider.addAdjustmentListener(new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent arg0) {
				bfColor();
			}
		});
		blueSlider.addAdjustmentListener(new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent arg0) {
				bfColor();
			}
		});

	}

	private void bfColor(){
		setColor=new Color(redSlider.getValue(),greenSlider.getValue(),blueSlider.getValue());
		colorLabel.setBackground(setColor);			
	}

	void setLabelFonts(){
		int styl,size;
		styl=0;
		size=12;
		switch(styleList.getSelectedIndex()){
		case 0:styl=0;break;
		case 1:styl=1;break;
		case 2:styl=2;break;
		case 3:styl=3;break;
		}
		size=Integer.parseInt(sizeF.getText());
		fonts=new Font(fontList.getSelectedValue().toString(),styl,size);
		fontExample.setFont(fonts);
	}


	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource().equals(queding)){
			window.text.setFont(fonts);
			window.text.setBackground(fontExample.getBackground());
			window.text.setForeground(fontExample.getForeground());
			window.listFont.setSelectedItem(fontList.getSelectedValue());
			window.listFSize.setSelectedItem(Integer.parseInt(sizeList.getSelectedValue().toString()));
			this.setVisible(false);          
		}
		else if(arg0.getSource().equals(quxiao)){
			this.setVisible(false);
		}

	}
	public void valueChanged(ListSelectionEvent arg0) {

		if(arg0.getSource()==fontList){
			fontF.setText(fontList.getSelectedValue().toString());
			setLabelFonts();
			if((fontList.getSelectedValue().toString().charAt(0)>='A')&&(fontList.getSelectedValue().toString().charAt(0)<='Z')){
				fontExample.setText("MyText!");
			}
			else fontExample.setText("我的写字板");
		}
		else if(arg0.getSource()==styleList){
			sytleF.setText(styleList.getSelectedValue().toString());
			setLabelFonts();
		}
		else if(arg0.getSource()==sizeList){
			sizeF.setText(sizeList.getSelectedValue().toString());
			setLabelFonts();
		}

	}

}


⌨️ 快捷键说明

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