GUI编程入门到游戏实战
GUI编程
1、简介
GUI的核心技术: Swing、AWT
缺点: 界面不美观、需要jre环境
为什么要学习?
- 可以写出自己心中想要的一些小工具
- 工作的时候,也可能需要维护到swing界面,概率极小!
- 了解MVC架构,了解监听!
2、AWT
2.1、AWT介绍
AWT(Abstract Window Toolkit),中文译为抽象窗口工具包,该包提供了一套与本地图形界面进行交互的接口,是Java提供的用来建立和设置Java的图形用户界面的基本工具。AWT中的图形函数与操作系统所提供的图形函数之间有着一一对应的关系,称之为peers,当利用AWT编写图形用户界面时,实际上是在利用本地操作系统所提供的图形库。由于不同操作系统的图形库所提供的样式和功能是不一样的,在一个平台上存在的功能在另一个平台上则可能不存在。为了实现Java语言所宣称的“一次编写,到处运行(write once, run anywhere)”的概念,AWT不得不通过牺牲功能来实现平台无关性,也即AWT所提供的图形功能是各种操作系统所提供的图形功能的交集。
2.2、awt的层次结构
2.3、组件和容器
2.3.1 Frame
package com.example.quickstart;
import java.awt.*;
//Gui的第一个页面
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个Java图形界面窗口");
//需要设置可见性,否则不可见
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(Color.lightGray);
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定不可改变
frame.setResizable(false);
}
}
问题:发现窗口无法关闭,停止java程序即可解决
回顾封装:
package com.example.quickstart;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 400, 400, Color.red);
MyFrame myFrame2 = new MyFrame(500, 100, 400, 400, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 500, 400, 400, Color.gray);
MyFrame myFrame4 = new MyFrame(500, 500, 400, 400, Color.black);
//输出窗口的数量
System.out.println("窗口的数量: "+MyFrame.count);
}
}
class MyFrame extends Frame{
static int count=0;//可能存在多个窗口,我们需要一个计数器
public MyFrame(int x,int y,int width,int height,Color backgroundColor){
super("MyFrame"+(++count));//已经构造好了一个Frame
setBounds(x,y,width,height);//设置窗口的起始位置和宽高
setBackground(backgroundColor);//设置窗口背景色
setVisible(true);//设置窗口的可见性
}
}
2.3.2 面板Panel
解决了窗口无法关闭问题
package com.example.quickstart;
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();
//new一个面板
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//设置位置和大小
frame.setBounds(300,300,500,500);
//设置背景颜色
frame.setBackground(Color.lightGray);
//Panel 设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(Color.yellow);
//将Panel放入Frame中
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件
//适配器模式
frame.addWindowListener(new WindowAdapter() {
/**
* 窗口点击关闭的时候需要做的事情
* @param e
*/
@Override
public void windowClosing(WindowEvent e) {
// 结束程序
System.exit(0);
}
});
}
}
2.3.3 布局管理器
-
流式布局
package com.example.quickstart; 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.CENTER //frame.setLayout(new FlowLayout(FlowLayout.LEFT));//按钮靠左 //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//按钮靠右 //frame.setLayout(new FlowLayout(FlowLayout.CENTER));//按钮居中 frame.setSize(400,400); frame.setVisible(true); //把按钮添加进容器Frame frame.add(button1); frame.add(button2); frame.add(button3); } }
-
边框布局
package com.example.quickstart; 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 north = new Button("North"); Button south = new Button("South"); Button center = new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(north,BorderLayout.NORTH); frame.add(south,BorderLayout.SOUTH); frame.add(center,BorderLayout.CENTER); frame.setSize(400,400); frame.setVisible(true); } }
-
表格布局
package com.example.quickstart; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); 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"); Button button6 = new Button("button6"); frame.setLayout(new GridLayout(3,2)); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.setSize(400,400);//窗口大小设置无效 frame.pack(); frame.setVisible(true); } }
案例:实现下面的布局
package com.example.quickstart;
import java.awt.*;
public class Demo1 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(400,400);
frame.setLocation(500,500);
Panel top = new Panel(new BorderLayout());
Panel bottom = new Panel(new BorderLayout());
Panel top_center = new Panel(new GridLayout(2,1));
Panel bottom_center = new Panel(new GridLayout(2,2));
frame.setLayout(new GridLayout(2,1));
frame.add(top);
frame.add(bottom);
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");
Button button6 = new Button("button6");
Button button7 = new Button("button7");
Button button8 = new Button("button8");
Button button9 = new Button("button9");
Button button10 = new Button("button10");
top.add(button1, BorderLayout.WEST);
top.add(top_center,BorderLayout.CENTER);
top.add(button2,BorderLayout.EAST);
bottom.add(button3,BorderLayout.WEST);
bottom.add(bottom_center,BorderLayout.CENTER);
bottom.add(button4,BorderLayout.EAST);
top_center.add(button5);
top_center.add(button6);
bottom_center.add(button7);
bottom_center.add(button8);
bottom_center.add(button9);
bottom_center.add(button10);
frame.setVisible(true);
}
}
总结:
-
Frame是一个顶级窗口
-
Panel无法单独显示,必须添加到某个容器中
-
布局管理器
- 流式布局
- 边框布局
- 表格布局
-
属性:
- 大小:setSize()
- 定位:setLocation()
- 背景颜色:setBackgroundColor()
- 可见性:setVisiable()
- 监听
2.3.4 事件监听
事件监听:当某个事件发生的时候干什么
package com.example.quickstart;
import com.example.util.WindowUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class testActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
//需要一个ActionListen,所以我们构造一个ActionListen
MyActionListen myActionListen = new MyActionListen();
button.addActionListener(myActionListen);
frame.setSize(400,400);//大小设置无效
frame.setLocation(500,500);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
WindowUtils.windowClose(frame);
}
}
class MyActionListen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("我被触发了");
}
}
编写一个窗口工具类用来关闭窗口
package com.example.util;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WindowUtils {
/**
* 关闭窗口的事件
* @param frame
*/
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
多个按钮共享一个事件
package com.example.quickstart;
import com.example.util.WindowUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent2 {
public static void main(String[] args) {
//两个按钮,实现同一个监听
Frame frame = new Frame();
Button start = new Button("start");
Button stop = new Button("stop");
//可以显式的定义触发会返回的命令,如果不显式定义,则会走默认的值
//可以多个按钮只写一个监听类
start.setActionCommand("start");
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);
WindowUtils.windowClose(frame);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获得按钮的信息
System.out.println("按钮被点击了"+e.getActionCommand());
}
}
2.3.5 TextField
package com.example.quickstart;
import com.example.util.WindowUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestTextField {
public static void main(String[] args) {
//一般main方法中只用来启动
new CustomFrame();
}
}
class CustomFrame extends Frame{
public CustomFrame(){
//创建一个文本框
TextField textField = new TextField();
add(textField);
CustomActionListen customActionListen = new CustomActionListen();
//监听这个文本框输入的文字,按下enter就会触发输入框的事件
textField.addActionListener(customActionListen);
//设置替换编码
textField.setEchoChar('*');
pack();
setVisible(true);
WindowUtils.windowClose(this);
}
}
class CustomActionListen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得一些资源
TextField textField= (TextField) e.getSource();
//获得文本输入框中的文本
String text = textField.getText();
System.out.println(text);
textField.setText("");//设置文本框内容为空
}
}
使用封装完成一个简单的加法计算器
package com.example.cases;
import com.example.util.WindowUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class CalcDemo {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,result;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//字符数,即10列宽度
num2 = new TextField(10);
result = new TextField(20);
//1个按钮
Button button = new Button("=");
//1个标签
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(result);
pack();
setVisible(true);
WindowUtils.windowClose(this);
MyCalculatorListener myCalculatorListener = new MyCalculatorListener();
button.addActionListener(myCalculatorListener);
}
//监听器类
//内部类的最大好处就是可以畅通无阻的访问外部类的属性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
result.setText(String.valueOf(n1+n2));
//清空前两个文本框的值
num1.setText("");
num2.setText("");
}
}
}
2.3.6 Graphics画笔
package com.example.quickstart;
import com.example.util.WindowUtils;
import java.awt.*;
public class TestBrush {
public static void main(String[] args) {
new MyBrush().loadFrame();
}
}
class MyBrush extends Frame{
public void loadFrame(){
setBounds(500,500,500,500);
setVisible(true);
WindowUtils.windowClose(this);
}
//画笔
@Override
public void paint(Graphics g) {
g.setColor(Color.RED);
g.drawOval(100,100,100,100);
g.fillOval(200,100,100,100);
//画笔用完,将它还原到最初的颜色
}
}
2.4、鼠标监听
案例:在面板上可以连续画点
package com.example.quickstart;
import com.example.util.WindowUtils;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MouseListenerFrame("画图");
}
}
class MouseListenerFrame extends Frame {
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList<Point> pointList;
public MouseListenerFrame(String title){
super(title);
setBounds(400,400,500,500);
//存鼠标点击的点
pointList=new ArrayList<>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
WindowUtils.windowClose(this);
}
@Override
public void paint(Graphics g) {
//画画需要监听鼠标的事件
for (Point point : pointList) {
g.setColor(Color.RED);
g.fillOval(point.x,point.y,10,10);
}
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标动作:按下、弹起、按下不放
@Override
public void mousePressed(MouseEvent e) {
MouseListenerFrame frame= (MouseListenerFrame) e.getSource();
Point point = new Point(e.getX(), e.getY());
pointList.add(point);
frame.repaint();
}
}
}
2.5、窗口监听
package com.example.quickstart;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new WindowListenerFrame();
}
}
class WindowListenerFrame extends Frame{
public WindowListenerFrame(){
setBackground(Color.red);
setBounds(400,400,500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");//窗口关闭时触发
WindowListenerFrame frame= (WindowListenerFrame) e.getSource();
frame.setTitle("我走了");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified");//最小化时触发
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");//窗口激活时触发
WindowListenerFrame frame= (WindowListenerFrame) e.getSource();
frame.setTitle("我来了");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated");//窗口隐藏时激活
}
});
}
}
2.6、键盘监听
package com.example.quickstart;
import com.example.util.WindowUtils;
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(400,400,500,500);
setVisible(true);
WindowUtils.windowClose(this);
//添加键盘监听
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键 "+keyCode);
}
}
});
}
}
3、Swing
3.1、JFrame
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//初始化
public void init(){
JFrame frame = new JFrame("JFrame窗口");
frame.setVisible(true);
frame.setBounds(400,400,500,500);
//frame.setBackground(Color.lightGray);//无效,需要给容器设置背景色
//获得一个容器
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.lightGray);
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置文字 JLabel
JLabel label = new JLabel("欢迎学习swing");
//设置字体样式
label.setFont(new Font("",Font.BOLD,18));
//让文本内容居中 设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(label);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
3.2、Dialog
package com.example.swing;
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);
setBounds(400,400,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame放组件需要一个容器
Container contentPane = getContentPane();
//绝对布局
contentPane.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(150,10,200,30);
contentPane.add(button);
//点击这个按钮的时候弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
class MyDialog extends JDialog{
public MyDialog(){
setVisible(true);
setBounds(500,480,300,300);
Container contentPane = getContentPane();
//绝对定位
contentPane.setLayout(null);
JLabel jLabel = new JLabel("欢迎来到我的世界");
//设置水平对齐方式
jLabel.setHorizontalAlignment(JLabel.CENTER);
//设置字体样式
jLabel.setFont(new Font("",Font.BOLD,20));
//因为设置绝对定位,如果不设置JLabel将无法显示
jLabel.setBounds(50,10,200,30);
contentPane.add(jLabel);
}
}
3.3、Icon
package com.example.swing;
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 jLabel = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
setBounds(400,400,500,500);
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、ImageIcon
package com.example.swing;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片的地址
URL url = ImageIconDemo.class.getResource("img1.jpg");//相对于该类的地址
JLabel jLabel = new JLabel("ImageIcon");
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
setBounds(400,400,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.5、JPanel
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(2, 1, 10, 10));//2行1列间距为10
JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1, 2));
panel1.add(new JButton("button1"));
panel1.add(new JButton("button2"));
panel1.add(new JButton("button3"));
panel2.add(new JButton("button4"));
panel2.add(new JButton("button5"));
contentPane.add(panel1);
contentPane.add(panel2);
setVisible(true);
setBounds(400,400,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
3.6、JScrollPane
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container contentPane = getContentPane();
JTextArea jTextArea = new JTextArea(20, 20);//每一行只能写20个字,一共20行
jTextArea.setText("欢迎来到我的世界");
jTextArea.setFont(new Font("",Font.BOLD,20));
//JScroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
setVisible(true);
setBounds(400,400,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.7、案例:图片按钮
package com.example.cases;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageButton extends JFrame {
public ImageButton(){
Container contentPane = getContentPane();
//将一个图片变为图标
URL url = ImageButton.class.getResource("../swing/img1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
//设置按钮提示,即悬停在按钮上时出现提示
jButton.setToolTipText("图片按钮");
contentPane.add(jButton);
setVisible(true);
setBounds(400,400,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageButton();
}
}
3.8、JRadioButton
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class JRadioButtonDemo extends JFrame {
public JRadioButtonDemo(){
Container contentPane = getContentPane();
//创建单选框按钮
JRadioButton radio1 = new JRadioButton("radio1");
JRadioButton radio2 = new JRadioButton("radio2");
JRadioButton radio3 = new JRadioButton("radio3");
//由于单选框按钮只能选择一个,所以需要分组;一个分组中只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radio1);
buttonGroup.add(radio2);
buttonGroup.add(radio3);
contentPane.add(radio1);
contentPane.add(radio2);
contentPane.add(radio3);
setLayout(new FlowLayout());
setVisible(true);
setBounds(400,400,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JRadioButtonDemo();
}
}
3.9、JCheckBox
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class JCheckBoxDemo extends JFrame {
public JCheckBoxDemo(){
Container contentPane = getContentPane();
//创建JCheckBox
JCheckBox checkbox1 = new JCheckBox("checkbox1");
JCheckBox checkbox2 = new JCheckBox("checkbox2");
JCheckBox checkbox3 = new JCheckBox("checkbox3");
contentPane.add(checkbox1);
contentPane.add(checkbox2);
contentPane.add(checkbox3);
setVisible(true);
setLayout(new FlowLayout());
setBounds(400,400,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JCheckBoxDemo();
}
}
3.10、JComboBox
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class ComboBoxDemo extends JFrame {
public ComboBoxDemo(){
Container contentPane = getContentPane();
//创建一个下拉列表框
JComboBox<String> comboBox = new JComboBox<>();
comboBox.addItem(null);
comboBox.addItem("欢迎");
comboBox.addItem("来到");
comboBox.addItem("我的");
comboBox.addItem("世界");
contentPane.add(comboBox);
setLayout(new FlowLayout());
setVisible(true);
setBounds(400,400,500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ComboBoxDemo();
}
}
3.11、JList
package com.example.swing;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class JListDemo extends JFrame {
public JListDemo(){
Container contentPane = getContentPane();
//生成列表内容
// String[] contents={"欢迎","来到","我的","世界"};
Vector<String> contents = new Vector<>();
//创建列表并放入内容
JList<String> jList = new JList<>(contents);
jList.setFixedCellWidth(200);
jList.setFixedCellHeight(30);
contents.add("欢迎");
contents.add("来到");
contents.add("我的");
contents.add("世界");
contentPane.add(jList);
setVisible(true);
setBounds(400,400,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
public static void main(String[] args) {
new JListDemo();
}
}
- 应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容
3.12、JTextField
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class JTextFieldDemo extends JFrame {
public JTextFieldDemo(){
Container contentPane = getContentPane();
JTextField textField1 = new JTextField("欢迎来到我的世界");
JTextField textField2 = new JTextField("欢迎来到我的世界",20);//指定列表框宽度
contentPane.add(textField1);
contentPane.add(textField2);
setVisible(true);
setBounds(400,400,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
public static void main(String[] args) {
new JTextFieldDemo();
}
}
3.13、JPasswordField
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class JPasswordFieldDemo extends JFrame {
public JPasswordFieldDemo(){
Container contentPane = getContentPane();
JPasswordField passwordField = new JPasswordField("",20);
passwordField.setEchoChar('¥');
contentPane.add(passwordField);
setVisible(true);
setBounds(400,400,300,300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPasswordFieldDemo();
}
}
3.14、JTextArea
package com.example.swing;
import javax.swing.*;
import java.awt.*;
public class JTextAreaDemo extends JFrame {
public JTextAreaDemo(){
Container contentPane = getContentPane();
//创建一个文本域,20行30列
JTextArea textArea = new JTextArea(20, 30);
textArea.setText("欢迎来到我的世界");
textArea.setFont(new Font("",Font.BOLD,20));
//JScroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(scrollPane);
setVisible(true);
setBounds(400,400,500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JTextAreaDemo();
}
}
4、贪吃蛇案例
1、分析
- 帧,如果时间片足够小,就是动画,一秒30帧、60帧。连起来就是动画,拆开就是静态的图片
- 键盘监听
- 定时器Timer
2、效果图
3、素材
-
up.jpg
-
right.jpg
-
bottom.jpg
-
left.jpg
-
header.jpg
-
food.jpg
-
body.jpg
ps:可自行下载使用,也可自定义素材
4、代码
游戏启动类
package com.example.snake;
import javax.swing.*;
//游戏的启动类
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new GamePanel());
frame.setResizable(false);//窗口大小不可变
frame.setBounds(550,100,900,720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
游戏界面类
package com.example.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
//游戏的面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
//定义蛇的数据结构
//最多816
int length;//蛇的长度
int[] snakeX=new int[600];//蛇的x坐标
int[] snakeY=new int[500];//蛇的y坐标
String direction;//蛇的方向
int foodX;//食物坐标x
int foodY;//食物坐标y
int score;//得分
boolean isStart=false;//游戏当前状态:开始、暂停;默认是暂停
boolean isFail=false;//游戏是否失败;默认没有失败
Timer timer=new Timer(100,this);//定时器100ms刷新一次
Random random=new Random();
public GamePanel(){
init();
//获得焦点和键盘事件
setFocusable(true);//获得焦点事件
addKeyListener(this);//获得键盘监听事件
}
/**
* 初始化方法
*/
public void init(){
length=3;
snakeX[0]=100;snakeY[0]=100;//脑袋的坐标
snakeX[1]=75;snakeY[1]=100;//第一节身体的坐标
snakeX[2]=50;snakeY[2]=100;//第二节身体的坐标
foodX=25+25*random.nextInt(34);
foodY=75+25*random.nextInt(24);
direction="right";
score=0;
timer.start();//游戏一开始就开启定时器
}
/**
* 绘制面板,游戏中的所有东西,都用这个画笔来画
* @param g
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
//绘制静态的面板
setBackground(Color.black);//背景色黑色
Data.headerIcon.paintIcon(this,g,25,13);//头部信息栏
g.setColor(Color.white);
g.fillRect(25,75,850,600);//默认的游戏界面
//画积分
g.setColor(Color.lightGray);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度: "+length,725,30);
g.drawString("分数: "+score,725,55);
//画食物
Data.foodIcon.paintIcon(this,g,foodX,foodY);
if("right".equalsIgnoreCase(direction)){
Data.rightIcon.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if("left".equalsIgnoreCase(direction)){
Data.leftIcon.paintIcon(this,g,snakeX[0],snakeY[0]);
}
else if("up".equalsIgnoreCase(direction)){
Data.upIcon.paintIcon(this,g,snakeX[0],snakeY[0]);
}
else if("bottom".equalsIgnoreCase(direction)){
Data.bottomIcon.paintIcon(this,g,snakeX[0],snakeY[0]);
}
for (int i = 1; i < length; i++) {
Data.bodyIcon.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if(isStart==false){
g.setColor(Color.lightGray);
g.setFont(new Font("微软雅黑",Font.BOLD,50));
g.drawString("按下空格开始游戏",270,400);
}
if(isFail){
g.setColor(Color.red);
g.setFont(new Font("微软雅黑",Font.BOLD,50));
g.drawString("游戏失败,按下空格重新开始",125,350);
g.drawString("最终得分: "+score,335,400);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(isStart && !isFail){
if(snakeX[0]==foodX && snakeY[0]==foodY){
//长度加1
length++;
//分数加10
score+=10;
//再次随机食物
foodX=25+25*random.nextInt(34);
foodY=75+25*random.nextInt(24);
}
//如果游戏是开始状态,让小蛇动起来
//移动
for (int i = length-1; i > 0; i--) {
snakeX[i]=snakeX[i-1];
snakeY[i]=snakeY[i-1];
}
if("up".equalsIgnoreCase(direction)){
snakeY[0]-=25;
if(snakeY[0]<75){
snakeY[0]=650;
}
}
else if("bottom".equalsIgnoreCase(direction)){
snakeY[0]+=25;
if(snakeY[0]>650){
snakeY[0]=75;
}
}
else if("left".equalsIgnoreCase(direction)){
snakeX[0]-=25;
if(snakeX[0]<25){
snakeX[0]=850;
}
}
else if("right".equalsIgnoreCase(direction)){
snakeX[0]+=25;
if(snakeX[0]>850){
snakeX[0]=25;
}
}
//判断是否失败,撞到自己就是失败
for (int i = 1; i < length; i++) {
if(snakeX[0]==snakeX[i] && snakeY[0]==snakeY[i]){
isFail=true;
timer.stop();
}
}
repaint();
}
timer.start();//定时器开启
}
/**
* 监听键盘按下
* @param e
*/
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//获取按下的键是哪一个
if(keyCode == KeyEvent.VK_SPACE){
if(isFail){
//重新开始
isFail=false;
init();
}
else{
//按下空格后的逻辑
isStart=!isStart;//取反
}
repaint();//重新绘制
}
if(keyCode == KeyEvent.VK_UP){
//按下上键
if(!"bottom".equalsIgnoreCase(direction)){
direction="up";
}
}else if(keyCode == KeyEvent.VK_DOWN){
//按下下键
if(!"up".equalsIgnoreCase(direction)){
direction="bottom";
}
}else if(keyCode == KeyEvent.VK_LEFT){
//按下左键
if(!"right".equalsIgnoreCase(direction)){
direction="left";
}
}else if(keyCode == KeyEvent.VK_RIGHT){
//按下右键
if(!"left".equalsIgnoreCase(direction)){
direction="right";
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
游戏数据类
package com.example.snake;
import javax.swing.*;
import java.net.URL;
public class Data {
private static URL headerURL=Data.class.getResource("image/header.jpg");
private static URL upURL=Data.class.getResource("image/up.jpg");
private static URL rightURL=Data.class.getResource("image/right.jpg");
private static URL bottomURL=Data.class.getResource("image/bottom.jpg");
private static URL leftURL=Data.class.getResource("image/left.jpg");
private static URL foodURL=Data.class.getResource("image/food.jpg");
private static URL bodyURL=Data.class.getResource("image/body.jpg");
public static ImageIcon headerIcon=new ImageIcon(headerURL);
public static ImageIcon upIcon=new ImageIcon(upURL);
public static ImageIcon rightIcon=new ImageIcon(rightURL);
public static ImageIcon bottomIcon=new ImageIcon(bottomURL);
public static ImageIcon leftIcon=new ImageIcon(leftURL);
public static ImageIcon foodIcon=new ImageIcon(foodURL);
public static ImageIcon bodyIcon=new ImageIcon(bodyURL);
}
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!