1.简介
Gui的核心技术:Swing AWT
1. 界面不太美观
2. 需要jre环境
为什么学?
1. 可以写出自己想要的一些小工具
2. 工作需要swing界面维护,使用概率很小
3. 了解MVC架构,了解监听
2.AWT
### 2.1 Awt 介绍
-
包含了很多类和接口
-
元素:窗口、按钮、文本框
-
java.awt包
2.2组件和容器
#### 1. Frame
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("我的第一个Java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(Color.GREEN);
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);//默认true,可以改变
}
}
运行效果图
#### 上面的代码的封装:
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100,500,200,200,Color.blue);
MyFrame myFrame2 = new MyFrame(300,500,200,200,Color.yellow);
MyFrame myFrame3 = new MyFrame(100,700,200,200,Color.red);
MyFrame myFrame4 = new MyFrame(300,700,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);
setBounds(x,y,w,h);
setVisible(true);
}
}
#### 2.Panel(面板)
解决了关闭事件
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//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(40,161,35));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(193,15,60));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭时间,System.exit(0)
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
#### 3.布局管理器
-
流式布局
import java.awt.*; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //组建 按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置为流式布局 frame.setLayout(new FlowLayout(FlowLayout.LEFT));//靠左 //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//靠右 frame.setSize(200,200); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); } }
-
东西南北中
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.setSize(200,200);
frame.setVisible(true);
}
}
- 表格布局(Grid)
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();//Java函数 自动排列
frame.setVisible(true);
}
}
总结:
1. Frame是一个顶级窗口
2. Panel无法单独显示,必须添加到某个容器中
3. 布局管理器
1. 流式
2. 东西南北中
3. 表格
4. 大小,定位,背景颜色,监听
2.3. 事件监听**
事件监听:当某个事情发生的时候,干什么?
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();
//因为,addActionListener()需要ActionListener,所以我们需要构造一个ActionListener
MyActionLister myActionLister = new MyActionLister();
button.addActionListener(myActionLister);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);//关闭窗口
frame.setVisible(true);
}
//关闭窗体的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionLister implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaaaa");
}
}
文本框监听事件
当文本框输入内容后 按 Enter键后控制台输出文本框的内容
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 TestText01 {
public static void main(String[] args) {
//启动
MyFrame myFrame = new MyFrame();
windowClose(myFrame);
}
//关闭窗口的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyFrame extends Frame{
Frame frame = new Frame();
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListner myActionListner = new MyActionListner();
//按下Enter触发事件
textField.addActionListener(myActionListner);
pack();
setVisible(true);
}
}
class MyActionListner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();//获得一些资源 返回一个对象
System.out.println(field.getText());//获得文本框中的文本
}
}
2.4 画笔(paint)
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics 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);
//养成习惯,画笔用完,将他还原为最初的颜色
}
}
2.5 鼠标监听
目的:实现鼠标画画
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame1("drawing");
}
}
//自己的类
class MyFrame1 extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame1(String title){
super(title);
setBounds(200,200,400,300);
//存鼠标点击的点
points = new ArrayList<>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
public void paint(Graphics g){
//画画,监听鼠标的事件
Iterator Iterator = points.listIterator();
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{
//鼠标 按下,弹起,按住不放
public void mousePressed(MouseEvent e){
MyFrame1 myFrame1 = (MyFrame1) e.getSource();
//点击的时候,产生一个点
myFrame1.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都要重新画一遍
myFrame1.repaint();//刷新
}
}
}
2.6 窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBackground(Color.blue);
setBounds(100, 100, 200, 200);
setVisible(true);
//addWindowFocusListener(new MyWindowListenter());
this.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");
}
}
);
class MyWindowListenter extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,通过按钮隐藏窗口
System.exit(0);//正常退出
}
}
}
}
2.7 键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是那个键
int keyCode = e.getKeyCode();//获得当前键盘的码
if (keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
3. Swing
### 3.1 窗口,面板
#### JFrame
mport javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init() {
JFrame jframe = new JFrame("这是一个JFrame窗口");
jframe.setVisible(true);
jframe.setBounds(100, 100, 200, 200);
jframe.setBackground(Color.cyan);
//设置文字 JLable
JLabel label = new JLabel("你好你好你好你好");
jframe.add(label);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
#### ![image-20200801161621544](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20200801161621544.png)
#### 标签居中
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJfram2().init();
}
}
class MyJfram2 extends JFrame{
public void init(){
this.setBounds(10,10,200,300);
this.setVisible(true);
JLabel label = new JLabel("你好你好你好你好");
this.add(label);
//让文本标签居中,设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.YELLOW);
}
}
3.2 弹窗(Dialog)
用来被弹出,默认有关闭事件
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 container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击创建一个窗口");//创建
button.setBounds(30,30,200,500);
//点击这个按钮的时候,弹出一个窗口
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("你好你好你好"));
}
}
3.3 标签
Lable
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){//无参构造
}
public IconDemo(int width,int height){
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
3.4 面板
JPanel
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 1, 10, 10));//后面参数是间距
JPanel panel1 = new JPanel(new GridLayout(1, 3));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
container.add(panel1);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScorll(滚动条)
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("欢迎学习java.......");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane();
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100, 100, 300, 150);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5 按钮
- 普通按钮
import javax.swing.*;
import java.awt.*;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
JButton button = new JButton();
button.setToolTipText("按钮");
//add
container.add(button);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
- 单选按钮
import javax.swing.*;
import java.awt.*;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//单选框
JRadioButton jRadioBUtton1 = new JRadioButton("JRadioBUtton1");
JRadioButton jRadioBUtton2 = new JRadioButton("JRadioBUtton2");
JRadioButton jRadioBUtton3 = new JRadioButton("JRadioBUtton3");
//由于单选框只能选择一个,分组
//一个group中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(jRadioBUtton1);
group.add(jRadioBUtton2);
group.add(jRadioBUtton3);
container.add(jRadioBUtton1, BorderLayout.CENTER);
container.add(jRadioBUtton2, BorderLayout.NORTH);
container.add(jRadioBUtton3, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
- 复选框
import javax.swing.*;
import java.awt.*;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//多选框
JCheckBox checkBox01 = new JCheckBox("checkBox01");
JCheckBox checkBox02 = new JCheckBox("checkBox02");
container.add(checkBox01, BorderLayout.NORTH);
container.add(checkBox02, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
3.6 列表
- 下拉框
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
- 列表框
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = this.getContentPane();
//生成列表的内容
String[] contents = {"1", "2", "3"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setSize(500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
-
应用场景
1.选择地区,或者一些单个选项。
列表就是用来展示信息,一般是动态扩容的。
3.7 文本框
- 文本框
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container container = this.getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world",20);
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
- 密码框
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container container = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
- 文本域
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container container = this.getContentPane();
JTextArea textArea = new JTextArea(20,50);
textArea.setText("你好你好你好你好");
container.add(textArea);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}