package com.Swing;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestMoreFrame {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 500, 500, Color.blue);
MyFrame myFrame2 = new MyFrame(600, 600, 500, 500, Color.YELLOW);
}
}
//继承Frame类 拥有全部属性
class MyFrame extends Frame{
static int id;
public MyFrame(int x,int y,int w,int h,Color color){
super("Frame"+(++id));
//设置可见性
setVisible(true);
//设置大小
setSize(w,h);
//设置位置
setLocation(x,y);
//setBounds(x,y,w,h); 一次性完成设置大小及位置
//设置背景颜色
setBackground(color);
//设置不可调整大小
setResizable(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
package com.Swing;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame("TestPanel");
frame.setVisible(true);
Panel panel = new Panel();
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(panel);
frame.setSize(400,400);
frame.setBackground(Color.black);
panel.setSize(500,500);
panel.setBackground(Color.cyan);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
package com.Swing;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//流式布局
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("FlowLayout");
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
frame.setSize(500,500);
//AWT 设置退出 (监听器)
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}
//东西南北中布局
class TestBorderLayout{
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout");
frame.setLayout(new BorderLayout());
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
frame.add(button1,BorderLayout.CENTER);
frame.add(button2,BorderLayout.EAST);
frame.add(button3,BorderLayout.WEST);
frame.add(button4,BorderLayout.SOUTH);
frame.add(button5,BorderLayout.NORTH);
frame.setVisible(true);
frame.setSize(500,500);
//AWT 设置退出 (监听器)
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}
//表格布局
class TestGridLayout{
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout");
frame.setLayout(new GridLayout(3,2));//行 列
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.setVisible(true);
frame.setSize(500,500);
//frame.pack();
//AWT 设置退出 (监听器)
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
}
package com.Swing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCalculate {
public static void main(String[] args) {
new MyFrame2().loading();
}
}
class MyFrame2 extends Frame{
TextField num1,num2,res;
public void loading(){
setVisible(true);
setBounds(300,300,400,500);
//pack();
num1 = new TextField(10);
num2 = new TextField(10);
res = new TextField(10);
Label label = new Label("+");
Button button = new Button("=");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(res);
button.addActionListener(new Calculate(this));
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public MyFrame2(){
}
}
class Calculate implements ActionListener {
MyFrame2 myFrame2;
public Calculate(MyFrame2 myFrame2){
this.myFrame2 = myFrame2;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(myFrame2.num1.getText());
int n2 = Integer.parseInt(myFrame2.num2.getText());
myFrame2.res.setText(""+(n1+n2));
myFrame2.num1.setText("");
myFrame2.num2.setText("");
}
}
package com.Swing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestTextField {
public static void main(String[] args) {
new MyFrame1();
}
}
class MyFrame1 extends Frame{
public MyFrame1(){
setVisible(true);
setBounds(400,400,500,500);
//文本框TextField 文本域TextArea
TextField textField = new TextField();
add(textField,BorderLayout.CENTER);
//设置替换编码为*
textField.setEchoChar('*');
ActionListener actionListener;
actionListener = ((ActionEvent e) -> {
e.getActionCommand();
});
//创建监听事件
TextListener textListener = new TextListener();
textField.addActionListener(textListener);
//关闭
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class TextListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获取监听信息强转为文本框
TextField field =(TextField) e.getSource();
//通过获取文本框内文本来获取输入
System.out.println(field.getText());
//设置文本框内容为“” (清空)
field.setText("");
}
}
package com.Swing;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
new Paint().loading();
}
}
class Paint extends Frame{
public void loading(){
setVisible(true);
setBounds(300,300,500,500);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
g.setColor(Color.cyan);
g.drawOval(100,100,100,100);
}
}
package com.Swing;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouthPaint {
public static void main(String[] args) {
new MouthPaint("画画");
}
}
class MouthPaint extends Frame{
ArrayList points;
public MouthPaint(String name){
super(name);
setVisible(true);
setBounds(300,300,500,500);
//存放鼠标的点
points = new ArrayList();
//鼠标监听器 针对窗口 在窗口点
addMouseListener(new MyMouseListener());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
//画画 监听鼠标事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point next =(Point) iterator.next();
g.setColor(Color.cyan);
g.fillOval(next.x,next.y,20,20);
}
}
public void addPoint(Point point){
points.add(point);
}
class MyMouseListener extends MouseAdapter{
//鼠标点击
@Override
public void mousePressed(MouseEvent e) {
MouthPaint frame = (MouthPaint) e.getSource();
//点击的时候产生一个点
frame.addPoint(new Point(e.getX(),e.getY()));
//每次点击鼠标 重画一遍所有的
frame.repaint();
}
}
}
见上画画
关闭时输出
package com.Swing;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowsListener {
public static void main(String[] args) {
new Listener().loading();
}
}
class Listener extends Frame{
public void loading(){
setVisible(true);
setBounds(100,100,500,500);
addWindowListener(new WindowAdapter() {
//窗口正在关闭时
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//窗口被激活时
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
setTitle("我又出现了");
}
});
}
}
package com.Swing;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestKeyListener {
public static void main(String[] args) {
new Key().loading();
}
}
class Key extends Frame{
public void loading(){
setVisible(true);
setBounds(100,100,300,300);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获取当前键盘下是哪个键 KeyCode值
int keyCode = e.getKeyCode();
System.out.println(keyCode);
//判断如果是 上键 输出
if(keyCode == KeyEvent.VK_UP){
System.out.println("上键");
}
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}