组件java
窗口数据结构
架构
面板dom
文本框ide
列表框函数
按钮工具
图片布局
监听事件测试
鼠标优化
键盘事
破解工具
GUI的核心技术:Swing AWT 界面不美观 须要jre环境
为了了解MVC架构 了解监听。
包含不少类和接口
元素:窗口,按钮,文本框
java.awt
是一个顶级窗口
import java.awt.*;
//GUI第一个界面
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("个人第一个界面");
//须要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(22,44,66));
//设置窗口初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
尝试回顾封装
import java.awt.*;
public class TestFrame1 {
public static void main(String[] args) {
MyFrame m1 = new MyFrame(100,100,200,200,Color.BLACK);
MyFrame m2 = new MyFrame(300,100,200,200,Color.red);
MyFrame m3 = new MyFrame(100,300,200,200,Color.yellow);
MyFrame m4 = new MyFrame(300,300,200,200,Color.BLUE);
}
}
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);
}
}
Panel没法单独显示,必须添加到某个容器中
解决了关闭事件
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(400,400,500,500);
frame.setBackground(new Color(13, 24, 56));
//panel设置坐标,至关于frame
panel.setBounds(100,100,300,300);
panel.setBackground(new Color(144, 11, 44));
//frame.add();
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0);
//适配器模式
frame.addWindowListener(new WindowAdapter() {
流式布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestButton {
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.setVisible(true);
frame.setBounds(200,200,400,400);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
东西南北中
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();
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(400,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
表格布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
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);
frame.addWindowListener(new WindowAdapter() {
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(200,200);
frame.setBackground(new Color(1,4,6));
frame.setLayout(new GridLayout(2,1));
//new四个面板
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(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() {
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 ac = new MyActionListener();
button.addActionListener(ac);
windowClose(frame);//关闭窗口
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
//关闭窗体事件
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
多个按钮实现同一监听事件
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;public class TestActionEvent2 {
public static void main(String[] args) {
//两个按钮实现同一监听
Frame frame = new Frame("开始-中止");
Button b1 = new Button("start");
Button b2 = new Button("stop");}
MyActionListener1 ac = new MyActionListener1(); b1.addActionListener(ac); b2.addActionListener(ac); //能够显示的定义触发会返回的命令,不显示走默认的值 //多个按钮使用一个监听类 b2.setActionCommand("b2-->stop"); frame.add(b1,BorderLayout.NORTH); frame.add(b2,BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); }
class MyActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()得到按钮信息
System.out.println("按钮被点击了-->"+e.getActionCommand());
}
}
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);
//监听这个文本框输入的文字
MyActionListener03 mal = new MyActionListener03();
//按下enter,就会触发文本框事件
textField.addActionListener(mal);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener03 implements ActionListener{
优化前
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new MyCalculator();
}
}
//计算器类
class MyCalculator extends Frame{
public MyCalculator() {
//3个文本框
TextField text1 = new TextField(10);
TextField text2 = new TextField(10);
TextField text3 = new TextField(20);
//1个按钮
Button button = new Button("=");
//点击事件
button.addActionListener(new MyCalculatorListener(text1,text2,text3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
}
//监听事件类
class MyCalculatorListener implements ActionListener{
private TextField text1,text2,text3;
//获取三个变量
public MyCalculatorListener(TextField text1, TextField text2, TextField text3) {
this.text1 = text1;
this.text2 = text2;
this.text3 = text3;
}
优化为面向对象
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
MyCalculator myCalculator = new MyCalculator();
myCalculator.loadFrame();
}
}
//计算器类
class MyCalculator extends Frame{
//属性
TextField text1,text2,text3;
//方法
public void loadFrame(){
//3个文本框
text1 = new TextField(10);//字符数
text2 = new TextField(10);
text3 = new TextField(20);
//1个按钮
Button button = new Button("=");
//1个标签
Label label = new Label("+");
//点击事件
button.addActionListener(new MyCalculatorListener(this));
//布局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
}
//监听事件类
class MyCalculatorListener implements ActionListener{
//获取计算器这个对象,在一个类中组合另外一个类
MyCalculator calculator=null;
public MyCalculatorListener(MyCalculator calculator) {
this.calculator=calculator;
}
内部类好处是能够更好的访问外部类的方法和属性
更好的包装
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
MyCalculator myCalculator = new MyCalculator();
myCalculator.loadFrame();
}
}
//计算器类
class MyCalculator extends Frame{
//属性
TextField text1,text2,text3;
//方法
public void loadFrame(){
//3个文本框
text1 = new TextField(10);//字符数
text2 = new TextField(10);
text3 = new TextField(20);
//1个按钮
Button button = new Button("=");
//1个标签
Label label = new Label("+");
//点击事件
button.addActionListener(new MyCalculatorListener());
//布局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
//监听事件类
//内部类
private class MyCalculatorListener implements ActionListener{
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,300,400,500);
setVisible(true);
}
//画笔
实现画点
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 points;
public MyFrame(String title) {
super(title);
setBounds(200,200,300,500);
//存鼠标点击的点
points=new ArrayList<>();
//鼠标监听器,是针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new MyWindow();
}
}
class MyWindow extends Frame{
public MyWindow() {
setVisible(true);
setBounds(100,200,300,400);
setBackground(Color.BLUE);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
});
}
}
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,100,200);
setVisible(true);
addKeyListener(new KeyAdapter() {
import javax.swing.*;
import java.awt.*;
public class TestJFrame {
//init()初始化
public void init(){
JFrame jframe = new JFrame("第一个JFrame窗口");
jframe.setVisible(true);
jframe.setBackground(Color.BLUE);
jframe.setBounds(100,200,200,200);
//设置文字JLabel
JLabel jLabel = new JLabel("欢迎光临");
jframe.add(jLabel);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
//创建一个窗口
new TestJFrame().init();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class TestDialog extends JFrame {
public TestDialog() {
this.setVisible(true);
this.setBounds(200,300,400,500);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");//建立
button.setBounds(30,20,100,50);
//点击按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
图标Icon
import javax.swing.*;
import java.awt.*;
//图标,须要实现类,Frame继承
public class TestIcon extends JFrame implements Icon {
private int height;
private int width;
public TestIcon(){//无参构造
}
public TestIcon(int height,int width){//有参构造
this.height=height;
this.width=width;
}
public void init(){
TestIcon testIcon = new TestIcon(100,100);
//图标放在标签上,也能够放在按钮上
JLabel label = new JLabel("testIcon", testIcon, SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
图片Image
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame{
public ImageIconDemo() {
//获取图片的地址
JLabel label = new JLabel("图片");
URL url = ImageIconDemo.class.getResource("1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100,200,50,50);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
JPanel
import javax.swing.*;
import java.awt.*;
public class TestJPanel extends JFrame {
public TestJPanel() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面参数意思是间距
JPanel jPanel = new JPanel(new GridLayout(2,2,5,5));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
container.add(jPanel);
this.setVisible(true);
this.setSize(200,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPanel();
}
}
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll() {
TextArea textArea = new TextArea("欢迎光临");//文本域
Container container = this.getContentPane();
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,150,250);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
图片按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJButton extends JFrame {
public TestJButton() {
Container container = this.getContentPane();
//将图片变为一个图标
URL resource = TestJButton.class.getResource("1.jpg");
Icon icon = new ImageIcon(resource);
//把图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");
container.add(jButton);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton();
}
}
单选按钮
import javax.swing.*;
import java.awt.*;
public class TestJButton1 extends JFrame {
public TestJButton1() {
Container container = this.getContentPane();
//单选框
JRadioButton radioButton1 = new JRadioButton("radioButton1");
JRadioButton radioButton2 = new JRadioButton("radioButton2");
JRadioButton radioButton3 = new JRadioButton("radioButton3");
//因为单选框只能选一个,分组
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.NORTH);
container.add(radioButton2,BorderLayout.CENTER);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton1();
}
}
复选按钮
import javax.swing.*;
import java.awt.*;
public class TestJButton2 extends JFrame {
public TestJButton2() {
Container container = this.getContentPane();
//复选框
Checkbox checkbox1 = new Checkbox("checkbox1");
Checkbox checkbox2 = new Checkbox("checkbox1");
container.add(checkbox1,BorderLayout.NORTH);
container.add(checkbox2,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton2();
}
}
下拉框
import javax.swing.*;
import java.awt.*;
public class TestComboBox extends JFrame {
public TestComboBox() {
Container container = this.getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem(null);
comboBox.addItem("猴子");
comboBox.addItem("大象");
comboBox.addItem("长颈鹿");
container.add(comboBox);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBox();
}
}
列表框
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboBox1 extends JFrame {
public TestComboBox1() {
Container container = this.getContentPane();
//生成列表的内容
//String [] strings = {"1","2","3"};
//列表放入的内容
Vector vector = new Vector();
JList jList = new JList(vector);
vector.add("yier");
vector.add("sas");
container.add(jList);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBox1();
}
}
应用场景:选择地区,或者一些单选项
列表:展现信息,通常是动态扩容。
文本框
import javax.swing.*;
import java.awt.*;
public class Test01 extends JFrame {
public Test01() {
Container container = this.getContentPane();
TextField textField = new TextField("heheh",10);
container.add(textField);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Test01();
}
}
密码框
import javax.swing.*;
import java.awt.*;
public class Test01 extends JFrame {
public Test01() {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Test01();
}
}
文本域
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll() {
TextArea textArea = new TextArea("欢迎光临");//文本域
Container container = this.getContentPane();
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,150,250);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
帧 30,60帧,拆开细分就是一些静态得图片
须要知识;键盘监听,定时器Timer
import javax.swing.*;
//游戏主启动类
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(100,100,900,730);
frame.setResizable(false);//窗口大小不可变
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//正常游戏面板都在这
frame.add(new GamePanel());
}
}
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*25
int[] snakeY = new int[500];//蛇的Y坐标 25*25
String fx;
//食物的坐标
int foodx;
int foody;
int score;//fens
Random random= new Random();
//游戏当前的状态,中止-->开始
boolean isStart = false;//默认中止
boolean isFail = false;//游戏失败状态
//定时器 以毫秒为单位 1000ms=1s
Timer timer = new Timer(100,this);//100毫秒执行一次
//构造器
public GamePanel() {
init();
//得到焦点和键盘事件
this.setFocusable(true);//得到焦点事件
this.addKeyListener(this);//得到键盘监听事件
}
//初始化方法
public void init(){
length=3;
snakeX[0] =100;snakeY[0]= 100;//头部的坐标
snakeX[1] =75;snakeY[1] = 100;//第一个身体坐标
snakeX[2] =50;snakeY[2] = 100;//第二个身体坐标
fx="R";//初始化方向向右
//把食物随机分配在界面上
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
timer.start();//游戏一开始定时器就启动
score=0;
}
//绘制面板,咱们游戏中因此得东西都是用这个画笔画