GUI:核心技术Swing、AWT,1.因为界面不美观
2.需要JRE环境,因为一个游戏本身的容量很小,但是它的运行依赖于JRE环境
为什么我们需要学习?
1.可以写出一些小工具
2.工作的时候,也有可能需要维护到swing界面,,但是概率很小
3.了解MVC架构,监听
1.包含了很多的类和接口,GUI(图形用户界面)
2.窗口、按钮、文本框
1.frame,第一步个窗口程序的运行代码如下:
package lesson01;
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(new Color(253, 189, 189));
//弹出的初始位置
frame.setLocation(400,400);
//设置大小固定
frame.setResizable(false);//设置大小不能重新设置
}
}
多个窗口
package lesson01;
import java.awt.*;
public class Testframe2 {
public static void main(String[] args) {
//展示多个窗口
Myframe myframe1 = new Myframe(100,100,200,200,Color.blue);
Myframe myframe2 = new Myframe(300,100,200,200,Color.yellow);
Myframe myframe3 = new Myframe(100,300,200,200,Color.white);
Myframe myframe4 = new Myframe(300,300,200,200,Color.black);
}
}
class Myframe extends frame{
static int id=0;/
//for循环直接生成4个按钮
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}
2.4事件监听
事件监听:当发生某个操作的事情会有哪些相应的操作相应
package lesson02;
import java.awt.*;
import java.awt.event.*;
public class Ceshi {
public static void main(String[] args) {
frame frame = new frame();
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(300,400);
frame.setBackground(new Color(14, 124, 227));
frame.pack();
frame.setLayout(new GridLayout(1,3));
Button button = new Button("bt1");
Button button2 = new Button("bt2");
Button button3 = new Button("bt3");
Button button4 = new Button("bt4");
//BorderLayout东西南北布局,GridLayout列表布局(流式)
Panel panel = new Panel(new BorderLayout());
Panel pane2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout(1,2));
panel.add(button);
pane2.add(button2);
pane2.add(button3);
panel3.add(button4);
frame.add(panel);
frame.add(pane2);
frame.add(panel3);
//在窗口上添加新的监听事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
//在按钮上添加新的监听事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("sabi");
}
});
}
}
2.4.2多个按钮同时监听一个事件(获取指令、设置指令)
package lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestAction3 {
public static void main(String[] args) {
//1.设置窗口、按钮等等忽略不写
frame frame = new frame();
frame.setVisible(true);
Button bt1 = new Button("bt1");
Button bt2 = new Button("bt1");
frame.add(bt1,BorderLayout.NORTH);
frame.add(bt2,BorderLayout.SOUTH);
bt1.setActionCommand("diyige");
bt2.setActionCommand("dierge");
//3.按钮下设置获取指令的方法即可
Monitor monitor = new Monitor();
bt1.addActionListener(monitor);
bt2.addActionListener(monitor);
}
static class Monitor implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("输出的顺序为"+e.getActionCommand());
}
}
}
2.5输入框TestField监听输入的文本(含)
package lesson02;
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) {
frame frame = new frame();
frame.setVisible(true);
frame.pack();
TextField textField = new TextField();
frame.add(textField);//已经继承,只需添加
MyActionListener myActionListener = new MyActionListener();
textField.addActionListener(myActionListener);
}
static class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//因为getSource是Object类型,所以需要强制转型,向下转型
TextField textField = (TextField)e.getSource();//获得资源;
textField.setEchoChar('*');
System.out.println(textField.getText());//获得文本框中输入的文本
textField.setText("");//记住先后顺序一定要理清楚,当按下回车键的时候窗口会有哪些监听时间
}
}
}
2.6简易计算器+内部类回顾复习
1.简易计算器实现代码
package lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc2 {
public static void main(String[] args) {
new Calculator();
}
//================================================================
static class Calculator extends frame{
public Calculator(){
//三个文本框
TextField num1 = new TextField();
TextField num2 = new TextField();
TextField num3= new TextField();
//一个标签
Label label = new Label("+");
//一个按钮
Button button = new Button("=");
button.addActionListener(new Action1(num1,num2,num3));//放入三个框框设定的名字
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
}
//=======================================================================
class Action1 implements ActionListener{
//获取三个变量
private TextField num1,num2,num3;
public Action1(TextField num1,TextField num2,TextField num3) {
//设置三个数abc两个是拿来监听下获取数字的,还有一个是得出数值的
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
//以下是计算的方法的代码
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
//算完的结果放在第三个框框中
num3.setText(""+(n1+n2));
//算完以后清楚前面两个框
num1.setText("");
num2.setText("");
}
}
2.7画笔
实现步骤:
1.设置一个我的画笔的方法,继承frame,
2.在我的画笔方法里面布局出一个框框,空白的
3.在我的画笔里面重写frame的画笔方法,设置画笔的颜色、设置的图形等等
package lesson03;
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) {
//super.paint(g);
//画笔需要有颜色、画笔可以画画
g.fillRect(100,100,100,100);
//repaint监听器使得画笔一直在动,就会有动画的效果
}
}
2.8模拟画图工具
第一版:
package lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new Myframe("画图");
}
}
//2 自己画板(需要有监听还有画板的布局)
class Myframe extends frame{
//画画需要画笔,需要监听当前鼠标的位置,需要集合来存储这个点
ArrayList points;
//2.1
public Myframe(String title) {
super(title);
setBounds(200,200,400,400);//在子类里面设定画板的大小
//存鼠标点击的点
points=new ArrayList<>();
setVisible(true);
//鼠标监听事件,正对这个窗口
this.addMouseListener(new MyMouseListener());
}
//2.2
//执行鼠标反应相对应的操作
@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 myframe =(Myframe)e.getSource();
//这个我们点击的时候,就会在界面上产生一个个的点
//这个点就是鼠标的点
myframe.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
myframe.repaint();//刷新
}
}
}
第二版
2.9鼠标监听(单纯回顾)
2.10键盘监听
package lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TestKeyListener {
public static void main(String[] args) {
new Keyframe();
}
static class Keyframe extends frame{
public Keyframe() {
setBounds(1,2,300,400);
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个,当前的码
int keyCode=e.getKeyCode();
System.out.println(keyCode);//输出键盘的内容keyCode
if (keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
}
3.Swing
3.1Jframe窗体
package lesson04;
import com.sun.jdi.PathSearchingVirtualMachine;
import javax.swing.*;
import java.awt.*;
public class JframeDemo02 {
public static void main(String[] args) {
new myframe().init();
}
}
class myframe extends Jframe {
public void init(){
Container container = this.getContentPane();
container.setBounds(100,100,100,100);
container.setBackground(Color.YELLOW);
setVisible(true);
setLocation(400,400);
JLabel label = new JLabel("现在我知道了");//JLabel和label显示出来的东西不一样
this.add(label);
//让标签文本居中
label.setHorizontalAlignment(0);
}
}
3.2弹窗
package lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends Jframe {
public DialogDemo() {
setVisible(true);
setSize(100,100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Jframe放东西、容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton jButton = new JButton("点击弹出一个对话框");
jButton.setBounds(30,30,200,50);
//点击这个按钮的时候弹出一个弹窗
container.add(jButton);
//按钮监听
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
MyDialog myDialog = new MyDialog();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog() {
setVisible(true);
setBounds(100,100,100,100);
//setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container =getContentPane();
container.setLayout(null);
container.add(new Label("这真的只是一个弹窗窗口"));
}
}
3.3创建带图标标签
3.3.1.这个是抄过来的
package lesson04;
import javax.swing.*;
import java.awt.*;
public class IconDemo extends Jframe implements Icon {
private int width;
private int height;
//写有参构造函数就必须写一个无参构造函数
public IconDemo() throws HeadlessException {
} //无参构造
public IconDemo(int height,int width) throws HeadlessException {
this.height=height;
this.width=width;
} //有参构造
public void init(){
IconDemo iconDemo = new IconDemo(15,15);//创建图标
iconDemo.getContentPane();//创建图标第二步
JLabel label = new JLabel("这是一个按钮",iconDemo,SwingConstants.CENTER);//创建标签,文本、图标、摆放位置
Container container = getContentPane();//获取一个容器
container.add(label);//在容器中添加标签
this.setVisible(true);//注意这里必须要this否则就会看不见
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口,注意这里必须要this否则就会看不见
}
@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;
}
public static void main(String[] args) {
new IconDemo().init();
}//主方法
}
3.3.2.这个是我自己写的
是一样的,但是需要自己锻炼总结:可以一开始把那些乱七八糟的名字换成自己想要的名字来写
package ceshi;
//这个是做的图像标签的测试
import lesson04.IconDemo;
import javax.swing.*;
import java.awt.*;
public class Demo01 extends Jframe implements Icon{ //调用接口就必须重写方法!!!!
private int width;
private int height;
public Demo01()throws HeadlessException{}
public Demo01(int height,int width)throws HeadlessException{
this.height=height;
this.width=width;
}
public void init(){
Demo01 demo01 = new Demo01(15,15); //图标
demo01.getContentPane();//Icon必须做的两个操作!!!!!
JLabel label = new JLabel("这只是一个按钮",demo01,SwingConstants.CENTER);
Container container = getContentPane();//获取一个容器!!!!!!!!很重要
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,height,width);
}
@Override
public int getIconWidth() {
return this.width; //
}
@Override
public int getIconHeight() {
return this.height;
}
public static void main(String[] args) {
new Demo01().init();
}
}
3.3.3带图片的标签
package ceshi;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Demo02 extends Jframe {
public Demo02() {//throws HeadlessException
URL url = Demo02.class.getResource("tx.jpg");//放入图片名,创建图片地址方便实用
ImageIcon imageIcon = new ImageIcon(url);//创建一个图片图片图标,放入地址
JLabel label = new JLabel("这是一个标签");
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
pack();
setBounds(50,50,50,50);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo02();
}
}
3.4面板
3.4.1普通的面板排版
package lesoon05;
import javax.swing.*;
import java.awt.*;
//创建新容器(获取容器)、设置面板、在面板中添加新的按钮、在容器中添加面板、主函数中调用
public class Demo01 extends Jframe {
public Demo01() throws HeadlessException {
Container contentPane = getContentPane();//获得容器,实例化容器
contentPane.setLayout(new GridLayout(2,1,20,20));//后面两个表示间距
JPanel jPanel = new JPanel(new GridLayout(1, 3));//面板也可以设置布局排列
JPanel jPanel2 = new JPanel(new GridLayout(1, 3));//面板也可以设置布局排列
JPanel jPanel3 = new JPanel(new GridLayout(1, 3));//面板也可以设置布局排列
JPanel jPanel4 = new JPanel(new GridLayout(1, 3));//面板也可以设置布局排列
jPanel.add(new Button("1"));
jPanel.add(new Button("2"));
jPanel2.add(new Button("3"));
jPanel2.add(new Button("4"));
jPanel2.add(new Button("5"));
jPanel3.add(new Button("6"));
jPanel3.add(new Button("7"));
jPanel3.add(new Button("8"));
jPanel3.add(new Button("9"));
jPanel3.add(new Button("10"));
contentPane.add(jPanel);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo01();
}
}
3.4.2滚动面板
package lesoon05;
import javax.swing.*;
import java.awt.*;
public class Demo02 extends Jframe {
public Demo02() {
Container contentPane = getContentPane();
//文本域
textarea textarea = new textarea(20, 10);//每行20个字,行高10
textarea.setText("这个是一个文本域");
//contentPane.add(textarea);
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textarea);//把文本放进滚动面板里面
contentPane.add(jScrollPane);//容器里面放进滚动面板
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo02();
}
}
3.5按钮(单选、复选)
3.5.1带图片的按钮
package lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//创建一个图片的获取地址,吧图片地址放到图片图标上、吧这个图标放在按钮上、获取一个容器、把这个按钮再放在容器里
public class Demo03 extends Jframe {
public Demo03() {
//将一个图片变为图标
URL resource = Demo03.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
//把这个图标放在按钮上
JButton jButton = new JButton();//
//jButton.add(imageIcon);这里为什么不是这样子设置的
//jButton.setIcon(imageIcon);
jButton.setToolTipText("图片");//类似于window界面的图片程序图片按钮
//add
Container contentPane = this.getContentPane();
contentPane.add(jButton);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo03();
}
}
3.5.2单选框
package lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//创建一个图片的获取地址,吧图片地址放到图片图标上、吧这个图标放在按钮上、获取一个容器、把这个按钮再放在容器里
public class Demo04 extends Jframe {
public Demo04() {
Container contentPane = getContentPane();
//将一个图片变为图标
URL resource = Demo03.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
//单选框
JRadioButton a = new JRadioButton("单选框");
JRadioButton b = new JRadioButton("单选框2");
JRadioButton c= new JRadioButton("单选框3");
//由于单选框只能选择一个,所以需要分组,一个组只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(a);
buttonGroup.add(b);
buttonGroup.add(c);
contentPane.add(a,BorderLayout.NORTH);
contentPane.add(b,BorderLayout.SOUTH);
contentPane.add(c,BorderLayout.CENTER);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo04();
}
}
3.5.3多选框
package lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//创建一个图片的获取地址,吧图片地址放到图片图标上、吧这个图标放在按钮上、获取一个容器、把这个按钮再放在容器里
public class Demo05 extends Jframe {
public Demo05() {
Container contentPane = getContentPane();
//多选框
Checkbox checkbox = new Checkbox("num1");
Checkbox checkbox2 = new Checkbox("num2");
contentPane.add(checkbox,BorderLayout.SOUTH);
contentPane.add(checkbox2,BorderLayout.CENTER);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo05();
}
}
3.6列表
3.6.1下拉框
package lesson06;
import javax.swing.*;
import java.awt.*;
public class Demo01 extends Jframe {
public Demo01() throws HeadlessException {
Container contentPane = getContentPane();
//设置下拉框
JComboBox a= new JComboBox();
a.addItem(null);
a.addItem("设计上映");
a.addItem("决定上映");
a.addItem("准备上映");
contentPane.add(a);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo01();
}
}
3.6.2列表框
package lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Demo02 extends Jframe {
public Demo02() {
Container contentPane = getContentPane();
//生成一个内容,在内容里面添加各个数据然后再把容器放入列表当中
Vector
3.7文本框、密码框、文本域
3.7.1文本框
package lesson06;
import javax.swing.*;
import java.awt.*;
public class Demo03 extends Jframe {
public Demo03() {
Container contentPane = getContentPane();
JTextField jTextField = new JTextField("hello");
JTextField jTextField2 = new JTextField("world",10);
contentPane.add(jTextField,BorderLayout.NORTH);
contentPane.add(jTextField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo03();
}
}
3.7.2密码框
package lesson06;
import javax.swing.*;
import java.awt.*;
public class Demo04 extends Jframe {
public Demo04() {
Container contentPane = getContentPane();
JPasswordField jPasswordField = new JPasswordField("密码框");
jPasswordField.setEchoChar('*');
contentPane.add(jPasswordField);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo04();
}
}
3.7.3文本域
package lesson05;
import javax.swing.*;
import java.awt.*;
public class Demo02 extends Jframe {
public Demo02() {
Container contentPane = getContentPane();
//文本域
textarea textarea = new textarea(20, 10);//每行20个字,行高10
textarea.setText("这个是一个文本域");
//contentPane.add(textarea);
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textarea);//把文本放进滚动面板里面
contentPane.add(jScrollPane);//容器里面放进滚动面板
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo02();
}
}



