文章目录
1、GUI(graphic user interface)
- 核心技术:Swing、AWT
- 因为界面不美观。
- 需要jre环境!
- 为什么我们要学习?
1.可以写出自己心中想要的一些小工具
2.工作时候,也可能需要维护到swig界面,概率极小!
3.了解MVC架构,了解监听!
2、 AWT(abstract windows tools)
- 1.包含了很多类和接口!GUI!
- 2.元素:窗口,按钮,文本框
- 组件和容器
alt+7可以查看类结构,
2.1、窗口
package com.ciian.gui;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
//查看源码ctrl+鼠标左键,alt+7显示类结构
Frame frame = new Frame("我的第一个Java图形界面窗口");
//设置可见性
frame.setVisible(true);
//设置大小
frame.setSize(400,400);
//设置初始弹出位置
frame.setLocation(200,200);
//设置背景颜色
frame.setBackground(Color.cyan);
//设置大小固定
frame.setResizable(false);
}
}
- 展示多个窗口
package com.ciian.gui;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
Frame myframe1 = new MyFrame(100,100,200,200,Color.cyan);
Frame myframe2 = new MyFrame(200,200,200,200,Color.yellow);
Frame myframe3 = new MyFrame(300,300,200,200,Color.gray);
Frame myframe4 = new MyFrame(400,400,200,200,Color.black);
}
}
class MyFrame extends Frame {
static int count = 0;
public MyFrame(int x,int y,int w,int h, Color color) {
super("MyFrame"+ (++count));
this.setVisible(true);
this.setBounds(x,y,w,h);
this.setBackground(color);
}
}
2.2、Panel面板(会自动创建一个流布局)
package com.ciian;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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(68, 180, 32));
//panel坐标,相对frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(168, 22, 22));
//panel是Component
frame.add(panel);
frame.setVisible(true);
//监听事件,关闭窗口
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭X时执行
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
} );
}
}
2.3、三种布局管理器
流式布局(FlowLayOut)
package com.ciian;
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());
//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);
}
}
东西南北中(BorderLayOut)
package com.ciian;
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);
}
}
表格布局(GridLayOut)
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);
}
}
- exercise
package com.ciian;
import java.awt.*;
public class ExTest {
public static void main(String[] args) {
//总Frame
Frame frame = new Frame();
frame.setSize(400, 300);
frame.setLocation(300, 400);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
frame.setLayout(new GridLayout(2, 1));
//14个面板
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);
//1中间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);
}
}
总结
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中。
3.布局管理器
1.流式
2.东西南北中
3.表格
4.大小,定位,背景颜色,可见性,监听!
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭X时执行
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
} );
2.4、事件监听
package com.ciian.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 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();
windowsClose(frame);
frame.setVisible(true);
}
private static void windowsClose(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.ciian.lesson3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
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);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了:msg=》" + e.getActionCommand());
if(e.getActionCommand().equals("start")){
}
}
}
输入框事件监听
package com.ciian.lesson3;
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();
//按下enter就会触发这个输入框的事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();
//获得一些资源返回的一个对象
System.out.println(field.getText());//获得输入框的文本
field.setText("");//null ""
}
}
2.6、简易计算器,组合+内部类回顾复习!
组合+内部类
oop原则:组合,大于继承!
不会有很大的耦合性,优先使用组合
计算机面向对象法
package com.ciian.lesson3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
Frame frame = new Calculator();
}
}
//计算器类
class Calculator extends Frame {
public Calculator() {
//3个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);//字符数
TextField num3 = new TextField(20);//字符数
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener {
//获取三个变量
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1,获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.将这个值+法运算后,放到第三个框
num3.setText(""+(n1+n2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
写成内部类
package com.ciian.lesson3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc2 {
public static void main(String[] args) {
new Calculator2().loadFrame();
}
}
//计算器类
class Calculator2 extends Frame {
//属性
TextField num1, num2, num3;
//方法
public void loadFrame() {
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MycalculatorListener(this));
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MycalculatorListener implements ActionListener {
//获取计算器这个对象,在一个类中组合另外一个类:
Calculator2 calculator = null;
public MycalculatorListener(Calculator2 calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
//2.将这个值+法运算后,放到第三个框
//3。清除前两个框
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText("" + (n1 + n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
2.7、画笔
package com.ciian.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.ciian.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame {
//画画需要画笔,需要监所鼠标当前的位置,需要集合来存储这个点
ArrayList<Point> points;
//画画需要画笔,需要监所鼠标当前的位置,需要集合来存储这个点
public MyFrame(String title) {
super(title);
setBounds(200, 200, 400, 300);
setVisible(true);
//存鼠标点击的点
points = new ArrayList<>();
//鼠标监所听器,正对这个窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator<Point> iterator = points.iterator();
while (iterator.hasNext()) {
Point point = iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面上
public void addPoint(Point point) {
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
frame.addPoint(new Point(e.getX(),e.getY()));
//重新画图
frame.repaint();
}
}
}
2.9、窗口监听
package com.ciian.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);
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
//关闭窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame frame = (WindowFrame) e.getSource();
frame.setTitle("被激活了");
}
//激活窗口
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,通过按钮,隐藏当前窗口
System.exit(0);//正常退出
}
}
);
}
}
2.10、键盘监听
package com.ciian.lesson03;
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);
addKeyListener(
new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘下的键是哪一个,当前的码
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode==KeyEvent.VK_UP){
System.out.println("上");
}
}
}
);
}
}
2.Swing
3.1、窗口、面板
package com.ciian.lesson04;
import 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);
//设置文字Jlabel
JLabel jLabel = new JLabel("呵呵");
jFrame.add(jLabel);
// 让文本居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化
Container contentPane = jFrame.getContentPane();
contentPane.setBackground(Color.cyan);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
3.2、弹窗
JDialog,用来被弹出,默认就有关闭事件
绝对布局:用setBounds设置位置
package com.ciian.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 contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
// 按钮
JButton button = new JButton("弹出一个对话框");
button.setBounds(30,30,200,50);
button.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
}
);
contentPane.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);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
Label label = new Label("学Java");
contentPane.add(label);
}
}
3.3、标签
label
new JLabel("xxx");
图标ICON
package com.ciian.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) throws HeadlessException {
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 = 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.ciian.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImgIconDemo extends JFrame {
public ImgIconDemo() {
JLabel label = new JLabel("ImgIcon");
URL url = ImgIconDemo.class.getResource("pipip.png");
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);
}
public static void main(String[] args) {
new ImgIconDemo();
}
}
3.4、面板
- JPanel
package com.ciian.lesson05;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10));
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
JPanel jPanel2 = new JPanel(new GridLayout(1, 2));
JPanel jPanel3 = new JPanel(new GridLayout(2 ,1));
JPanel jPanel4 = new JPanel(new GridLayout(3, 2));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
contentPane.add(jPanel4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
- JScrollPanel
package com.ciian.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container contentPane = getContentPane();
// 文本
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("学Java");
// Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
setVisible(true);
setBounds(100,100,300,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5、按钮
图片按钮
package com.ciian.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo extends JFrame {
public JButtonDemo() {
Container contentPane = getContentPane();
// 将一个图片变成图标
URL resource = JButtonDemo.class.getResource("pipip.png");
ImageIcon imageIcon = new ImageIcon(resource);
// 将这个图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
jButton.setToolTipText("图片按钮");
// add
contentPane.add(jButton);
setVisible(true);
setSize(300,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
- 单选按钮
package com.ciian.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container contentPane = getContentPane();
// 将一个图片变成图标
URL resource = JButtonDemo02.class.getResource("pipip.png");
ImageIcon imageIcon = new ImageIcon(resource);
// 单选框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
// 分组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
contentPane.add(jRadioButton1,BorderLayout.CENTER);
contentPane.add(jRadioButton2,BorderLayout.NORTH);
contentPane.add(jRadioButton3,BorderLayout.SOUTH);
setVisible(true);
setSize(300,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
- 复选按钮
package com.ciian.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container contentPane = getContentPane();
// 将一个图片变成图标
URL resource = JButtonDemo03.class.getResource("pipip.png");
ImageIcon imageIcon = new ImageIcon(resource);
// 复选框
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2 = new JCheckBox("checkBox2");
contentPane.add(checkBox1,BorderLayout.NORTH);
contentPane.add(checkBox2,BorderLayout.SOUTH);
setVisible(true);
setSize(300,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6、列表
- 下拉框
package com.ciian.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo extends JFrame {
public TestComboboxDemo() {
Container contentPane = getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
contentPane.add(status);
// status.addActionListener();
setVisible(true);
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo();
}
}
- 列表框
package com.ciian.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container contentPane = getContentPane();
// String[] contents={"1","2","3"};
Vector contents = new Vector();
JList jList = new JList(contents);
contents.add("zhangsan");
contents.add("lisi");
contents.add("wangwu");
contentPane.add(jList);
setVisible(true);
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
- 应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
3.7、文本框
- 文本框
package com.ciian.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world", 20);
container.add(textField, BorderLayout.NORTH);
container.add(textField2, BorderLayout.SOUTH);
setVisible(true);
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
- 密码框
package com.ciian.lesson06;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = getContentPane();
//面板
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField);
setVisible(true);
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
- 文本域
package com.ciian.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container contentPane = getContentPane();
// 文本
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("学Java");
// Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
setVisible(true);
setBounds(100,100,300,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
贪吃蛇
帧,如果时间片足够小,就是动画,一秒30帧60帧。连起来是动画,拆开就是静态的图片!
- StartGame
package com.ciian.snake;
import javax.swing.*;
//游戏的主启动类
public class StartGame {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setBounds(10,10,915,750);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 正常游戏界面都或该在面上!
jFrame.add(new GamePanel());
jFrame.setVisible(true);
}
}
- Data
package com.ciian.snake;
import javax.swing.*;
import java.net.URL;
//数据中心
public class Data {
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 rightURL = Data.class.getResource("/statics/right.png");
public static URL leftURL = Data.class.getResource("/statics/left.png");
public static ImageIcon up = new ImageIcon(upURL);
public static ImageIcon down = new ImageIcon(downURL);
public static ImageIcon right = new ImageIcon(rightURL);
public static ImageIcon left = new ImageIcon(leftURL);
public static URL bodyURL = Data.class.getResource("/statics/body.png");
public static URL foodURL = Data.class.getResource("/statics/food.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static ImageIcon food = new ImageIcon(foodURL);
}
- GamePanel
package com.ciian.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 {
// 定义蛇的数据结构
int length;//蛇的长度
int[] snakeX = new int[600];//蛇的x坐标 25*34;
int[] snakeY = new int[500];//蛇的y坐标 25*24;
String fx;
int score;
// 食物的坐标
int foodX;
int foodY;
Random random = new Random();
// 游戏当前的状态:开始、停止
boolean isStart = false;
//游戏失败
boolean isFall = false;
//定时器以ms为单位1000ms=1s
Timer timer = new Timer(100, this);//100毫秒执行一次!
// 构造器
public GamePanel() {
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;//身体1的坐标
snakeX[2] = 50;
snakeY[2] = 100;//身体2的坐标
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);//头部广告栏
g.fillRect(25, 75, 850, 600);//默认游戏界面
//画积分
g.setColor(Color.white);
g.setFont(new Font("微软雅黑", Font.BOLD, 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.body.paintIcon(this, g, snakeX[i], snakeY[i]);//身体1的坐标
}
if (isStart == false) {
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
g.drawString("按下空格开始游戏", 300, 300);
}
if (isFall) {
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 (isFall) {
//重新开始
isFall = false;
init();
} else {
isStart = !isStart;
}
this.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";
}
}
//事件监听---需要通过固定事件米刷新,1s16次
@Override
public void actionPerformed(ActionEvent e) {
if (isStart && isFall == false) {
//吃食物
if (snakeX[0] == foodX && snakeY[0] == foodY) {
//长度+1
length++;
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 (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]) {
isFall = true;
}
}
this.repaint();//重画
}
timer.start();//定时器开启
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
总结
- GUI
- AWT
- 组件
- 容器
- 面板
- 事件监听
- 文本框监听
- 画图类paint(g)
- 鼠标监听
- 键盘监听
- 窗口监听
- Swing
- 容器
- 面板
- 额可以带滚动条
- label
- 普通标签
- 图片标签
- 图像标签
- 按钮
- 普通按钮
- 带图片按钮
- 单选框
- 复选框
- 列表
- 下拉框
- 列表框
- 文本框
- 普通文本
- 密码框
- 文本域
- 贪吃蛇
- AWT