Java学习笔记——图形化界面GUI

GUI

  • 框架图
  • 监听器与事件
    • 鼠标事件
    • 键盘事件
  • 布局
  • Frame
  • Button
    • 规范写法
  • Dialog
  • MenuBar

代码:

框架图

Java学习笔记——图形化界面GUI_第1张图片
具体方法参见:Jdk13_API

监听器与事件

Java学习笔记——图形化界面GUI_第2张图片
在代码中表现为:addXxxListener()方法、XxxEvent类。

鼠标事件

package test;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MouseDemo {
     
	private Frame f;
	private Button b;
	MouseDemo(){
     
		init();
	}
	//初始化函数
	public void init() {
     
		f = new Frame("My Window");
		b = new Button("click");
		f.setBounds(550, 250, 500, 300);
		f.setLayout(new FlowLayout());
		f.add(b);
		myEvents();
		f.setVisible(true);
	}
	private void myEvents() {
     
		f.addWindowListener(new WindowAdapter() {
     
			@Override
			public void windowClosing(WindowEvent e) {
     
				System.exit(0);
				}
		});
		//添件鼠标事件
		f.addMouseListener(new MouseAdapter() {
     
			@Override
			public void mouseClicked(MouseEvent e) {
     
//				if(e.getClickCount()==2 && e.getButton()==MouseEvent.BUTTON1) {
     
//					System.out.println("鼠标左键双击动作");
//				}else if(e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON1) {
     
//					System.out.println("鼠标左键单击动作");
//				}else if(e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON3) {
     
//					System.out.println("鼠标右键单击动作");
//				}
				int num = e.getClickCount();
				int button = e.getButton();
				switch(num) {
     
				case 1:
					if(button==MouseEvent.BUTTON1) {
     
						System.out.println("鼠标左键单击动作");
					};
					if(button==MouseEvent.BUTTON3) {
     
						System.out.println("鼠标右键单击动作");
					};
					break;
				case 2:
					if(button==MouseEvent.BUTTON1) {
     
						System.out.println("鼠标左键双击动作");
					};
					break;
				}
			}
		});
		b.addActionListener(new ActionListener() {
     
			@Override
			public void actionPerformed(ActionEvent e) {
     
				System.out.println("You have clicked the button!");
			}
		});
	}
	public static void main(String[] args) {
     
		new MouseDemo();
	}
}

键盘事件

package test;

import java.awt.*;
import java.awt.event.*;

public class KeyDemo {
     
	//演示键盘事件, 加入文本框
	private Frame f;
	private TextField tf;
//	private Button b;
	KeyDemo(){
     
		init();
	}
	//初始化函数
	public void init() {
     
		//设置Frame
		f = new Frame("My Window");
		f.setBounds(550, 250, 500, 300);
		f.setLayout(new FlowLayout());
		//设置TestField
		tf = new TextField();
//		tf = new TextField(10);
		tf.setColumns(10);
		//放置组件
		f.add(tf);
//		b = new Button("click");
//		f.add(b);
		myEvents();
		//显示窗口
		f.setVisible(true);
	}
	private void myEvents() {
     
		//设置关闭窗口
		f.addWindowListener(new WindowAdapter() {
     
			@Override
			public void windowClosing(WindowEvent e) {
     
				System.exit(0);
			}
		});
		//输入状态下按下Ctrl+ESC关闭窗口
		tf.addKeyListener(new KeyAdapter() {
     
			@Override
			public void keyPressed(KeyEvent e) {
     
				if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER) {
     
					System.exit(0);
				}
				//文本框内只允许输入数字
				int num = e.getKeyCode();
				if(num<KeyEvent.VK_0 || num>KeyEvent.VK_9) {
     
					//不在数字范围内时, 禁止输入到文本框中
					e.consume();
				}
			}
		});
		
	}
	public static void main(String[] args) {
     
		new KeyDemo();
	}
}

布局

Java学习笔记——图形化界面GUI_第3张图片

Frame

package test;

import java.awt.*;
import java.awt.event.*;

public class WindowDemo {
     
	//演示建立基本窗口
	public static void myWindow() {
     
		//窗口对象
		Frame f = new Frame("My Window");
		//设置窗口大小
		f.setSize(500, 300);
		//设置位置
		f.setLocation(550, 250);
		//设置布局
		f.setLayout(new FlowLayout());
		//添加监视器并设置关闭动作
		f.addWindowListener(new WindowAdapter() {
     
			//实现关闭操作
			@Override
			public void windowClosing(WindowEvent e) {
     
				System.exit(0);
			}
		});
		//设置窗口可见
		f.setVisible(true);
	}
	public static void main(String[] args) {
     
		myWindow();
	}
}

Button

package test;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ButtonDemo {
     
	//窗口上添加button并指定功能
	public static void myButton() {
     
		Frame f = new Frame("My Window");
		//设置窗口位置以及大小
		f.setBounds(550, 250, 500, 300);
		//设置布局
		f.setLayout(new FlowLayout());
		//添加监视器并设置关闭动作
		f.addWindowListener(new WindowAdapter() {
     
			//实现关闭操作
			@Override
			public void windowClosing(WindowEvent e) {
     
				System.exit(0);
				}
		});
		//button
		Button b = new Button("click");
		//设置button监视器
		b.addActionListener(new ActionListener() {
     
			@Override
			public void actionPerformed(ActionEvent e) {
     
				System.out.println("You have clicked the button!");
			}
		});
		//添加button
		f.add(b);
		//设置窗口可见
		f.setVisible(true);
	}
	public static void main(String[] args) {
     
		myButton();
	}
}

规范写法

package test;

import java.awt.*;
import java.awt.event.*;

public class NormalDemo {
     
	private Frame f;
	private Button b;
	NormalDemo(){
     
		init();
	}
	//初始化函数
	public void init() {
     
		f = new Frame("My Window");
		b = new Button("click");
		f.setBounds(550, 250, 500, 300);
		f.setLayout(new FlowLayout());
		f.add(b);
		myEvents();
		f.setVisible(true);
	}
	private void myEvents() {
     
		f.addWindowListener(new WindowAdapter() {
     
			@Override
			public void windowClosing(WindowEvent e) {
     
				System.exit(0);
				}
		});
		b.addActionListener(new ActionListener() {
     
			@Override
			public void actionPerformed(ActionEvent e) {
     
				System.out.println("You have clicked the button!");
			}
		});
	}
	public static void main(String[] args) {
     
		new NormalDemo();
	}
}

Dialog

package test;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
/**
 * 将指定目录下的所有文件和文件夹输出在文本区域中
 * 并添加提示对话框
 * @author 14251
 *
 */
public class MyWindowDemo {
     
	private Frame f;
	private Button b;
	private TextField tf;
	private TextArea ta;
	private File file;
	public MyWindowDemo() {
     
		init();
	}
	public void init() {
     
		//窗口信息
		f = new Frame("My Window");
		f.setLayout(new FlowLayout());
		f.setBounds(450, 150, 500, 500);
		//按钮信息
		b = new Button("查询");
		//文本框信息
		tf = new TextField();
		tf.setColumns(33);
		//文本区域信息
		ta = new TextArea(25, 40);
		//添加组件
		f.add(tf);
		f.add(b);
		f.add(ta);
		f.setVisible(true);
		//导入事件
		myEvents();
	}
	private void myEvents() {
     
		f.addWindowListener(new WindowAdapter() {
     
			@Override
			public void windowClosing(WindowEvent e) {
     
				System.exit(0);
			}
		});
		b.addActionListener(new ActionListener() {
     
			@Override
			public void actionPerformed(ActionEvent e) {
     
				showInfo();
			}
		});
		//敲回车也能获取文件目录
		tf.addKeyListener(new KeyAdapter() {
     
			@Override
			public void keyPressed(KeyEvent e) {
     
				if(KeyEvent.VK_ENTER==e.getKeyCode()) {
     
					showInfo();
				}
			}
		});
	}
	
	private void showInfo() {
     
		String dirPath = tf.getText();
		file = new File(dirPath);
		if(file.exists() && file.isDirectory()) {
     
			ta.setText("");
			String[] names = file.list();
			for(String name : names) {
     
				ta.append(name+"\r\n");
			}
		}else {
     
			pathNotExistDialog();
		}
	}
	
	//路径不存在的对话框
	private void pathNotExistDialog() {
     
		//对话框
		Dialog d = new Dialog(f, "注意", false);
		d.setBounds(520, 275, 400, 150);
		d.setLayout(new FlowLayout());
		//标签
		Label lab = new Label(file.getAbsolutePath()+"不存在, 请重新输入!");
//		lab.setText(file.getAbsolutePath()+"不存在, 请重新输入!");
		//按钮
		Button okB = new Button("确定");
		//添加组件
		d.add(lab);
		d.add(okB);
		d.setVisible(true);
		d.addWindowListener(new WindowAdapter() {
     
			@Override
			public void windowClosing(WindowEvent e) {
     
				d.setVisible(false);
			}
		});
		okB.addActionListener(new ActionListener() {
     
			@Override
			public void actionPerformed(ActionEvent e) {
     
				d.setVisible(false);
			}
		});
	}

	public static void main(String[] args) {
     
		new MyWindowDemo();
	}
}

MenuBar

package test;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**
 * 演示MenuBar, Menu, MenuItem
 * MenuBar-菜单栏, Menu-菜单, MenuItem-菜单项
 * MenuBar可以放置Menu, Menu可以放置Menu和MenuItem
 * Frame通过setMenuBar()设置MenuBar, Menu通过add()添加Menu或MenuItem
 * @author 14251
 *
 */
public class MenuDemo {
     
	private Frame frame;
	private TextArea textArea;
	//用来记录当前对象是否有文件对象,
	//若有, 则不弹出Dialog窗口
	private File f;
	//用于打开或保存文件
	private FileDialog file_load, file_save;
	private MenuBar menuBar;
	private Menu file, create;
	private MenuItem fileItem, dirItem, open, save, quit;
	public MenuDemo(){
     
		init();
	}
	public void init() {
     
		//初始化
		frame = new Frame();
		textArea = new TextArea();
		file_load = new FileDialog(frame, "打开文件", FileDialog.LOAD);
		file_save = new FileDialog(frame, "保存文件", FileDialog.SAVE);
		menuBar = new MenuBar();
		file = new Menu("文件");
		create = new Menu("新建");
		fileItem = new MenuItem("文件");
		dirItem = new MenuItem("文件夹");
		open = new MenuItem("打开");
		save = new MenuItem("保存");
		quit = new MenuItem("退出");
		
		//Frame上添加MenuBar以及文本区域
		frame.setMenuBar(menuBar);
		frame.add(textArea);
		//MenuBar上添加file
		menuBar.add(file);
		
		//file上添加create, open, quit
		file.add(create);
		file.add(open);
		file.add(save);
		file.add(quit);
		
		//create上添加子功能
		create.add(fileItem);
		create.add(dirItem);
		
		//设置窗口信息
		frame.setBounds(450, 150, 500, 500);
		frame.setVisible(true);
		
		//导入事件
		myMenuEvents();
		
	}	
		/*
		 * 设置功能
		*/
		public void myMenuEvents() {
     
			//Frame
			frame.addWindowListener(new WindowAdapter() {
     
				@Override
				public void windowClosing(WindowEvent e) {
     
					System.exit(0);
				}
			});
			//quit-退出功能
			quit.addActionListener(new ActionListener() {
     
				@Override
				public void actionPerformed(ActionEvent e) {
     
					System.exit(0);
				}
			});
			//open-打开文件
			open.addActionListener(new ActionListener() {
     
				@Override
				public void actionPerformed(ActionEvent e) {
     
					file_load.setVisible(true);
					String path = file_load.getDirectory();
					String name = file_load.getFile();
					if(path==null || name==null) {
     
						return;
					}
					textArea.setText("");
					f = new File(path, name);
					BufferedReader br = null;
					try {
     
						br = new BufferedReader(new FileReader(f));
						String line = null;
						while((line=br.readLine())!=null) {
     
							textArea.append(line+"\r\n");
						}
					} catch(IOException e1) {
     
						e1.printStackTrace();
					} finally {
     
						if(br!=null) {
     
							try {
     
								br.close();
							}catch(IOException e2) {
     
								e2.printStackTrace();
							}
						}
					}	
				}
			});
			//save-保存文件
			save.addActionListener(new ActionListener() {
     
				@Override
				public void actionPerformed(ActionEvent e) {
     
					BufferedWriter bw = null;
					if(f==null) {
     
						//文件不存在时, 显示保存对话框, 并新建文件
						file_save.setVisible(true);
						String path = file_save.getDirectory();
						String name = file_save.getFile();
						f = new File(path, name);
					}
					try {
     
						bw = new BufferedWriter(new FileWriter(f));
						String content = textArea.getText();
						bw.write(content);
						bw.flush();
					} catch (IOException e1) {
     
						e1.printStackTrace();
					} finally {
     
						if(bw!=null) {
     
							try {
     
								bw.close();
							}catch(IOException e2) {
     
								e2.printStackTrace();
							}
						}
					}
				}
			});
		}
	
	//主函数
	public static void main(String[] args) {
     
		new MenuDemo();
	}
}

你可能感兴趣的:(笔记,java,gui)