自个记的GUI编程
- 这是什么
- 他怎么玩
- 该如何去在我们平时运用?
- 学这玩意儿的目的,为了巩固java基础并写出一些小玩意
大纲:
1.组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
- 还可以写容易进去的东西
2.简介
Gui
的核心技术:Swing AWT,
AWT是底层,丑不拉几的。Swing是封装的,有更多好看的
1.因为界面不美观
2.需要jre环境!
为什么我们要学习?
1.MVC的基础
2.可以写出自己心中想要的一些小工具
3.工作时候,也可能需要维护到swing界面,概率极小!
4.了解MVC架构,了解监听
3.AWT
3.1、AWT介绍
抽象的窗口工具
- 包含了很多类和接口!GUI:图形用户界面编程
Eclipse
:Java写的 - 元素:窗口,按钮,文本框
java.awt
3.2、组件和容器
1.Frame
public static void main(String[] args) {
//Frame,JDK,看源码!
Frame frame = new Frame("我的第一个Java图形界面窗口");//title
//需要设置可见性 withe height
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 Color
frame.setBackground(new Color(208, 196, 196));
//弹出的初始设置 //750 240
frame.setLocation(750,240);
//设置大小固定
frame.setResizable(false);
}
问题:窗口无法关闭
尝试回顾封装:
public static void main(String[] args) {
//展示多个窗口 new就完了
MyFrame myFrame = new MyFrame(750, 240, 400, 400, new Color(255, 255, 255));
MyFrame myFrame1 = new MyFrame(650, 240, 400, 400, Color.red);
MyFrame myFrame2 = new MyFrame(550, 240, 400, 400, new Color(255, 255, 255));
MyFrame myFrame3 = new MyFrame(450, 240, 400, 400, new Color(255, 255, 255));
}
class MyFrame extends Frame {
static int id= 0;
public MyFrame(int x, int y , int w , int h,Color color){
super("MyFrame"+(++id));
setBounds(x,y,w,h);
setVisible(true);
setBackground(color);
}
}
2.面板Panel
解决了关闭事件!
package com.Han.lesson01;
import javafx.scene.layout.Pane;
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(40,161,35));
//Panel设置坐标(相对坐标),相对于Frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(193,15,60));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件。监听窗口关闭事件 System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
3.3、布局管理器
流式布局:FlowLayout
public static void main(String[] args) {
MyFrame frame = new MyFrame(750,240,400,400,new Color(215, 196, 196));
frame.close(frame);
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
东南西北中:BorderLayout
public static void main(String[] args) {
MyFrame frame = new MyFrame(750,240,500,500,new Color(192,168,190));
frame.close(frame);
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);
}
表格布局:GridLayout
public static void main(String[] args) {
MyFrame frame = new MyFrame(750,240,500,500,new Color(192,168,168));
frame.close(frame);
Button bt1 = new Button("bt1");
Button bt2 = new Button("bt2");
Button bt3 = new Button("bt3");
Button bt4 = new Button("bt4");
Button bt5 = new Button("bt5");
Button bt6 = new Button("bt6");
frame.setLayout(new GridLayout(3,2));
frame.add(bt1);
frame.add(bt2);
frame.add(bt3);
frame.add(bt4);
frame.add(bt5);
frame.add(bt6);
// frame.pack();//java的方法:自动布局
}
监听实现窗口关闭
WindowListener
是一个接口,一般是要写实现类的用适配器模式:
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
小练习:
用窗口、面板做出下图样式
分析过程,代码实现
代码示例:
package com.Han.lesson01;
import java.awt.*;
public class Test {
public static void main(String[] args) {
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");
MyFrame frame = new MyFrame(750,240,500,500,new Color(192,168,168));
frame.close(frame);
frame.setLayout(new GridLayout(2,1));
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));
p1.add(button1, BorderLayout.WEST);//左
p2.add(button2);
p2.add(button3);
p1.add(button4,BorderLayout.EAST);//右
p1.add(p2,BorderLayout.CENTER);
p3.add(button5, BorderLayout.WEST);
p4.add(button6);
p4.add(button7);
p4.add(button8);
p4.add(button9);
p3.add(button10,BorderLayout.EAST);
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东南西北中
- 表格
- 大小,定位,背景颜色,可见性,监听
3.4、事件监听
事件监听:当某个事情发生的时候,干什么?
按钮设置监听:
button1.addActionListener(new 实现ActionListener接口的类);
package com.Han.Demo02;
import com.Han.Demo01.MyFrame;
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) {
//按下按钮,触发一些事件
MyFrame frame = new MyFrame();
Button button = new Button("Button");
button.addActionListener(new ActionListener() {//事件监听,ActionListener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("哈哈哈");
}
});
frame.close(frame);
frame.add(button);
frame.pack();
frame.setVisible(true);
}
}
//关闭窗口
// public static void close1(Frame frame){
// frame.addWindowListener(new WindowAdapter() {
// @Override
// public void windowClosing(WindowEvent e) {
// System.exit(0);
// }
// });
// }
3.4.1实现多个按钮共用一个监听事件,点击不同按钮触发不同事件
主要代码: button1.setActionCommand("start")//可以显示的定义触发会返回的命令,如果不显示定义则会走默认的值 button1.setActionCommand("stop")//设置 if(e.getActionCommand().equals("start")){ System.out.println("开始"); }else{ System.out.println("结束"); }
实现代码:
package com.Han.Demo02;
import com.Han.Demo01.MyFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//两个按钮 一个监听
public class TestAction {
public static void main(String[] args) {
MyFrame frame = new MyFrame(740,240,500,500,new Color(192,168,168));
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义则会走默认的值
//可以多个按钮只写一个监听
button1.setActionCommand("start");
button2.setActionCommand("stop");
MyMonitoy myMonitoy = new MyMonitoy();
button1.addActionListener(myMonitoy);
button2.addActionListener(myMonitoy);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.setVisible(true);
frame.close(frame);
}
}
class MyMonitoy implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand获取按钮的信息,需要事先设置setActionCommand
if(e.getActionCommand().equals("start")){
System.out.println("开始");
}else{
System.out.println("结束");
}
}
}
3.5、输入框TextField监听
package com.Han.Demo02;
import java.awt.*;
import java.awt.event.*;
public class TestText01 {
public static void main(String[] args) {
//启动
new Myframe();
}
}
class Myframe extends Frame{
public Myframe(){
TextField textField = new TextField();
add(textField);
MyActionlisenner myActionlisenner = new MyActionlisenner();
textField.addActionListener(myActionlisenner);
textField.setEchoChar('*');
setVisible(true);
setBounds(740,240,500,500);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionlisenner implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField)e.getSource();
System.out.println(textField.getText());
textField.setText("");
}
}
3.6、简易计算器,组合+内部类回顾复习(什么是组合)
面向过程代码如下:
package com.Han.Demo02;
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 calcwindow();
}
}
class calcwindow extends Frame {
public calcwindow(){
TextField text1 = new TextField(10);//字符数
TextField text2 = new TextField(10);//字符数
TextField text3 = new TextField(10);//字符数
Button button = new Button("=");
Label label = new Label("+");//标签
setLayout(new FlowLayout());//流式布局
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
button.addActionListener(new MyListener(text1,text2,text3));
}
}
class MyListener implements ActionListener {
private TextField num1,num2,num3;
public MyListener(TextField num1, TextField num2 , TextField num3){
this.num1=num1;
this.num2=num2;
this.num3=num3;
}
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(num1.getText());
int b = Integer.parseInt(num2.getText());
num3.setText(""+(a+b));
System.out.println(num3.getText());
num1.setText("");
num2.setText("");
}
}
优化一下下:改为面向对象的代码
package com.Han.Demo02;
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 calcwindow().loadFrame();
}
}
class calcwindow extends Frame {
TextField text1,text2,text3;
public void loadFrame(){
text1 = new TextField(10);
text2 = new TextField(10);
text3 = new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyListener(this));
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyListener implements ActionListener {
//获取计算器这个对象,在一个类中组合另一个类:
calcwindow clc = null;
public MyListener(calcwindow clc){
this.clc=clc;
}
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(clc.text1.getText());
int b = Integer.parseInt(clc.text2.getText());
clc.text3.setText(""+(a-b));
clc.text1.setText("");
clc.text2.setText("");
}
}
再优化一下:内部类方式!
内部类方式更好的包装
内部类最大的好处就是畅通无阻的访问外部的属性方法
package com.Han.Demo02;
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 calcwindow().loadFrame();
}
}
class calcwindow extends Frame {
TextField text1,text2,text3;
public void loadFrame(){
text1 = new TextField(10);
text2 = new TextField(10);
text3 = new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(text1.getText());
int b = Integer.parseInt(text2.getText());
text3.setText(""+(a+b));
}
});
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
3.7、画笔
package com.Han.Demo02;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setVisible(true);
setBounds(200,200,600,500);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@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);
//养成习惯,用完把画笔颜色还原到最初的颜色 小声bb:把设置颜色代码注释掉
}
}
3.8、鼠标监听
懵逼树上懵逼果,懵逼树下你和我
3.9、窗口监听
package com.Han.Demo03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowsFrame();
}
}
class WindowsFrame extends Frame{
public WindowsFrame(){
setBackground(Color.BLUE);
setBounds(100,100,200,200);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//正常退出
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowsFrame soure = (WindowsFrame) e.getSource();
soure.setTitle("被激活了");
System.out.println("windowActivated");
}
});
}
}
3.10、键盘监听
package com.Han.Demo03;
import java.awt.*;
import java.awt.event.*;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(100,200,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下事件
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键,当前的码
int keycode = e.getKeyCode();
System.out.println(keycode);
//不需要去记录这个值,直接使用静态属性 VK_XXX
if(keycode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
4.Swing
4.1窗口面板
JFrame:顶级窗口
Container:容器
尽量不要在容器上直接做,尽量用面板去做,然后容器添加面板
不设置容器,设置的所有东西不会生效,因为它们是在容器上的:Swing要有容器
创建容器:
Container cont = jFrame.getContentPane();
JFrame关闭事件:
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
4.2弹窗
JDialog,用来被弹出,默认就有关闭事件
package com.Han.Demo04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public static void main(String[] args) {
new DialogDemo();
}
public DialogDemo(){
this.setVisible(true);
this.setBounds(100,100,400,400);
//setDefaultCloseOperation设置默认值 ; WindowConstants窗口常量
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container cont = this.getContentPane();//获得内容页 容器:Container
//绝对布局
cont.setLayout(null);
//按钮
Button button = new Button("点击弹出对话框");
button.setBounds(120,20,150,20);
button.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
//把按钮添加上
cont.add(button);
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog(){
this.setVisible(true);
this.setBounds(200,200,200,200);
Container contentPane = this.getContentPane();
// contentPane.setLayout(null);注释掉绝对布局或者设置setBounds
Label label = new Label("兔老师带你学java");
label.setAlignment(Label.CENTER);
contentPane.add(label);
}
}
4.3标签
JLabel
标签居中:
label.setAlignment(Label.CENTER);
JLabel label = new JLabel("xxx")
4.3.1图标icon
它是一个接口,有三个方法,需要实现
正常应该写在弹窗里,继承弹窗、实现Icon
Icon的三个方法:
patinIcon:画一个图标
getIconWidth();获得宽
getIconHeight();获得高
添加图片到窗口标签上代码:
URL url = 当前类.class.getResource("");//获得当前类目录下的资源:例如:getResource(name="tx.png")
ImageIcon imageIcon = new ImageIcon(url);
JLabel label = new JLabel();
label.setIcon(imageIcon);//把图片设置到标签上
// contentPane.add(label);
将图片变为图标
URL url = 当前类.class.getResource("tx.png");
Icon icon = new ImageIcon(url);
package com.Han.Demo04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class MyImageIcon extends JFrame {
public MyImageIcon(){
//获取图片的地址,强行记住
//图片放标签里 1.new ImageIcon(可以放图片进去)
URL url = MyImageIcon.class.getResource("tx.png");
ImageIcon imageIcon = new ImageIcon(url);
JLabel label = new JLabel();//标签
label.setIcon(imageIcon);//把图片设置到标签上
label.setHorizontalAlignment(SwingConstants.CENTER);//居中显示
Container contentPane = getContentPane();
contentPane.add(label);
setVisible(true);
// setBounds(100,100,200,200);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyImageIcon();
}
}
4.4面板
面板也是一个Swing容器,他可以看作为容器容纳其他组件,但它也必须被添加到其他容器中
Swing中常用的面板包括
JPanel
面板和JScrollPane
面板
package com.Han.Demo05;
import javax.swing.*;
import java.awt.*;
/**
* Jpanel补充
*/
public class JpanelDemo extends JFrame {
public JpanelDemo(){
Container cont = this.getContentPane();//容器
cont.setLayout(new GridLayout(2,1,10,10));//后边两个参数是间距
JPanel jp1 = new JPanel(new GridLayout(1,3));//面板
JPanel jp2 = new JPanel(new GridLayout(1,2));//面板
JPanel jp3 = new JPanel(new GridLayout(2,1));//面板
JPanel jp4 = new JPanel(new GridLayout(3,2));//面板
jp1.add(new JButton("1"));
jp1.add(new JButton("1"));
jp1.add(new JButton("1"));
jp2.add(new JButton("2"));
jp2.add(new JButton("2"));
jp3.add(new JButton("3"));
jp3.add(new JButton("3"));
jp4.add(new JButton("4"));
jp4.add(new JButton("4"));
jp4.add(new JButton("4"));
jp4.add(new JButton("4"));
jp4.add(new JButton("4"));
jp4.add(new JButton("4"));
cont.add(jp1);
cont.add(jp2);
cont.add(jp3);
cont.add(jp4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JpanelDemo();
}
}
4.4.1JScroll
有滚动条的面板
JScrollPane
面板是带滚动条的面板,它也是一个面板,但是JScrollPane
只能放置一个组件,并且不可以使用布局管理器如果需要在
JScrollPane
面板中放置多个组件,需要将多个组件放置在JPanel
面板上,然后将JPanel
作为一个整体组件添加到JScrollPane
组件上。
package com.Han.Demo05;
import javax.swing.*;
import java.awt.*;
public class JscrollDemo extends JFrame {
public static void main(String[] args) {
new JscrollDemo();
}
public JscrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 10);
JScrollPane jScrollPane = new JScrollPane(textArea);
textArea.setText("欢迎来到兔子学Java");
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
4.5按钮
图片按钮
package com.Han.Demo05;
import javax.swing.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public static void main(String[] args) {
new JButtonDemo01();
}
public JButtonDemo01(){
URL url = JButtonDemo01.class.getResource("tx.png");
Icon icon = new ImageIcon(url);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
// this.setVisible(true);
// this.setBounds(100,100,700,700);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Container container = this.getContentPane();
// container.add(button)
new MyWindow().add(button);
}
}
public class MyWindow extends JFrame {
public MyWindow(){
// Container container = this.getContentPane();
this.setVisible(true);
this.setBounds(100,100,700,700);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void add(JButton jb){//给容器添加按钮
this.getContentPane().add(jb);
}
}
单选按钮
JRadioButton
package com.Han.Demo05;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo02 {
public static void main(String[] args) {
new JButtonDemo02();
}
public JButtonDemo02(){
//单选按钮
JRadioButton button1 = new JRadioButton("JRadioButton01");
JRadioButton button2 = new JRadioButton("JRadioButton02");
JRadioButton button3 = new JRadioButton("JRadioButton03");
//由于单选按钮一般只能选择一个,所以一般分个组,一个组中只能选择一个
//如果不分组,三个单选按钮都可以选,就跟多选按钮一样了
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
MyWindow myWindow = new MyWindow();//参考上块代码中的Mywindow
myWindow.add(button1, BorderLayout.NORTH);
myWindow.add(button2, BorderLayout.CENTER);
myWindow.add(button3, BorderLayout.SOUTH);
}
}
复选按钮
package com.Han.Demo05;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 {
public static void main(String[] args) {
new JButtonDemo03();
}
public JButtonDemo03(){
//多选按钮
JCheckBox button1 = new JCheckBox("JCheckBox1");
JCheckBox button2 = new JCheckBox("JCheckBox2");
MyWindow myWindow = new MyWindow();
myWindow.add(button1, BorderLayout.NORTH);
myWindow.add(button2, BorderLayout.CENTER);
}
}
4.6列表
下拉框
package com.Han.Demo06;
import javax.swing.*;
import java.awt.*;
public class TestComboBoxDemo01 extends JFrame {
public TestComboBoxDemo01(){
Container container = this.getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("烧烤兔");
jComboBox.addItem("麻辣兔");
jComboBox.addItem("红烧兔头");
container.add(jComboBox);
this.setVisible(true);
this.setBounds(700,200,500,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo01();
}
}
列表框
package com.Han.Demo06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboBoxDemo02 extends JFrame {
public TestComboBoxDemo02(){
Container container = this.getContentPane();
//生成列表的内容
// String [] str = {"1","2","3"};
Vector weiketuo = new Vector();
//列表中应该放入内容
JList jList = new JList(weiketuo);//str
// container.add(jList);
weiketuo.add("张三");
weiketuo.add("李四");
weiketuo.add("当然还有我小兔子");
container.add(jList);
this.setVisible(true);
this.setBounds(700,200,500,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo02();
}
}
- 应用场景
- 选择地区,或者一些单个选项(选项框只有两个选择用单选框按钮,大于2个用下拉框,节省内存)
- 列表:展示 一些信息,一般都是动态扩容的。
4.7文本框
-
文本框
package com.Han.Demo06; import javax.swing.*; import java.awt.*; public class TestTextDemo01 extends JFrame{ public TestTextDemo01(){ Container container = this.getContentPane(); container.setLayout(null); JTextField jTextField1 = new JTextField("Hello"); JTextField jTextField2 = new JTextField("World"); jTextField1.setBounds(30,20,200,30); jTextField2.setBounds(30,80,200,30); container.add(jTextField1); container.add(jTextField2); this.setVisible(true); this.setBounds(700,200,500,200); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo01(); } }
-
密码框
package com.Han.Demo06; import javax.swing.*; import java.awt.*; public class TestTextDemo02 extends JFrame { public TestTextDemo02(){ Container container = this.getContentPane(); container.setLayout(null); JPasswordField jpw1 = new JPasswordField(); // jpw1.setEchoChar('兔'); jpw1.setBounds(30,20,200,30); container.add(jpw1); this.setVisible(true); this.setBounds(700,200,500,200); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo02(); } }
-
文本域
和4.4.1一样的代码
这就不写了
C/S:客户端 --> 服务器
Java主流:B/S:浏览器 --> 服务器
贪吃蛇
帧,如果时间片足够小,就是动画。一秒30帧(30张图片)连起来是动画,拆开就是静态的图片
- 键盘监听
- 定时器Timer
- 话不多说直接开撕!