GUI编程
告诉大家该怎么学?
- 这是什么?
- 它怎么玩?
- 该如何去在我们平时运用?
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
1、简介
Gui的核心技术:Swing AWT
- 因为界面不美观
- 需要jre环境!
为什么我们要学习?
- 可以写出自己心中想要的一些小工具
- 工作的时候,也可能需要维护到swing界面,概率极小
- 了解MVC架构,了解监听!
2、AWT
2.1、Awt介绍
- 包含了很多类和接口!GUI
- 元素:窗口,按钮,文本框
- java.awt
2.2、组件和容器
1、Frame
package com.luffy.lesson01;
import java.awt.*;
/**
*
* Frame类初始
*
* */
public class TestFrame_01 {
public static void main(String[] args) {
//这里的构造函数传的String是用来设置Title的
Frame frame = new Frame("你好!我是路飞");
//设置Frame窗口的可见性
frame.setVisible(true);
//设置Frame窗口的大小
frame.setSize(400,400);
//设置Frame窗口在屏幕的位置
frame.setLocation(200,200);
//设置Frame窗口的背景颜色
frame.setBackground(new Color(123,2,213));
//让Frame窗口不可拉伸
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,停止java程序
尝试回顾封装
package com.luffy.lesson01;
import java.awt.*;
/**
*
* 封装Frame,MyFrame工具类
*
* */
public class TestFrame_02 {
public static void main(String[] args) {
MyFrame m1 = new MyFrame(100,100,200,200);
MyFrame m2 = new MyFrame(500,100,200,200);
MyFrame m3 = new MyFrame(100,500,200,200);
MyFrame m4 = new MyFrame(500,500,200,200);
}
}
class MyFrame extends Frame{
private static int count = 0;
public MyFrame(int x,int y,int w,int h){
super("这是第" + count + "个Frame窗口");
setBounds(x,y,w,h);
setVisible(true);
setBackground(new Color(88,25,12));
setResizable(false);
}
}
2、面板Panel
package com.luffy.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
*
* Panel初始(类似Layout)
*
* */
public class TestPanel_01 {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局(意思应该是先清空frame原有的布局)
frame.setLayout(null);
frame.setBounds(200,200,500,500);
frame.setBackground(new Color(56,23,6));
frame.setResizable(false);
panel.setBounds(50,50,300,300);
panel.setBackground(new Color(60,78,05));
//frame需要一个component,而panel的父父类就是component
frame.add(panel);
//给frame添加窗口监听事件,这里一般方法名需要什么就给他new什么
//但是这里new一个windowListener需要重写他的所有方法,所以这里使用他的适配器Adapter
frame.addWindowListener(new WindowAdapter() {
//这里只重写了他的窗口关闭事件
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
解决了关闭问题
2.3、布局管理器
-
流式布局
package com.luffy.lesson01; import java.awt.*; /** * * 流式布局 * * */ public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); frame.setBackground(new Color(200,103,156)); frame.setBounds(50,50,500,500); //给frame设置流式布局,效果为居中 frame.setLayout(new FlowLayout(FlowLayout.CENTER)); Button b1 = new Button("按钮1"); Button b2 = new Button("按钮2"); Button b3 = new Button("按钮3"); //给frame窗口添加三个按钮,按照流式布局摆放 frame.add(b1); frame.add(b2); frame.add(b3); frame.setVisible(true); } }
-
东西南北中
package com.luffy.lesson01; import java.awt.*; /** * * 东西南北中 * * */ public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame(); frame.setBounds(50,50,500,500); frame.setBackground(new Color(220,56,123)); 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); } }
-
表格布局
package com.luffy.lesson01; import java.awt.*; /** * * 网格布局 * * */ public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame(); frame.setBounds(50,50,500,500); frame.setBackground(new Color(66,26,26)); frame.setLayout(new GridLayout(3,2)); Button b1 = new Button("b1"); Button b2 = new Button("b2"); Button b3 = new Button("b3"); Button b4 = new Button("b4"); Button b5 = new Button("b5"); frame.add(b1); frame.add(b2); frame.add(b3); frame.add(b4); frame.add(b5); //会让表格布局以好看的形式布置 frame.pack();//java函数! frame.setVisible(true); } }
课堂练习
代码实现
package com.luffy.lesson01; import java.awt.*; /** * * Frame案例练习 * * */ public class ExDemo { public static void main(String[] args) { //整个窗口 Frame frame = new Frame(); frame.setLayout(new GridLayout(2,1)); frame.setBounds(300,400,400,300); frame.setBackground(Color.BLACK); //上半层 Panel p1 = new Panel(); p1.setLayout(new BorderLayout()); //上半层要的一个中间面板 Panel p3 = new Panel(); p3.setLayout(new GridLayout(2,1)); //给上半层的中间面板添加两个按钮 p3.add(new Button("p2-btn-1")); p3.add(new Button("p2-btn-2")); //给上半层添加它要的三个东西 p1.add(new Button("West-1"), BorderLayout.WEST); p1.add(new Button("East-1"), BorderLayout.EAST); p1.add(p3,BorderLayout.CENTER); //下半层 Panel p2 = new Panel(); p2.setLayout(new BorderLayout()); //下半层要的一个中间面板 Panel p4 = new Panel(); p4.setLayout(new GridLayout(2,2)); //给下半层的中间面板添加四个按钮 for(int i = 1;i < 5; i++){ p4.add(new Button("b" + i)); } //给下半层添加它要的三个东西 p2.add(new Button("East-2"),BorderLayout.EAST); p2.add(new Button("West-2"),BorderLayout.WEST); p2.add(p4,BorderLayout.CENTER); //将两个大面板加入窗口 frame.add(p1); frame.add(p2); //窗口可见 frame.setVisible(true); } }
总结:
-
Frame是一个*窗口
-
Panel无法单独显示,必须添加到某个容器中
-
布局管理器
- 流式
- 东西南北中
- 表格
-
大小,定位,背景颜色,可见性,监听
2.4、事件监听
事件监听:当某个事情发生的时候,干什么?
package com.luffy.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("button");
//给按钮添加一个自定义的行动监听
button.addActionListener(new MyActionListener());
//关闭窗口监听的方法
windowClose(frame);
//设置窗口的属性
frame.setVisible(true);
frame.add(button,BorderLayout.CENTER);
frame.setBounds(50,50,500,500);
}
public static void windowClose(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) {
//getActionCommand默认传过来的是发送方的默认信息
System.out.println(e.getActionCommand());
}
}
多个按钮,共享一个事件
package com.luffy.lesson02;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("button1"); Button button2 = new Button("button2"); //给两个按钮添加同一个事件 button1.addActionListener(new MyMonitor()); button2.addActionListener(new MyMonitor()); //设置窗口的属性 frame.setVisible(true); frame.add(button1,BorderLayout.WEST); frame.add(button2,BorderLayout.EAST); frame.setBounds(50,50,500,500); }}class MyMonitor implements ActionListener { //根据不同按钮实现不同事件 @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("button1")){ System.out.println(e.getActionCommand()); }else if(e.getActionCommand().equals("button2")){ System.out.println(e.getActionCommand()); } }}
2.5、输入框TextField监听
package com.luffy.lesson02;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(); //按下Enter就触发事件 textField.addActionListener(new MyActionListener2()); //给输入的信息每个都设置成*,替换编码 textField.setEchoChar('*'); //将文本框添加到窗口中 add(textField); //设置窗口属性 setVisible(true); setBounds(50,50,500,500); }}class MyActionListener2 implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //获得一些资源,返回一个对象 TextField textField = (TextField) e.getSource(); System.out.println(textField.getText()); //每次回车之后都清空文本框 textField.setText(""); }}
2.6、简易计算器,组合+内部类回顾复习
package com.luffy.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 TestCalc { public static void main(String[] args) { new Calculator().loadFrame(); }}//计算器class Calculator extends Frame{ private TextField num1; private TextField num2; private TextField num3; public void loadFrame(){ //调用关闭窗口的监听方法 windowClose(this); //设置布局 setLayout(new FlowLayout()); //创建需要的组件 num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(20); Label label = new Label("+");//符号 Button button = new Button("=");//等于按钮 add(num1); add(label); add(num2); add(button); add(num3); //给等于按钮设置监听 button.addActionListener(new MyCalculatorListener()); //设置窗口属性 setBounds(50,50,700,200); setVisible(true); } //关闭窗口 public static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } //监听器 private class MyCalculatorListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { int t1 = Integer.parseInt(num1.getText()); int t2 = Integer.parseInt(num2.getText()); num3.setText("" + (t1 + t2)); //先清空再赋值 num1.setText(""); num2.setText(""); } }}
2.7、画笔
package com.luffy.lesson03;import java.awt.*;/** * * 画笔paint初始 * * */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.drawOval(100,100,100,100);//空心的圆 g.setColor(new Color(30,56,60)); g.fillOval(100,100,100,100);//实心的圆 g.setColor(Color.GREEN); g.fillRect(150,200,200,200); }}
2.8、鼠标监听
package com.luffy.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{ //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点 private final ArrayList<Point> points; public MyFrame(String title){ super(title); //给窗口添加鼠标事件 addMouseListener(new MyMouseListener()); points = new ArrayList<>(); //设置窗口的属性 setBounds(200,200,400,300); setVisible(true); } @Override public void paint(Graphics g) { //iterator是迭代器是意思 Iterator<Point> iterator = points.iterator(); while(iterator.hasNext()){ Point point = iterator.next(); g.setColor(Color.black); g.fillOval(point.x,point.y,10,10); } } //添加一个点到界面上 public void addPaint(Point point){ points.add(point); } class MyMouseListener extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { Frame frame = (Frame) e.getSource(); //每点击鼠标一次就往集合里加一个点,然后用画笔(paint)画出来 addPaint(new Point(e.getX(),e.getY())); //每次点击鼠标都需要重新画一遍 frame.repaint(); } }}
2.9、窗口监听
package com.luffy.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(){ //设置Frame窗口属性 setVisible(true); setBounds(100,100,200,200); setBackground(Color.black); setResizable(true); //给窗口添加监听事件 addWindowListener(new WindowAdapter() {//匿名内部类 //关闭窗口事件 @Override public void windowClosing(WindowEvent e) { System.out.println("窗口被关闭"); setVisible(false);//隐藏窗口 System.exit(0);//强制关闭窗口 } //激活窗口事件 @Override public void windowActivated(WindowEvent e) { setTitle("哟,回来啦"); System.out.println("窗口被激活"); } }); }}
2.10、键盘监听
package com.luffy.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(){ setVisible(true); setBounds(1,2,300,400); //键盘监听事件 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("你点击了上键"); } } }); }
3、Swing
3.1、窗口、面板
package com.luffy.lesson04;import javax.swing.*;import java.awt.*;/** * * Swing初始 * * */public class JFrameDemo { public static void main(String[] args) { //建立一个窗口 new JFrameDemo().init(); } //初始化 public void init(){ //*窗口 JFrame jFrame = new JFrame("这是一个JFrame窗口"); jFrame.setVisible(true); jFrame.setBounds(100,100,200,200); //获取窗口资源修改背景颜色 Container contentPane = jFrame.getContentPane(); contentPane.setBackground(Color.BLUE); //创建居中文字 JLabel jLabel = new JLabel(); jLabel.setText("我是居中文字"); jLabel.setHorizontalAlignment(SwingConstants.CENTER); //将文字添加到窗口 jFrame.add(jLabel); //关闭窗口(结束) jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }}
3.2、弹窗
package com.luffy.lesson04;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/** * * Swing弹窗 * * */public class DigLogDemo extends JFrame{ public DigLogDemo(){ //设置窗口属性 setVisible(true); setBounds(200,200,600,600); //创建一个按钮 JButton jButton = new JButton("点我弹出一个窗口"); jButton.setBounds(30,30,200,50); jButton.addActionListener(new ActionListener() { //按钮按下弹出窗口 @Override public void actionPerformed(ActionEvent e) { new MyDialogDemo(); } }); //给窗口内容设置属性 Container contentPane = this.getContentPane(); contentPane.setBackground(Color.green); contentPane.add(jButton); contentPane.setLayout(null);//绝对布局 } public static void main(String[] args) { new DigLogDemo(); }}//弹出的窗口class MyDialogDemo extends JDialog { public MyDialogDemo(){ setVisible(true); setBounds(100,100,500,500);// this.setDefaultCloseOperation(EXIT_ON_CLOSE);//好像在Swing里默认就可以关闭窗口 //获取当前窗口内容 Container contentPane = this.getContentPane();// contentPane.setLayout(null);//设置绝对布局 //在JFrame里和JDialog里用标签也得用JLabel contentPane.add(new JLabel("我是海贼王路飞")); }}
3.3、标签
label
new JLabel("×××")
图标和Icon
package com.luffy.lesson04;import javax.swing.*;import java.awt.*;/** * * 图标和标签的绑定显示 * * */public class IconDemo extends JFrame implements Icon{ private int wight; private int height; public static void main(String[] args) { new IconDemo().init(); } public void init(){ //设置窗口属性 this.setVisible(true); this.setBounds(200,200,600,600); this.setBackground(Color.WHITE); //一个图标 IconDemo iconDemo = new IconDemo(15,15); //将图标和字符绑定,使它成为一个标签,设置居中 JLabel jLabel = new JLabel("JLabel",iconDemo,SwingConstants.CENTER); this.add(jLabel);// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public IconDemo(){} public IconDemo(int wight,int height){ this.wight = wight; this.height = height; } //画一个图标在哪 @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillOval(x,y,wight,height); } //图标的宽 @Override public int getIconWidth() { return wight; } //图标的高 @Override public int getIconHeight() { return height; }}
图片和Icon
package com.luffy.lesson04;import javax.swing.*;import java.awt.*;import java.net.URL;/** * * 图片和标签的绑定显示 * * */public class ImageIconDemo extends JFrame { public ImageIconDemo(){ //获取调用class的那个类同等路径下的资源 URL url = ImageIconDemo.class.getResource("luffy.jpg"); //将获取到的资源转成图片图标格式 ImageIcon imageIcon = new ImageIcon(url); //创建一个标签 JLabel jLabel = new JLabel("这是路飞"); //将标签和图片绑定,并设置他们的对齐方式 jLabel.setIcon(imageIcon); jLabel.setHorizontalAlignment(SwingConstants.CENTER); //获取当前窗口内容,将绑定好的标签添加到内容 Container contentPane = getContentPane(); contentPane.add(jLabel); //给窗口设置属性 this.setVisible(true); this.setBounds(100,100,200,200); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { //启动! new ImageIconDemo(); }}
3.4、面板
JPanel
package com.luffy.lesson05;import javax.swing.*;import java.awt.*;/** * * JPanel的使用 * * */public class JPanelDemo extends JFrame { public JPanelDemo(){ Container contentPane = this.getContentPane(); //设置主页面的布局 contentPane.setLayout(new GridLayout(2,2,10,10));//10是网格布局的间距 //创建四个面板放入主页面 JPanel jPanel1 = new JPanel(new GridLayout(1,2)); JPanel jPanel2 = new JPanel(new GridLayout(2,2)); JPanel jPanel3 = new JPanel(new GridLayout(2,3)); JPanel jPanel4 = new JPanel(new GridLayout(3,1)); //给不同面板添加对应的按钮 jPanel1.add(new Button("1")); jPanel1.add(new Button("1")); jPanel2.add(new Button("2")); jPanel2.add(new Button("2")); jPanel2.add(new Button("2")); jPanel2.add(new Button("2")); jPanel3.add(new Button("3")); jPanel3.add(new Button("3")); jPanel3.add(new Button("3")); jPanel3.add(new Button("3")); jPanel3.add(new Button("3")); jPanel3.add(new Button("3")); jPanel4.add(new Button("4")); jPanel4.add(new Button("4")); jPanel4.add(new Button("4")); //将四个面板添加到主页面 contentPane.add(jPanel1); contentPane.add(jPanel2); contentPane.add(jPanel3); contentPane.add(jPanel4); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new JPanelDemo(); }}
JScrollPanel(滚动条)
package com.luffy.lesson05;import javax.swing.*;import java.awt.*;/** * * JScroll的使用 * * */public class JScrollDemo extends JFrame { public JScrollDemo(){ Container contentPane = this.getContentPane(); TextArea textArea = new TextArea(20,50);//文本的行列大小 textArea.setText("我是路飞"); //将创建的文本放入JScroll滚动条中 JScrollPane jScrollPane = new JScrollPane(textArea); contentPane.add(jScrollPane); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setSize(500,500); } public static void main(String[] args) { new JScrollDemo(); }}
3.5、按钮
-
图片按钮
package com.luffy.lesson05;import javax.swing.*;import java.awt.*;import java.net.URL;/** * * JButton的使用 * * */public class JButtonDemo01 extends JFrame { public JButtonDemo01(){ Container contentPane = this.getContentPane(); //获取图片路径 URL resource = JButtonDemo01.class.getResource("luffy.jpg"); //将图片和标签融合转为一个标签 Icon icon = new ImageIcon(resource); //将标签和按钮融合 JButton jButton = new JButton(icon); jButton.setToolTipText("路飞图片");//给按钮设置鼠标提示信息 //将融合好的标签添加到页面内容中 contentPane.add(jButton); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo01(); }}
-
单选按钮
package com.luffy.lesson05;import javax.swing.*;import java.awt.*;/** * * JRadioButton的使用(单选框) * * */public class JButtonDemo02 extends JFrame{ public JButtonDemo02(){ Container contentPane = this.getContentPane(); //创建三个单选框 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.NORTH); contentPane.add(jRadioButton2,BorderLayout.SOUTH); contentPane.add(jRadioButton3,BorderLayout.CENTER); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo02(); }}
-
复选按钮
package com.luffy.lesson05;import javax.swing.*;import java.awt.*;/** * * JCheckBox的使用(复选框) * * */public class JButtonDemo03 extends JFrame{ public JButtonDemo03(){ Container contentPane = this.getContentPane(); //创建三个复选框 JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1"); JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2"); JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3"); //将复选框添加到页面内容 contentPane.add(jCheckBox1,BorderLayout.NORTH); contentPane.add(jCheckBox2,BorderLayout.SOUTH); contentPane.add(jCheckBox3,BorderLayout.CENTER); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo03(); }}
3.6、列表
下拉框
package com.luffy.lesson05;import javax.swing.*;import java.awt.*;/** * * ComboBox的使用(下拉框) * * */public class TestComboBoxDemo01 extends JFrame{ public TestComboBoxDemo01(){ Container contentPane = this.getContentPane(); //创建一个下拉框 JComboBox<String> jComboBox = new JComboBox<>(); //给下拉框设置下拉属性 jComboBox.addItem(null); jComboBox.addItem("正在热映"); jComboBox.addItem("已下架"); jComboBox.addItem("即将上映"); //将下拉框添加到页面内容 contentPane.add(jComboBox); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboBoxDemo01(); }}
列表框
package com.luffy.lesson05;import javax.swing.*;import java.awt.*;import java.util.Vector;/** * * JList(列表) * */public class TestComboBoxDemo02 extends JFrame{ public TestComboBoxDemo02(){ Container contentPane = this.getContentPane(); //添加三个列表值// String[] contents = {"路飞","索隆","山治"}; Vector<String> contents = new Vector<>(); //将列表值添加到JList列表中 JList<String> jList = new JList<>(contents); //Vector是补充知识,这里指的是动态添加,数据结构上面放的是一个引用,所以可以后续添加 contents.add("我是Vector1"); contents.add("我是Vector2"); contents.add("我是Vector3"); //将JList列表添加到页面内容 contentPane.add(jList); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboBoxDemo02(); }}
- 应用场景
- 下拉框,选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容!
3.7、文本框
文本框
package com.luffy.lesson06;import javax.swing.*;import java.awt.*;/** * * JTextField的使用(文本框) * * */public class TestTextDemo01 extends JFrame{ public TestTextDemo01(){ Container contentPane = this.getContentPane(); //创建两个文本框 JTextField jTextField1 = new JTextField("我是文本框1"); JTextField jTextField2 = new JTextField("我是文本框2",20); //将文本框添加到页面内容 contentPane.add(jTextField1,BorderLayout.SOUTH); contentPane.add(jTextField2,BorderLayout.NORTH); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo01(); }}
密码框
package com.luffy.lesson06;import javax.swing.*;import java.awt.*;/** * * JPasswordField的使用(密码框) * * */public class TestTextDemo02 extends JFrame { public TestTextDemo02(){ Container contentPane = this.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 TestTextDemo02(); }}
文本域
package com.luffy.lesson05;import javax.swing.*;import java.awt.*;/** * * JScroll的使用(滚动条)和TextArea(文本域) * * */public class JScrollDemo extends JFrame { public JScrollDemo(){ Container contentPane = this.getContentPane(); TextArea textArea = new TextArea(20,50);//文本的行列大小 textArea.setText("我是路飞"); //将创建的文本放入JScroll滚动条中 JScrollPane jScrollPane = new JScrollPane(textArea); contentPane.add(jScrollPane); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setSize(500,500); } public static void main(String[] args) { new JScrollDemo(); }}
贪吃蛇
帧,如果时间片足够小,就是动画,一秒30帧 60帧。连起来就是动画,拆开就是静态的图片!
键盘监听
定时器 Timer