实验6_图形用户界面高级组件及线程的编写

大纲:

1. 使用线程实现字体不同颜色间的变换的小程序。

2. 简单的记事本小程序。

 

1. 使用线程实现字体不同颜色间的变换的小程序。

// Ex6_2_TextChangeJFrame.java

/**
 * 题目要求:
 *		编写一个Applet的小程序,利用线程绘制一串颜色闪烁、字体大小不断变化的字符串,
 * 源程序保存为Ex6_2.java。
 **/

/**
 * 题目分析:
 * 1. 首先设定好整体的窗口布局,将画板需要画的字符串使用一个继承Canvas类来实现。
 * 2. 线程的实现方法就直接用主类继承Runnable接口。
 * 3. 颜色大小的字体设定主要在继承Canvas类中实现,
 * 4. 线程负责重绘刚刚看设定的继承Canvas,然后变化的时间间隔就通过线程的休眠时间来设定。
 **/

import javax.swing.*;
import java.awt.*;
import java.awt.Color;

public class Ex6_2_TextChangeJFrame extends JFrame implements Runnable
{
	MyCanvasl c1;
	Thread firstThread;

	Ex6_2_TextChangeJFrame(){
		super("多线程实现字体不同颜色间变换");	// 继承父类构造方法
		setBounds(200, 200, 800, 300);			// 位置设定
		setVisible(true);						// 窗口设为可见
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	// 关闭窗口

		Container c = getContentPane(); 
		c1 = new MyCanvasl();			// 申请内嵌类的变量
		c1.setBackground(Color.white);
		c.add(c1, BorderLayout.CENTER);
		
		firstThread = new Thread(this);	// 创建线程
		firstThread.start();
	}

	public void run(){	// 实现接口
		Thread nowThread = Thread.currentThread();
		while(true){
			c1.repaint();				// 重绘,刷新画板
			try{
				firstThread.sleep(1000);
			}
			catch(InterruptedException e){
			}
		}
	}

	class MyCanvasl extends Canvas{
		int red, green, blue;
		int fontPoint;
		char c[] = {'超', '级', '无', '敌', '大', '小', '颜', '色', '变', '换', '字', '符', '串'};
		
		public void paint(Graphics g){
			fontPoint = (int) (Math.random() * 50);
			g.setFont(new Font("宋体", 0, fontPoint));	// 设定字体
			
			for(int i = 0; i < 13; ++i){
				red = (int) (Math.random() * 255);
				green = (int) (Math.random() * 255);
				blue  = (int) (Math.random() * 255);
				g.setColor(new Color(red, green, blue));	// 设定颜色
				g.drawChars(c, i, 1, 20+i*fontPoint+i*fontPoint, 80);	// 显示字体
			}
		}
	}


	public static void main(String [] args)
	{
		Ex6_2_TextChangeJFrame f = new Ex6_2_TextChangeJFrame();
	}

}


 

2. 简单的记事本小程序。

// Nodepad.java -- 简单记事本程序的实现

/**
 * 题目要求:
 * 实现“简单记事本”窗口,源程序保存为Nodepad.java。
 * 1)创建一个名为“简单记事本”窗口,在窗口中添加一个带有滚动条的文本区。
 * 2)在窗口中添加一个具有以下菜单的菜单栏:
 *? “文件”菜单,包含“打开”、“保存”、“退出”的菜单项,菜单间加分隔线,添加事件处理方法,使菜单具有打开、保存文件及退出的功能。
 *	 “编辑”菜单,包含“复制”、“剪切”、“粘贴”的菜单项,添加事件处理方法,使菜单具有“复制”、“剪切”、“粘贴”的功能。
 *	 “格式”菜单,包含“格式设置”的二级子菜单;而二级“格式设置”子菜单,包含 “自动换行”、“取消自动换行”、“断行不断字”、“取消断行不断字”的菜单项,添加事件处理方法,设置文本框自动换行和断行不断字的格式。
 * 3)在窗口中添加工具栏,包含“打开”、“保存”、“复制”、“剪切”、“粘贴”图片按钮的工具栏,添加事件处理方法,单击相应的按钮能实现相应的功能。(图片可到ftp中文件夹:实验六/img中下载)
 * 4)在窗口中添加弹出式菜单,包含“打开”、“保存”、“复制”、“剪切”、“粘贴”的菜单项,直接添加分隔线,添并加事件处理方法,选择相应的菜单项能实现相应的功能。
 **/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.*;

public class Nodepad extends JFrame implements ActionListener, MouseListener
{
	JMenuBar mb = new JMenuBar();						// 创建菜单栏

	JMenu m1 = new JMenu("文件");					// 创建菜单
	JMenuItem open = new JMenuItem("打开");	// 创建菜单项
	JMenuItem close = new JMenuItem("保存");
	JMenuItem exit = new JMenuItem("退出");

	JMenu m2 = new JMenu("编辑");
	JMenuItem copy = new JMenuItem("复制");
	JMenuItem cut = new JMenuItem("剪切");
	JMenuItem paste = new JMenuItem("粘贴");

	JMenu m3 = new JMenu("格式");
	JMenu m3_2 = new JMenu("格式设置");
	JMenuItem LineWrapTrue = new JMenuItem("自动换行");
	JMenuItem LineWrapFalse = new JMenuItem("取消自动换行");
	JMenuItem WrapStylewordTrue = new JMenuItem("断行不断字");
	JMenuItem WrapStylewordFalse = new JMenuItem("取消断行不断字");

	Container c;										// 记事本编辑面板设置
	JTextArea editor = new JTextArea();
	Font t = new Font("sanserif", Font.PLAIN, 12);

	JPopupMenu pm = new JPopupMenu();					// 创建弹出菜单对象
	JMenuItem item1 = new JMenuItem("复制");
	JMenuItem item2 = new JMenuItem("剪切");
	JMenuItem item3 = new JMenuItem("粘贴");
	JMenuItem item5 = new JMenuItem("打开");
	JMenuItem item6 = new JMenuItem("保存");

	JToolBar toolBar;								// 工具栏
	JButton bOpen = new JButton(new ImageIcon("open.png"));
	JButton bSave = new JButton(new ImageIcon("save.png"));
	JButton bCopy = new JButton(new ImageIcon("copy.png"));
	JButton bCut = new JButton(new ImageIcon("cut.png"));
	JButton bPaste = new JButton(new ImageIcon("paste.png"));

	Nodepad(){
		super("简单记事本");
		this.setSize(800, 600);
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e){
			System.err.println("不能设置外观的原因:" + e);
		}

		addFileMenu();
		addEditMenu();
		addFormatMenu();
		addJMenuBar();
		
		setJToolbar();			// 工具栏的设置
		setPopupMenu();			// 设置弹出菜单

		addEachActionListener();	// 注册监听事件

		setJMenuBar(mb);				// 显示菜单栏
		c = getContentPane();			// 为JFrame布局
		c.setLayout(new BorderLayout());
		c.add("Center", new JScrollPane(editor));
		c.add("North", toolBar);

		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private void addFileMenu(){	// 创建“文件”菜单的方法
		m1.add(open);
		m1.add(close);
		m1.addSeparator();		// 将分隔条加入到菜单中
		m1.add(exit);
		m1.setFont(t);			// 设置菜单字体
	}

	private void addEditMenu(){	// 创建“编辑”菜单的方法
		m2.add(copy);
		m2.add(cut);
		m2.addSeparator();
		m2.add(paste);
		m2.setFont(t);
	}

	private void addFormatMenu(){	// 创建“格式”菜单的方法
		m3.add(m3_2);
		m3_2.add(LineWrapTrue);
		m3_2.add(LineWrapFalse);
		m3_2.addSeparator();
		m3_2.add(WrapStylewordTrue);
		m3_2.add(WrapStylewordFalse);
		m3_2.setFont(t);
	}

	private void addJMenuBar(){	// 将菜单加入到菜单棒的方法
		mb.add(m1);
		mb.add(m2);
		mb.add(m3);
	}

	private void setJToolbar(){		// 工具栏的设置
		toolBar = new JToolBar();			// 工具栏
		toolBar.add(bOpen);
		toolBar.add(bSave);
		toolBar.addSeparator();
		toolBar.add(bCopy);
		toolBar.add(bCut);
		toolBar.add(bPaste);
	}


	private void setPopupMenu(){	// 初始化弹出菜单对象
		editor.add(pm);			// 将弹出菜单加入到文本区中

		pm.add(item5);
		pm.add(item6);
		pm.addSeparator();
		pm.add(item1);
		pm.add(item2);
		pm.add(item3);

	}
	
	private void addEachActionListener(){	// 注册菜单项的鼠标事件监听器
		open.addActionListener(this);		// 文件菜单
		close.addActionListener(this);
		exit.addActionListener(this);
		
		copy.addActionListener(this);		// 编辑菜单
		cut.addActionListener(this);
		paste.addActionListener(this);

		LineWrapTrue.addActionListener(this);		// 格式菜单
		LineWrapFalse.addActionListener(this);
		WrapStylewordTrue.addActionListener(this);
		WrapStylewordFalse.addActionListener(this);

		bOpen.addActionListener(this);			// 工具栏
		bSave.addActionListener(this);
		bCopy.addActionListener(this);
		bCut.addActionListener(this);
		bPaste.addActionListener(this);

		item1.addActionListener(this);		// 弹出式菜单		
		item2.addActionListener(this);
		item3.addActionListener(this);
		item5.addActionListener(this);
		item6.addActionListener(this);
		editor.addMouseListener(this);		// 注册文本区的鼠标事件监听器
	}

	private void loadFile(){			// 打开文件
		JFileChooser fc = new JFileChooser();
		int r = fc.showOpenDialog(this);
		if(r == JFileChooser.APPROVE_OPTION){
			File file = fc.getSelectedFile();
			try{
				editor.read(new FileReader(file), null);
			}
			catch(IOException e){
			}
		}
	}
	
	private void saveFile(){		// 保存文件
		JFileChooser fc = new JFileChooser();
		int r = fc.showSaveDialog(this);
		if(r == JFileChooser.APPROVE_OPTION){
			File file = fc.getSelectedFile();
			try{
				editor.write(new FileWriter(file));
			}
			catch(IOException e){
			}
		}
	}

	public void actionPerformed(ActionEvent e){
		if(e.getSource() == item5 || e.getSource() == open || e.getSource() == bOpen){
			loadFile();
		}
		else if(e.getSource() == item6 || e.getSource() == close || e.getSource() == bSave){
			saveFile();
		}
		else if(e.getSource() == exit){
			System.exit(0);
		}
		else if(e.getSource() == item1 || e.getSource() == copy || e.getSource() == bCopy){
			editor.copy();
		}
		else if(e.getSource() == item2 || e.getSource() == cut || e.getSource() == bCut){
			editor.cut();
		}
		else if(e.getSource() == item3 || e.getSource() == paste || e.getSource() == bPaste){
			editor.paste();
		}
		else if(e.getSource() == LineWrapTrue){
			editor.setLineWrap(true);
		}
		else if(e.getSource() == LineWrapFalse){
			editor.setLineWrap(false);
		}
		else if(e.getSource() == WrapStylewordTrue){
			editor.setWrapStyleWord(true);
		}
		else if(e.getSource() == WrapStylewordFalse){
			editor.setWrapStyleWord(false);
		}
	}

	public void mouseReleased(MouseEvent e){
		if (e.isPopupTrigger()){
			pm.show(editor, e.getX(), e.getY());	// 显示弹出式菜单
		}
	}
	
	public void mouseClicked(MouseEvent e){
	}
	
	public void mouseEntered(MouseEvent e){
	}

	public void mouseExited(MouseEvent e){
	}

	public void mousePressed(MouseEvent e){
	}

	public static void main(String args[]){
		Nodepad test = new Nodepad();
	}
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值