组件和事件
密码栏
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 TesTextDemo01 extends JFrame{
public TesTextDemo01() {
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 TesTextDemo01();
}
}
文本框
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 TesTextDemo2 extends JFrame{
public TesTextDemo2() {
Container container =this.getContentPane();
JPasswordField passwordField = new JPasswordField();
JTextField textfield =new JTextField("hello woeld");
JTextField textfield1 =new JTextField("大数据五班");
container.add(textfield,BorderLayout.NORTH);
container.add(textfield1,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TesTextDemo2();
}
}
自定义添加的动作
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");
MyMointor mymointor =new MyMointor();
button1.addActionListener(mymointor);
button2.addActionListener(mymointor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class MyMointor 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,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_0) {
System.out.println("你按到零 0");
}
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 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.blue);
this.setBounds(100,100,100,100);
this.setVisible(true);
this.addWindowListener(
new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println(" windowOpened");
}
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);
System.out.println("我要关闭");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified");
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}
);
}
}
鼠标
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,400,400);
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.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();
myFrame.addPaint(new Point(e.getX(),e.getY()));
myFrame.repaint();
}
}
}
20200800605013