GUI编程
-
这是什么?
-
它怎么玩?
-
该如何去再我们平时运用?
组件
-
组件
-
弹窗
-
面板
-
文本框
-
列表框
-
按钮
-
图片
-
监听事件
-
鼠标
-
键盘事件
-
外挂:Java
1、简介
GUI的核心技术:Swing AWT,
-
因为不美观
-
需要jre环境!
MVC架构
-
可以写出自己心中想要的小工具
-
工作的时候,也可能需要维护swing界面,概率极小
-
了解MVC架构,了解监听器!
2 AWT
2.1AWT介绍
-
包含了很多类和接口! GUI:图形用户界面
-
元素:窗口、按钮、文本框
-
Java.awt
2.2组件和容器
1、Frame
{
public static void main(String[] args) {
//Frame,JDK 看源码
Frame frame = new Frame("我的第一个java图形界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置床窗口大小
frame.setSize(400,400);
//设置背景颜色 Color
frame.setBackground(new Color(39, 122, 81));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关不掉,停止Java程序运行
尝试回顾封装
package com.HuoLong.lesson01;
import java.awt.*;
import java.io.File;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口 new
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.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);//不用
setBounds(x,y,w,h);//位置和大小
setVisible(true);
}
}
2.面板Panel
解决了关闭事件!
package com.HuoLong.lesson01;
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(44, 148, 96));
//panel 设置坐标,相对于Frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(141, 30, 86));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:new WindowAdapter
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
3.布局管理器
-
流式布局:从左到右 Flow
package com.HuoLong.lesson01;
import java.awt.*;
public class TestLowLayout {
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());
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setVisible(true);
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
}
} -
东西南北中: Border
package com.HuoLong.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.HuoLong.lesson01;
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);
}
}
代码实现:
package com.HuoLong.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ExDemo {
public static void main(String[] args) {
//总 Frame
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true);
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
//4个面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
//上面OK
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
总结:
-
Frame是一个顶级窗口
-
Panel无法单独显示,必须添加到某个容器中。
-
布局管理器
-
流式
-
东西南北中
-
表格
-
4.大小,定位,背景颜色,可见性,监听!
4.事件监听
事件监听:当某个事情发生的时候,干什么?
package com.HuoLong.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 TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
//因为,addActionListener()需要一个ActionListener,所以我们需要构造一个 ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();//自适应
windowClose(frame);
frame.setVisible(true);
}
//关闭窗体事件
private static void windowClose(Frame frame){//
frame.addWindowListener(new WindowAdapter() {
多个按钮,共用一个事件
package com.HuoLong.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 TestActionEvent02 {
public static void main(String[] args) {
//两个按钮实现同一个监听
//开始 停止
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!
//可以可以多个按钮只写一个监听类
button2.setActionCommand("button2-stop");
//
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
//
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
windowClosed(frame);
frame.pack();
frame.setVisible(true);
}
static void windowClosed(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("按钮被点击了:"+e.getActionCommand());
}
}
5.输入框TextField 监听
package com.HuoLong.lesson02;
import javax.swing.plaf.PanelUI;
import java.awt.*;
import java.awt.event.*;
public class TestText01 {
public static void main(String[] args) {
//启动!
MyFrame myFrame = new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();//单行文本TextField 1
add(textField);//Frame.add()
Button button = new Button("按钮");
add(button);
//监听这个文本框输入的文字
MyActionLister2 myActionLister2 = new MyActionLister2();
//按下enter 就会触发这个输入框的事件
textField.addActionListener(myActionLister2);
button.addActionListener(myActionLister2);
//设置替换编码
textField.setEchoChar('*');//输入替换* //2.替换setEchoChar
windowLister();
setVisible(true);
pack();
}
public void windowLister(){
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionLister2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
Button button= (Button) e.getSource();//e.getSource获得一些资源// 1
System.out.println(button.getLabel());//获得输入框的文本//
//button.setText(" ");//null "" //3
}
}
2.6、简易计算器,组合+内部类回顾复习
2.7画笔
package com.HuoLong.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) {
//画笔需要有颜色,画笔可以画画
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.8鼠标监听
目的:想要实现鼠标画画!
package com.HuoLong.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseLister {
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 MyMouseLister());
}
@Override
public void paint(Graphics g) {
//画画,需要监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point points= (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(points.x,points.y,10,10);
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseLister extends MouseAdapter{
//鼠标 按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
//这里我们点击的时候,就会在界面上产生一个点
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();
}
}
}
2.9 窗口监听
package com.HuoLong.lesson03;
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);
//addWindowListener(new MyWindowLister());
this.addWindowListener(new WindowAdapter() {
//匿名内部类
@Override
public void windowOpened(WindowEvent e) {
System.out.println();
}
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame frame= (WindowFrame) e.getSource();
frame.setTitle("被激活了");
super.windowActivated(e);
}
});
}
}
2.10 键盘监听
package com.HuoLong.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyLister {
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();//不需要记录这个数值,直接使用静态属性 VK_XXX
System.out.println(KeyCode);
if (KeyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
//根据按下的不用操作,产生不同结果
}
});
}
}
3、Swing
3.1窗口、面板
package com.HuoLong.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化 干什么
public void init(){
//顶级窗口
JFrame jf = new JFrame("这是一个JFrame窗口");
jf.setVisible(true);
/*jf.setBounds(100,100,200,200);*/
/*jf.setBackground(Color.cyan);*/
//设置文字JLabel
JLabel jLabel = new JLabel("欢迎来到火龙系列节目");
//容器实例化
Container contentPane = jf.getContentPane();
contentPane.add(jLabel);
contentPane.setBackground(Color.cyan);
contentPane.setBounds(100,100,200,200);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//WindowConstants常量
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
jf.setDefaultCloseOperation(WindowConstants.Exit_ON_CLOSE);//JFrame关闭事件 WindowConstants常量Container contentPane = jf.getContentPane();//容器init()//初始化,很多游戏代码里面看到的
标签居中
package com.HuoLong.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
//获得一个容器
this.setVisible(true);
this.setBounds(10,10,200,300);
Container contentPane = this.getContentPane();//获得一个容器,得到一个对象
contentPane.setBackground(Color.yellow);
JLabel jLabel = new JLabel("欢迎来到火龙Java");
add(jLabel);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//让文本标签居中 设置水平对齐 setH..
jLabel.setHorizontalAlignment(SwingConstants.CENTER);//0
}
}
3.2弹窗
JDialog,用来被弹出,默认就有关闭事件!
package com.HuoLong.lesson04;
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,50);
//点击这个按钮的时候,弹出一个弹窗
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);
JLabel label = new JLabel("火龙老师带你学Java");
label.setBounds(100,100,100,100);
container.add(label);
}
}
3.3、标签
label
new JLabel("xxx");
图标Icon
implements Icon接口
package com.HuoLong.lesson04;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame继承
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 contentPane = this.getContentPane();
contentPane.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;
}
}
图片Icon
package com.HuoLong.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片的地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("logo.png");//url 具体的地址 获取当前这个class这个类的下面这个同级资源//
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
this.getClass.getResoucre(); //url 具体的地址 获取当前这个class这个类的下面这个同级资源
3.4、面板
JPanel
package com.HuoLong.lesson05;
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));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,1));
JPanel panel4 = new JPanel(new GridLayout(3,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
JScrollPanel
package com.HuoLong.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("欢迎学习火龙Java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5、按钮
图片按钮
package com.HuoLong.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container container = getContentPane();
//将一个图片变成一个图标
URL url = JButtonDemo01.class.getResource("logo.png");
ImageIcon icon = new ImageIcon(url);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选按钮
package com.HuoLong.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02(){
Container container = getContentPane();
//将一个图片变成一个图标
URL url = JButtonDemo01.class.getResource("logo.png");
ImageIcon icon = new ImageIcon(url);
//单选框
JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组,一个组只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
复选按钮
package com.HuoLong.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03(){
Container container = getContentPane();
//将一个图片变成一个图标
URL url = JButtonDemo01.class.getResource("logo.png");
ImageIcon icon = new ImageIcon(url);
//多选框
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 JButtonDemo03();
}
}
3.6、列表
-
下拉框
package com.HuoLong.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestConboboxDemo01 extends JFrame {
public TestConboboxDemo01(){
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 TestConboboxDemo01();
}
} -
列表框
package com.HuoLong.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestConboboxDemo02 extends JFrame {
public TestConboboxDemo02(){
Container container = this.getContentPane();
//生成列表的内容
//String[] contents ={"1","2","3"};
Vector contents = new Vector();
//列表种需要放入内容
JList jList = new JList(contents);
contents.add("zhangsan");
contents.add("lisi");
contents.add("wangwu");
container.add(jList);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestConboboxDemo02();
}
} -
应用场景
-
选择地区,或者一些单个选项。
-
列表,展示信息,一般是动态扩容。
-
3.7、文本框
-
文本框
package com.HuoLong.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container container = this.getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world",20);
container.add(textField,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();
}
} -
密码框
package com.HuoLong.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02(){
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();//***
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
} -
文本域
-
package com.HuoLong.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("欢迎学习火龙Java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
贪吃蛇
帧,如果时间片足够小,就是动画,一秒30帧 60帧。连起来是动画,拆开就是静态的图片!
键盘监听
定时器Timer
GamePanel1.java
package com.HuoLong.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 GamePanel1 extends JPanel implements KeyListener, ActionListener {
//定义蛇的数据结构
int length;//设的长度
int[] snakeX =new int[600];//蛇的x坐标 25*25
int[] snakeY =new int[500];//蛇的x坐标 25*25
String fx;//初始方向向右
//食物的坐标
int foodx;
int foody;
Random random=new Random();
int score;//成绩
//游戏当前的状态: 开始,停止
boolean isStart=false;//默认是不开始
boolean isFail=false;//游戏状态失败
//定时器 以ms为单位 1000ms=1s
Timer timer=new Timer(100,this); //100毫秒执行一次!
//构造器
public GamePanel1(){
init();
//获得焦点和键盘事件
this.setFocusable(true);//获得焦点事件 //
this.addKeyListener(this);//获得键盘监听事件 //
timer.start();//游戏一开始定时器就启动
}
//初始化方法
public void init(){
length=3;
snakeX[0]=100;snakeY[0]=100;//脑袋的坐标
snakeX[1]=75;snakeY[1]=100;//第一个身体的坐标
snakeX[2]=50;snakeY[2]=100;//第二个身体的坐标
fx="R";
//把食物随机分布在界面上
foodx=25+25*random.nextInt(34);//
foody=75+25*random.nextInt(24);//
score=0;
}
//绘制面板,我们游戏中的所有东西都使用这个画笔来画
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
//绘制静态的面板
this.setBackground(Color.WHITE);
Data.header.paintIcon(this,g,25,11);//头部广告栏画上去 c:画到哪 第二个参数:画到哪
g.fillRect(25,75,850,600);//默认的游戏界面
//画积分
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,18));// 粗体 18号字体
g.drawString("长度"+length,750,35);
g.drawString("分数"+score,750,50);
//画食物
Data.food.paintIcon(this,g,foodx,foody);
//把小蛇画上去
if (fx.equals("R")){
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向右,需要通过方向来判断
}else if (fx.equals("L")){
Data.Left.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向右,需要通过方向来判断
}else if (fx.equals("U")){
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向右,需要通过方向来判断
}else if (fx.equals("D")){
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向右,需要通过方向来判断
}
for (int i = 1; i <length ; i++) {
Data.boby.paintIcon(this,g,snakeX[i],snakeY[i]);// 第一个身体坐标
}
//游戏状态
if (isStart==false){
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
g.drawString("按下空格开始游戏",300,300);
}
if (isFail) {
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
g.drawString("失败,按下空格重新开始",300,300);
}
}
//键盘监听事件
@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){
fx="U";
}else if (keyCode==KeyEvent.VK_DOWN){
fx="D";
}else if (keyCode==KeyEvent.VK_LEFT){
fx="L";
}else if (keyCode==KeyEvent.VK_RIGHT){
fx="R";
}
}
//事件监听--需要通过定时器来,1s=10次
@Override
public void actionPerformed(ActionEvent e) {
if(isStart && isFail == false){//如果游戏是开始状态,就让小蛇动起来
//吃事物
if (snakeX[0]==foodx&&snakeY[0]==foody){
length++;//长度+1
//分数加10
score=score+10;
//再次随机食物
foodx=25+25*random.nextInt(34);//
foody=75+25*random.nextInt(24);//
}
//移动
for (int i = length-1; i >0 ; i--) {//后一节移动到前一节的位置 snakeX[1]=snakeX[0];snakeY[1]=snakeY[0];
snakeX[i]=snakeX[i-1];
snakeY[i]=snakeY[i-1];
}
//走向
if (fx.equals("R")){
snakeX[0]=snakeX[0]+25;
if (snakeX[0]>850){snakeX[0]=25;} //边界判断
}else if (fx.equals("L")){
snakeX[0]=snakeX[0]-25;
if (snakeX[0]<25){snakeX[0]=850;} //边界判断
}else if (fx.equals("U")){
snakeY[0]=snakeY[0]-25;
if (snakeY[0]<75){snakeY[0]=650;} //边界判断
}else if (fx.equals("D")){
snakeY[0]=snakeY[0]+25;
if (snakeY[0]>650){snakeY[0]=75;} //边界判断
}
//失败判断,撞到自己就算失败
for (int i = 1; i <length ; i++) {
if (snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
isFail=true;
}
}
repaint();//重画页面
}
timer.start();//定时器开启
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
Data.java
package com.HuoLong.snake;
import javax.swing.*;
import java.net.URL;
//数据中心
public class Data {
//相对路径 tx.jpg
//绝对路径 / 相当于当前的项目
public static URL headerURL =Data.class.getResource("/statics/header.png");
public static ImageIcon header=new ImageIcon(headerURL);
public static URL upURL =Data.class.getResource("/statics/up.png");
public static URL downURL =Data.class.getResource("/statics/down.png");
public static URL LeftURL =Data.class.getResource("/statics/left.png");
public static URL rightURL =Data.class.getResource("/statics/right.png");
public static ImageIcon up=new ImageIcon(upURL);
public static ImageIcon down=new ImageIcon(downURL);
public static ImageIcon Left=new ImageIcon(LeftURL);
public static ImageIcon right=new ImageIcon(rightURL);
public static URL bobyURL =Data.class.getResource("/statics/body.png");
public static ImageIcon boby=new ImageIcon(bobyURL);
public static URL foodURL =Data.class.getResource("/statics/food.png");
public static ImageIcon food=new ImageIcon(foodURL);
}
StartGame.java
package com.HuoLong.snake;
import javax.swing.*;
//游戏的主启动类
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new GamePanel1()); //
frame.setBounds(10,10,900,720);
frame.setResizable(false);//窗口大小不可变
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//正常的游戏界面都应该在面板上
}
}
1.定义数据
2.画上去
3.监听事件
键盘
事件