Java-Gui
1、AWT
1.1、组件和容器
1、Frame
package com.wanan.lesson1;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("我的第一个Java图行窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置窗口颜色 Color
frame.setBackground(Color.black);
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题: 发现窗口无法关闭只能停止程序运行
尝试回顾封装
package com.wanan.lesson1;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame MyFrame1 = new MyFrame(100,100,200,200,Color.black);
MyFrame MyFrame2 = new MyFrame(300,100,200,200,Color.yellow);
MyFrame MyFrame3 = new MyFrame(100,300,200,200,Color.red);
MyFrame MyFrame4 = new MyFrame(300,300,200,200,Color.MAGENTA);
}
}
class MyFrame extends Frame{
static int id = 0;//可能存在多个窗口,创建计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("Myframe" + (++id));
setBackground(color);
setVisible(true);
setBounds(x,y,w,h);
}
}
2、面板Panel
解决关闭问题
package com.wanan.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//Panel , 可以看成是一个空间,但是不能单独存在
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(35, 210, 35));
//panel设置坐标,想对于panel
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(17, 238, 216,50));
//frame.add(panel);
frame.add(panel);
frame.setVisible(true);
frame.setResizable(false);
//监听事件,监听窗口关闭事件 Syetem.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
3、布局管理
流式布局 FlowLayout
package com.wanan.lesson1; 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("组件小程序"); //组件-按钮 Button baton1 = new Button("baton1"); Button baton2 = new Button("baton2"); Button baton3 = new Button("baton3"); //设置为流式布局 //frame.setLayout(new FlowLayout()); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setVisible(true); frame.setSize(200,200); //把按钮添加上去 frame.add(baton1); frame.add(baton2); frame.add(baton3); } }
东南西北中 Border
package com.wanan.lesson1; import com.sun.org.apache.bcel.internal.generic.NEW; import java.awt.*; public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); Button east = new Button("East"); Button west = new Button("West"); Button south = new Button("South"); Button north = new Button("North"); Button center = new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setVisible(true); frame.setSize(200,200); } }
表格布局 Grid
package com.wanan.lesson1; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); Button btn1 = new Button("btn1"); Button btn2 = new Button("btn2"); Button btn3 = new Button("btn3"); Button btn4 = new Button("btn4"); Button btn5 = new Button("btn5"); Button btn6 = new Button("btn6"); frame.setLayout(new GridLayout(3,2)); frame.add(btn1); frame.add(btn2); frame.add(btn3); frame.add(btn4); frame.add(btn5); frame.add(btn6); frame.pack(); frame.setVisible(true); } }
课程DEMO
package com.wanan.lesson1;
import java.awt.*;
public class Demo {
public static void main(String[] args) {
Frame frame = new Frame("Demo");
frame.setVisible(true);
frame.setSize(300,400);
frame.setLocation(300,400);
frame.setBackground(Color.yellow);
frame.setLayout(new GridLayout(2,1));
//4个面板
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2,2));
panel1.add(new Button("East-1"),BorderLayout.EAST);
panel1.add(new Button("West-1"),BorderLayout.WEST);
panel2.add(new Button("p2-btn-1"));
panel2.add(new Button("p2-btn-2"));
panel1.add(panel2,BorderLayout.CENTER);
panel3.add(new Button("East-2"),BorderLayout.WEST);
panel3.add(new Button("West-2"),BorderLayout.EAST);
for (int i = 0; i < 4; i++) {
panel4.add(new Button("p4-btn-"+i));
}
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
}
}
总结
1,Frame是一个顶级的窗口
2,Panel无法单独显示,必须添加到某个容器中。
3,布局管理器:
- 流式
- 东西南北中
- 表格
4,大小,定位,背景颜色,可见性,监听!
4、事件监听
事件监听:当某某个事情发生的时候,干什么?
按钮监听事件DEMO
package com.wanan.lisson2;
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 TestActionEvent {
public static void main(String[] args) {
//按下按钮触发一些事件
Frame frame = new Frame();
Button button = new Button("输出aaa");
//因为addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener MyActionListener = new MyActionListener();
button.addActionListener(MyActionListener);
frame.add(button,BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
windowClose(frame);
}
//关闭窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//按钮监听事件
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮,共享一个事件
package com.wanan.lisson2;
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 TestActionEvent02 {
public static void main(String[] args) {
//两个按钮实现同一个监听
//开始 停止
Frame frame = new Frame("开始-停止-DEMO");
Button start = new Button("开始");
Button stop = new Button("停止");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!
//可以多个按钮只写一个监听类
start.setActionCommand("START");
stop.setActionCommand("STOP");
MyMonitor myMonitor = new MyMonitor();
start.addActionListener(myMonitor);
stop.addActionListener(myMonitor);
frame.add(start,BorderLayout.NORTH);
frame.add(stop,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
//关闭窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//按钮监听事件
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand(); 获取按钮的信息
System.out.println("按钮被点击了:msg=> "+e.getActionCommand());
if (e.getActionCommand().equals("START")){
System.out.println("命令Start开启成功");
}
}
}
5、输入框TextField 监听
package com.wanan.lisson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
//启动!
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
// 按下回车就会触发输入框事件
textField.addActionListener(myActionListener2);
// 设置编码
textField.setEchoChar('*');
setVisible(true);
setSize(200,200);
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField fieid = (TextField) e.getSource();//获得一些资源,返回了一个对象
System.out.println(fieid.getText());//获得输入框中的文本
fieid.setText("");//null ""
}
}
6、简易计算器、组合+内部类回顾学习
op原则:组合>继承
class A extend B{
}
class A {
public B b;
}
代码Demo
package com.wanan.lisson2;
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 TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
// 三个文本框,一个按钮,一个标签
TextField number1 = new TextField(10);//字符数
TextField number2 = new TextField(10);//字符数
TextField number3 = new TextField(20);//字符数\
Button btn = new Button("=");
btn.addActionListener(new MyCalculatorListener(number1,number2,number3));
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(number1);
add(label);
add(number2);
add(btn);
add(number3);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监控类
class MyCalculatorListener implements ActionListener{
//获取三个变量
private TextField number1,number2,number3;
public MyCalculatorListener(TextField number1,TextField number2,TextField number3) {
this.number1 = number1;
this.number2 = number2;
this.number3 = number3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1,获得加数和被加数
int n1 = Integer.parseInt(number1.getText());
int n2 = Integer.parseInt(number2.getText());
//2,将这个值加法运算后放到第三个框
number3.setText(""+(n1+n2));
//3,清楚前两个框
number1.setText("");
number2.setText("");
}
}
完全改造为面向对象写法,并且优化输入非数字报错!
package com.wanan.lisson2;
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 TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
// 三个文本框,一个按钮,一个标签
num1 = new TextField(10);//字符数
num2 = new TextField(10);
num3 = new TextField(20);//字符数
Button btn = new Button("=");
Label label = new Label("+");
btn.addActionListener(new MyCalculatorListener(this));
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(btn);
add(num3);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监控类
class MyCalculatorListener implements ActionListener{
//获取计算器这个对象,在一个类里面组合另外一个类;
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed (ActionEvent e) {
//1,获得加数和被加数
try {
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2,将这个值加法运算后放到第三个框
calculator.num3.setText("" + (n1 + n2));
//3,清楚前两个框
calculator.num1.setText("");
calculator.num2.setText("");
}catch (NumberFormatException e1){
calculator.num3.setText("请输入整形数字!");
calculator.num1.setText("");
calculator.num2.setText("");
}
}
}
内部类:
更好的包装
package com.wanan.lisson2; 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 TestCalc { public static void main(String[] args) { new Calculator().loadFrame(); } } //计算器类 class Calculator extends Frame { //属性 TextField num1, num2, num3; //方法 public void loadFrame() { // 三个文本框,一个按钮,一个标签 num1 = new TextField(10);//字符数 num2 = new TextField(10); num3 = new TextField(20);//字符数 Button btn = new Button("="); Label label = new Label("+"); btn.addActionListener(new MyCalculatorListener()); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(btn); add(num3); setVisible(true); pack(); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } //监控类 private class MyCalculatorListener implements ActionListener { //获取计算器这个对象,在一个类里面组合另外一个类; /* Calculator calculator = null; public MyCalculatorListener() { this.calculator = calculator; }*/ @Override public void actionPerformed(ActionEvent e) { //1,获得加数和被加数 try { int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); //2,将这个值加法运算后放到第三个框 num3.setText("" + (n1 + n2)); //3,清楚前两个框 num1.setText(""); num2.setText(""); } catch (NumberFormatException e1) { num3.setText("请输入整形数字!"); num1.setText(""); num2.setText(""); } } } }
7、画笔
package com.wanan.lesson3;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().load();
}
}
class MyPaint extends Frame{
public void load(){
setVisible(true);
setBounds(200,200,600,500);
}
//画笔
@Override
public void paint(Graphics g) {
//super.paint(g);
// 画笔,需要有颜色,画笔可以画画
// g.setColor(Color.red);
// g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100);//实心的园
//g.setColor(Color.GREEN);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完,将它还原到最初的颜色
}
}
8、鼠标监听
目的:想要实现鼠标画画!
package com.wanan.lesson3;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.peer.MouseInfoPeer;
import java.util.ArrayList;
import java.util.Iterator;
//测试鼠标监听
public class TestHouseListener {
public static void main(String[] args) {
new Myframe("画图");
}
}
class Myframe extends Frame{
//画画需要画笔,需要监听鼠标当前位置,需要集合来存储这个点
ArrayList points;
public Myframe(String title){
super(title);
setBounds(200,200,400,300);
//存鼠标点击的点
points = new ArrayList<>();
setVisible(true);
//鼠标监听器
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.BLUE);
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 frame = (Myframe) e.getSource();
//这个我们点击的时候就会在页面产生一个点!
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重画一遍
frame.repaint();//刷新
}
}
}
9、窗口监听
package com.wanan.lesson3;
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 TestWindow {
public static void main(String[] args) {
new WindownFrame("窗口监听");
}
}
class WindownFrame extends Frame{
TextField text1;
public WindownFrame(String Frame){
super(Frame);
setVisible(true);
setBackground(Color.BLUE);
setBounds(100,100,200,200);
text1 = new TextField();
add(text1);
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindownFrame source = (WindownFrame) e.getSource();
source.setTitle("傻子刚刚在看啥去了?");
text1.setText("傻子刚刚在看啥去了?");
}
});
}
/* class MywindownListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,通过按钮隐藏窗口
System.exit(0);//正常退出
}
}*/
}
10、键盘监听
package com.wanan.lesson3;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
//键盘
public class TestKeyListener {
public static void main(String[] args) {
new KeyFram();
}
}
class KeyFram extends Frame{
public KeyFram(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
int KeyCode = e.getKeyCode();
System.out.println(KeyCode);//不需要去记录这个值,直接使用这个静态属性 VK_XXX
//获得键盘按下的是哪个
if (KeyCode == KeyEvent.VK_UP) {
System.out.println("按下了上键");
}
}
});
}
}
2、Swing
2.1、窗口 面板
package com.wanan.lesson4;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo2 {
public static void main(String[] args) {
new Myjframe().init();
}
}
class Myjframe extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(10,10,200,200);
//设置文字
JLabel jLabel = new JLabel("这是一个test");
this.add(jLabel);
//让我们的文本居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.BLUE);
}
}
标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
2.2、弹窗 JDialog
JDialog,用来弹出一个窗口,默认携带窗口关闭事件!
package com.wanan.lesson4;
import javax.accessibility.AccessibleContext;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame
Container contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
//按钮
JButton jbutton = new JButton("点击弹出一个对话框");
jbutton.setBounds(30,30,200,50);
jbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
//点击这个按钮弹窗
contentPane.add(jbutton);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,500,500);
Container contentPane = this.getContentPane();
//contentPane.setLayout(null);
JLabel jLabel = new JLabel("这是一个弹窗");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
this.add(jLabel);
}
}