2021-06-10

day28

  1. 文本框
    2.密码框
    3.事件类型
    4.文本域
    5.鼠标事件
    6.键盘事件
    7.动作监听

package Demo01;

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

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

public class TestTextDemo01 extends JFrame {

public TestTextDemo01() {
	Container container = this.getContentPane();
	JTextField textfield=new JFormattedTextField("hello world");
	JTextField textfield2 =new JTextField("大数据五班");
	container.add(textfield,BorderLayout.NORTH);
	container.add(textfield2,BorderLayout.SOUTH);
	
	
	//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[]Frame) {
	new TestTextDemo01();
}

}

package Demo01;

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

import javax.swing.JFormattedTextField;
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[]Frame) {
	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(400,400);
frame.locate(400, 300);

	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(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.NORTH);
	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) {
	//
	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 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("");
}

}

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, 300, 300);
	this.setVisible(true);
	this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	
	this.addKeyListener(
			new KeyAdapter() {
		@SuppressWarnings("unused")
		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 TestMOuseListener {

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

}
class WindowFrame extends JFrame {
public WindowFrame() {

	this.setVisible(true);
	this.setBounds(100, 100, 600, 600);
	this.setBackground(Color.blue);
//	this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	//this.addWindowListener(new MyWindowListener());//
	
	this.addWindowListener(
			//匿名内部类
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
				
				super.windowClosing(e);
				
				setVisible(false);//设置不可见
				System.out.println("我要关闭");
				//System.exit(0);
			}
			public void WindowClosing(WindowEvent e) {
			//	super.windowActivated(e);
				
				System.out.println("windowActivated");
			}
			public void WindowClosed(WindowEvent e) {
				super.windowClosed(e);
				
				System.out.println("windowClosed");
			}
			public void WindowOpened(WindowEvent e) {
				//super.windowOpened(e);
				
				System.out.println("windowOpened");
			}


			
			}

);
}

}
/*
class MyWindowListener extends WindowAdapter{
//按住 alt + //
public void windowClosing(WindowEvent e) {
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 java.util.Iterator;

import javax.swing.JFrame;

public class TestWindow {

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

}
class MyFrame extends JFrame{
ArrayList points;
public MyFrame(String title) {
super(title);
this.setBounds(200, 200, 400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
points=new ArrayList<>();
this.addMouseListener(new MyMouseListener());

}

@Override
public void paint(Graphics g) {
	
	Iterator iterator =points.iterator();
	while(iterator.hasNext()) {
		Point point=(Point)iterator.next();
		g.setColor(Color.cyan);
		g.fillOval(point.x, point.y, 20, 20);
	}
}


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();
	}
	
}

}

在Java中,我们可以使用SimpleDateFormat类来将时间字符串转换为年月日格式。首先,你需要创建一个SimpleDateFormat的对象,并指定日期格式,例如"yyyy-MM-dd"表示年月日的格式。然后,使用该对象的parse()方法将时间字符串解析为一个Date对象。最后,再次使用SimpleDateFormat对象的format()方法将Date对象格式化为另一种日期格式,例如"yyyy年MM月dd日"表示年月日的格式。 以下是一个示例代码: ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateConversion { public static void main(String[] args) { String dateString = "2021-06-10"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = dateFormat.parse(dateString); SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy年MM月dd日"); String formattedDate = outputFormat.format(date); System.out.println(formattedDate); } catch (ParseException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先创建了一个SimpleDateFormat对象dateFormat,用于解析时间字符串"2021-06-10"。然后使用parse()方法将时间字符串解析为一个Date对象date。接下来,我们再次创建了一个SimpleDateFormat对象outputFormat,用于定义输出的日期格式为"yyyy年MM月dd日"。最后,使用format()方法将Date对象date格式化为所需的日期格式,并将结果打印出来。 运行以上代码,将输出"20210610日"。这样,我们就成功地将时间字符串转换为年月日格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值