AWT
一、AWT介绍
•
•
•使用AWT所涉及的类
•Container和component是AW T中的两个核心类
文本框的设置
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 TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container=this.getContentPane();
JTextField textfield=new JTextField("Hello");
JTextField textfield2=new JTextField("No");
container.add(textfield,BorderLayout.NORTH);
container.add(textfield,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
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('*');
JTextField textfield=new JTextField("Hello");
JTextField textfield2=new JTextField("No");
container.add(textfield,BorderLayout.NORTH);
container.add(textfield,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
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(400,300);
frame.locate(400, 400);
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("大数据5");
}
}
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,300);
frame.locate(400, 400);
Button button1=new Button(“start”);
Button button2=new Button(“stop”);
MyMoniter myMonitor=new MyMoniter();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class MyMoniter implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println(“大数据5”+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 TestKeyList {
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() {
public void keyPassed(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.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 TestMouseListener {
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());
}
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, 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();
}
}
}
窗体事件
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.red);
this.setBounds(100,100,200,200);
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");
}
});
}
**内部类**
/*内部类
class MyWindowListener extends WindowAdapter{
//按住alt+/
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
super.windowClosing(e);
setVisible(false);//设置不可见
System.out.println("我要关闭");
System.exit(0);
}
}*/
}