Lesson_for_java_day25--java中GUI实例(键盘鼠标事件、菜单栏、自定义弹窗、调用弹窗)

一、键盘鼠标事件:

package gui;

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

public class MouseAndKeyEvent {
	private Frame frame;
	private Button button;
	private TextField textField;
	public MouseAndKeyEvent() {
		init();
	}
	
	public void init(){//初始化界面
		frame = new Frame("my Frame");
		frame.setBounds(300,100,600,500);
		frame.setLayout(new FlowLayout());
		button = new Button("my Button");
		textField = new TextField(20);
		frame.add(textField);
		frame.add(button);
		myEvent();
		frame.setVisible(true);	
	}
	
	public void myEvent(){//监听事件
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		
		button.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e){
				System.out.println("鼠标进入该组件");
			}
			
			public void mouseClicked(MouseEvent e){
				if(e.getClickCount() == 2)
					System.out.println("鼠标双击");
			}
		});
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("按钮action");
			}
		});
		
		button.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
					System.exit(0);
				System.out.println(e.getKeyChar() + "-------" + e.getKeyCode());
				if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER)
					System.out.println("ctrl + enter is run");
			}
		});
		
		textField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){
				int code = e.getKeyCode();
				if(!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)){
					System.out.println(code + "------是非法的");
					e.consume();//如果条件不符合,事件不会按照默认方法处理(取消事件)
				}
			}
		});	
	}
	
	public static void main(String[] args) {
		new MouseAndKeyEvent();
	}
}

二、菜单栏:

package exercise;

import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MyMenuDemo {
	private Frame frame;
	private MenuBar mBar;
	private Menu fileMenu,submMenu;
	private MenuItem closeItem,subItem,openItem,saveItem;
	private FileDialog openDialog,saveDialog;
	private TextArea textArea;
	private File file;
	
	public MyMenuDemo() {
		init();
	}
	
	public void init() {
		frame = new Frame("my window");
		frame.setBounds(300,100,650,500);
		//frame.setLayout(new FlowLayout());
		mBar = new MenuBar();
		textArea = new TextArea();
		
		fileMenu = new Menu("文件");
		openItem = new MenuItem("打开");
		saveItem = new MenuItem("保存");
		
		submMenu = new Menu("子菜单");//Menu有子条目
		subItem = new MenuItem("子条目");//MenuItem没有子条目
		closeItem = new MenuItem("退出");
		
		submMenu.add(subItem);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(submMenu);
		fileMenu.add(closeItem);
		mBar.add(fileMenu);
		
		frame.setMenuBar(mBar);
		
		openDialog = new FileDialog(frame,"我要打开",FileDialog.LOAD);
		saveDialog = new FileDialog(frame,"我要保存",FileDialog.SAVE);
		
		frame.add(textArea);
		myEvent();
		frame.setVisible(true);
		
	}
	
	private void myEvent() {
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		
		closeItem.addActionListener(new ActionListener() {	
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		openItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openDialog.setVisible(true);
				String dirPath = openDialog.getDirectory();
				String fileName = openDialog.getFile();
				if(dirPath == null || fileName == null)
					return;
				textArea.setText("");
				File file = new File(dirPath,fileName);
				BufferedReader buf = null;
				try {
					buf = new BufferedReader(new FileReader(file));
					String line = null;
					while ((line = buf.readLine()) != null) {
						textArea.append(line + "\r\n");
						
					}
				} catch (IOException e2) {
					throw new RuntimeException("读取失败");
				}
				finally{
					try {
						buf.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		
		saveItem.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				if(file == null){
					saveDialog.setVisible(true);
					String dirPath = saveDialog.getDirectory();
					String fileName = saveDialog.getFile();
					if(dirPath == null || fileName == null)
						return;
					file = new File(dirPath,fileName);
				}
				BufferedWriter bufw = null;
				try {
					bufw = new BufferedWriter(new FileWriter(file));
					String text = textArea.getText();
					bufw.write(text);
					bufw.flush();
				
				} catch (Exception e2) {
					throw new RuntimeException();
				}finally{
					try {
						bufw.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}	
			}
		});
	}
	
	public static void main(String[] args) {
		new MyMenuDemo();
	}	
}

三、自定义弹窗

package exercise;

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

public class MyWindowDemo {
	private Frame frame;
	private TextField tField;
	private Button button;
	private TextArea tArea;
	private Button b;
	private Dialog d;
	public MyWindowDemo() {
		inti();
	}
	
	public void inti(){
		frame = new Frame("my window");
		frame.setBounds(300,100,600,500);
		frame.setLayout(new FlowLayout());
		tField = new TextField(60);
		button = new Button("转到");
		b = new Button("确定");
		tArea = new TextArea(25,70);
		
		frame.add(tField);
		frame.add(button);
		frame.add(tArea);
		myEvent();
		frame.setVisible(true);
	}
	
	private void myEvent(){
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		
//		d.addWindowListener(new WindowAdapter() {
//			public void windowClosing(WindowEvent e){
//				d.setVisible(false);
//			}
//		});
		
		button.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				showdir();			
			}	
		});
		
		tField.addKeyListener(new KeyAdapter() {//回车出发事件
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					showdir();
				}
			}	
		});
		
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				d.setVisible(false);
			}
		});
	}
	
	private void showdir(){
		String dirPath = tField.getText();//获取文本信息
		File dir = new File(dirPath);
		if(dir.exists() && dir.isDirectory()){
			String[] names = dir.list();
			for(String name : names){
				tArea.append(name + "\r\n");
			}
			tField.setText(null);
		}
		else {
			d = new Dialog(frame, "提示信息--self",true);//自定义弹窗信息
			d.setBounds(400,200,240,100);
			d.setLayout(new FlowLayout());
			Label label = new Label();
			
			d.add(label);
			d.add(b);
			label.setText("你输入的信息:"+ dirPath +"有误,请重新输入");
			d.setVisible(true);
		}
	}
	
	public static void main(String[] args) {
		new MyWindowDemo();
	}
}

四、模拟登入界面,触发弹窗信息

package sonyi.exercise;

import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

public class LoginExercise {
	public static void main(String[] args) {
		new FrameDemo();
	}
}

class FrameDemo {
	JFrame frame;
	JButton jButton1,jButton2;
	public FrameDemo() {
		frame = new JFrame("登入界面");
		frame.setLayout(new FlowLayout());
		frame.setBounds(300, 200, 250, 300);
		
		JPanel jP1 = new JPanel();
		Label label1 = new Label("用户名:");
		TextField tField1 = new TextField(20);
		jP1.add(label1);
		jP1.add(tField1);
		
		JPanel jP2 = new JPanel();
		Label label2 = new Label("密   码:");
		JPasswordField jPasswordField = new JPasswordField(14);
		jP2.add(label2);
		jP2.add(jPasswordField);
		
		JPanel jP3 = new JPanel();
		JLabel label3 = new JLabel("备   注:");
		label3.setVerticalAlignment(SwingConstants.TOP);
		
		TextArea tArea = new TextArea(4,20);
		jP3.add(label3);
		jP3.add(tArea);
		
		JPanel jP4 = new JPanel();
		Checkbox checkbox = new Checkbox("已经阅读条款,并同意条款内容");
		jP4.add(checkbox);
		
		JPanel jP5 = new JPanel();
		jButton1 = new JButton("确定");
		jButton2 = new JButton("取消");
		jP5.add(jButton1);
		jP5.add(jButton2);
		
		frame.add(jP1);
		frame.add(jP2);
		frame.add(jP3);
		frame.add(jP4);
		frame.add(jP5);
		myEvent();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
	}
	
	public void myEvent() {
		jButton1.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(frame, "测试弹窗,没有链接信息");//弹窗信息
			}
		});
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值