day28

文本框

文本框(JTextField)是界面中用于输入和输出一行文本的框。JTextField类用来建立文本框。与文本框相关的接口是ActionListener。

文本框处理程序的基本内容有以下几个方面:

①声明一个文本框名。

②建立一个文本框对象。

③将文本框对象加入到某个容器。

④对需要控制的文本框对象注册监视器,监听文本框的输入结束(即输入回车键)事件。

⑤一个处理文本框事件的方法,完成对截获事件进行判断和处理。

JTextField类的主要构造方法

JTextField();//文本框的字符长度为1

JTextField(int columns);//初始值为空字符串,文本框的字符长度设为columns

JTextField(String text);//文本框初始值为text的字符串

JTextField(String text,int columns);//文本框初始值为text,文本框的字符长度为columns

JTextField类的其他方法:

setFont(Font f);//设置字体

setText(String text);//在文本框中设置文本

getText();//获取文本框中的文本

setEditable(boolean);//指定文本框的可编辑性,默认为true

addActionListener(ActionListener);//为文本框设置动作监视器,指定ActionListener对象接受该文本框上发生的输入结束动作事件

界面布局

界面布局指的是容器内组件的排列方式。
容器对象通过setLayout方法设置其内部组件的布局。

Swing常用的布局如下:

BorderLayout :将容器分为东西南北中5个区域FlowLayout :组件从左到右,从上到下的顺序排列
GridLayout :容器分为单元格大小相同的网格
GridBagLayout :单元格大小不同,组件可以占据多个单元格
BoxLayout :在容器中按照水平或垂直方向排列
SpringLayout :组件根据相对位置排列
CardLayout :组件按照层叠的方式排列

事件处理

事件处理指的是交互式组件接受用户或系统行为, 并进行处理。

Swing的事件处理将组件、行为和处理进行分离,其中:

进行交互的组件称为事件源
在组件上发生的行为称为事件
负责对事件进行处理的对象称为监听者

事件处理的流程是:

1 )建立指定事件的监听者对象
2)为事件源加入(注册)监听者
3 )当指定事件发生时,监听者的方法处理

不同类事件的监听者在Java中以不同的接口表示,具体事件由接口中的.指定方法表示。

ActionListener 组件单击、回车等确认动作
ItemL istener 某选择项被选择/取消选择
TextListener 文本内容改变
MouseListener 鼠标事件
KeyListener 键盘事件
FocusListener 焦点事件
CompomentListener 容器中的组件发生的事件
WindowListener 窗口事件
ContainerListener 容器中添加/删除组件事件

建立监听者对象即是建立对应接口的实现类,并实现相应的方法处理指
定的事件。
例如,鼠标点击、确认等行为的监听者接口为ActionListener ;键盘输
入行为的监听者接口为KeyListener等。
为了方便访问界面组件,监听者的实现类一般是包含组件的类。

代码及运行结果

package Demo01;

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

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

public class TestTextDemo01 extends JFrame {
	public TestTextDemo01() {
		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 TestTextDemo01();
		
	}

}

在这里插入图片描述

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 TestTextDemo02 extends JFrame {
	public TestTextDemo02() {
		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 TestTextDemo02();
		
	}

}

在这里插入图片描述

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(500, 400);
		frame.locate(400, 200);
		
		MyActionListener MyActionListener=new MyActionListener();
		Button button=new Button();
		
		button.addActionListener(MyActionListener);
		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("大数据五班");
		
	}
	
}

在这里插入图片描述

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 TextActionTwo {
	public static void main(String[] args) {
		JFrame frame =new JFrame("开始--停止");
		frame.setSize(500, 400);
		frame.locate(400, 200);
		Button button1=new Button("start");
		Button button2=new Button("stop");
		
		MyMonit0r myMonit0r =new MyMonit0r();
		
		button1.addActionListener(myMonit0r);
		button2.addActionListener(myMonit0r);
		frame.add(button1,BorderLayout.CENTER);
		frame.add(button2,BorderLayout.SOUTH);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
	}

}
class MyMonit0r implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		//System.out.println("大数据五班->"+e.getActionCommand());
		if(e.getActionCommand().equals("stop")) {
			System.out.println("我要下班了");
		}else {
			System.out.println("我要开始工作了");
			
		}
	}
	
}

在这里插入图片描述

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 textFileld=new TextField();
		this.add(textFileld);
		MyActionListener2 myActionListener2 =new MyActionListener2();
		textFileld.addActionListener(myActionListener2);
		textFileld.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("");
	}
	
}

在这里插入图片描述

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, 20, 300, 400);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.addKeyListener(
				new KeyAdapter() {
					@Override
					public void keyPressed(KeyEvent e) {
						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上键");
						}
					}
			
		});
	}
}

在这里插入图片描述

package Demo03;

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

import javax.swing.JFrame;

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

}
class WindowFrame extends JFrame{
	public WindowFrame() {
		this.setBackground(Color.cyan);
		this.setBounds(166, 200, 300, 400);
		this.setVisible(true);
		//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		//this.addWindowListener(new MyWindowListener());//匿名类
		
		this.addWindowListener(
				new WindowAdapter() {
					//匿名内部类
					@Override
					public void windowClosing(WindowEvent e) {
						// TODO 自动生成的方法存根
						super.windowClosing(e);
						setVisible(false);//设置不可见
						System.out.println("我要关闭");
						//System.exit(0);
					}
					@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 MyWindowListener extends WindowAdapter{
		//  按住alt+/
		@Override
		public void windowClosing(WindowEvent e) {
			// TODO 自动生成的方法存根
			super.windowClosing(e);
			setVisible(false);//设置不可见
			System.out.println("我要关闭");
			System.exit(0);
}
	}*/

在这里插入图片描述

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 javax.swing.JFrame;
import javax.swing.text.html.HTMLDocument.Iterator;

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(400, 400, 400, 400);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		points=new ArrayList<>();
		this.addMouseListener(new MyMouseListener());
	}
	
	@Override
	public void paint(Graphics g) {
		java.util.Iterator iterator=points.iterator();
		while(iterator.hasNext()) {
			Point point=(Point)iterator.next();
			g.setColor(Color.cyan);
			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();
		}
		
	}
}

在这里插入图片描述

大数据2005 周敏 2020080605048

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值