2021-06-08

Java学习第二十八天
1.TestText文本框编辑

package demo01;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class TestText extends JFrame{
public TestText() {
	Container container=this.getContentPane();
	JTextField textfield=new JTextField("Hello  world");
	JTextField textfield2=new JTextField("大数据五班");
	container.add(textfield,BorderLayout.NORTH);
	container.add(textfield2,BorderLayout.SOUTH);
	
	this.setVisible(true);
	this.setSize(500,350);
	this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
	new TestText();
}
}

2.TestText1密码框

package demo01;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class TestText1 extends JFrame{
public TestText1() {
	Container container=this.getContentPane();
	JPasswordField passwordfield=new JPasswordField();
	passwordfield.setEchoChar('^');
	container.add(passwordfield);
	
	
	this.setVisible(true);
	this.setSize(500,350);
	this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
	new TestText1();
}
}


3.TextActionEvent事件
当用户在文本框中单击按钮(按钮可以触发ActionEvent事件,当按钮获得监听器之后,
如果激活按钮,比如用鼠标单击按钮,就可以触发ActionEvent事件,
监听器将字符串显示在一个文本区

package demo02;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class TextActionEvent {
	public static void main(String[] args) {
		JFrame frame =new JFrame();
		frame.setSize(400,400);
		frame.locate(400, 300);
		 MyActionListener myActionListener=new  MyActionListener();
		Button button =new Button();
		button.addActionListener(myActionListener);
		frame.add(button);
		frame.add(button,BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
	}

}
class MyActionListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
	
		System.out.println("2005");
	}
	
}

4.TextAtionTwo事件
当用户在文本框中单击按钮(按钮可以触发ActionEvent事件,当按钮获得监听器之后,
如果激活按钮,比如用鼠标单击按钮,就可以触发ActionEvent事件,
监听器将字符串显示在一个文本区

package demo02;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class TextAtionTwo {
	public static void main(String[] args) {
		JFrame frame =new JFrame("开始--停止");
		frame.setSize(400,400);
		frame.locate(400, 300);
		Button button1 =new Button("start");
		Button button2 =new Button("stop");
		 
		MyMonitor myMonitor= new MyMonitor();
		button1.addActionListener(myMonitor);
		button2.addActionListener(myMonitor);
	
		frame.add(button1,BorderLayout.WEST);
		frame.add(button2,BorderLayout.SOUTH);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
	}

}
class MyMonitor implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		//System.out.println(e.getActionCommand("大数据2005"+e.getActionCommand()));
		if(e.getActionCommand().equals("stop")) {
			System.out.println("我要学习了");
		}else {
			System.out.println("我要休息了");
		}
	}
	
}

5.TextText01
当用户在文本框中输入密码(字符串)并显示*号点击enter(enter可以触发事件,当按钮获得监听器之后,
如果激活按钮,比如用鼠标单击按钮,就可以触发ActionEvent事件,
监听器将字符串显示在一个文本区

package demo02;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class TextText01 {
	public static void main(String[] args) {
		new Myframe();
	}

}
class Myframe extends JFrame{
	public Myframe() {
		TextField textField =new TextField();
		this.add(textField);
		MyActionListener2 myActionListener2 =new MyActionListener2();
		textField.addActionListener(myActionListener2);
		textField.setEchoChar('*');
		this.setVisible(true);
		this.pack();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}
class MyActionListener2 implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		TextField field=(TextField)e.getSource();
		System.out.println(field.getText());
		field.setText("");
	}
	
}

6.TestKeyListener键盘事件
监听键盘是否输入W键或上键
如输入W键或上键则显示"你按下了上箭头"“你按下了W”

package demo03;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class TestKeyListener {
	public static void main(String[] args) {
		new KeyFrame();
	}

}
class KeyFrame extends JFrame{
	public KeyFrame() {
		this.setBounds(10,10,330,410);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.addKeyListener(
				new KeyAdapter() {
					@Override
					public void keyPressed(KeyEvent e) {
						// TODO 自动生成的方法存根
						int keycode=e.getKeyCode();
					//	System.out.println(keycode);
						if(keycode==KeyEvent.VK_UP) {
						System.out.println("你按下了上箭头");
						}
						if(keycode==KeyEvent.VK_W) {
							System.out.println("你按下了W");
						}
						
					}
		
			
			
		});
	}
}

7.TestWindow监听鼠标事件
当用户点击弹窗关闭按钮(按钮可以触发ActionEvent事件,当按钮获得监听器之后,
如果激活按钮,比如用鼠标单击按钮,就可以触发鼠标监听事件,
监听器将字符串显示在文本区显示"我要关闭"

package demo03;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;



public class TestWindow {
	public static void main(String[] args) {
		new WindowFrame();
	}

}
class WindowFrame extends JFrame{
	public WindowFrame() {
		this.setBackground(Color.orange);
		this.setBounds(120,120,500,500);
		this.setVisible(true);
		//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		//this.addWindowListener(new WindowListener());
		this.addWindowListener(
				//匿名内部类
				new WindowAdapter() {
					@Override
					public void windowClosing(WindowEvent e) {
						// TODO 自动生成的方法存根
						super.windowClosing(e);
						setVisible(false);
						
						System.out.println("我要关闭");
						
					}
					@Override
					public void windowActivated(WindowEvent e) {
						// TODO 自动生成的方法存根
						super.windowActivated(e);
						System.out.println("windowActivated");
					}
					@Override
					public void windowClosed(WindowEvent e) {
						// TODO 自动生成的方法存根
						super.windowClosed(e);
						System.out.println("windowClosed");
					}
					@Override
					public void windowOpened(WindowEvent e) {
						// TODO 自动生成的方法存根
						super.windowOpened(e);
						System.out.println("windowOpened");
					}
				}
				);
	}
//	class MyWinowListener extends WindowAdapter{
	//@Override
	//public void windowActivated(WindowEvent e) {
		// TODO 自动生成的方法存根
		//super.windowActivated(e);
		//setVisble(false);
		//System.out.println("关闭");
		//System.exit(0);
	//}
	
	
}
	
//}

8.TextMouseListener 鼠标监听事件
当用户点击弹窗内并出现颜色(就可以可以触发鼠标监听事件,当按钮获得监听器之后,
如果激活按钮,比如用鼠标单击按钮,就可以触发鼠标监听事件,
监听器将显示在文本区显示当前点击位置坐标

package demo03;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFrame;

public class TextMouseListener {
	public static void main(String[] args) {
	new	MyFrame("我的画图");
	}

}
class MyFrame extends JFrame{
	ArrayList points;
	public MyFrame(String title) {
		super(title);
		this.setBounds(200,200,410,410);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		points=new ArrayList<>();
		this.addMouseListener(new MyMouseListener());
	}
	
public void paint(Graphics g) {
	Iterator iterator=points.iterator();
	while(iterator.hasNext()) {
		Point point=(Point)iterator.next();
		g.setColor(Color.red);
		g.fillOval(point.x, point.y,10,10);
		
	}
	
}
	
	public void addPaint(Point point) {
		points.add(point);
	}
	private class MyMouseListener extends MouseAdapter{
		
		@Override
		public void mousePressed(MouseEvent e) {
			MyFrame myFrame=(MyFrame) e.getSource();
			System.out.println("x坐标: "+e.getX()+"y坐标: "+e.getY());
			myFrame.addPaint(new Point(e.getX(),e.getY()));
			myFrame.repaint();
		}
		
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值