----基础

// 创建一个窗体对象
        JFrame frame = new JFrame();
        // 设置窗口大小
        frame.setSize(300, 200);
        // 指定窗口的显示位置
        frame.setLocation(300, 200);
        // 指定窗口的标题栏
        frame.setTitle("MY FRIST WINDOWS!");

        // 获取窗口的内容面板
        Container c = frame.getContentPane();

        // 设置内容面板的背景色
        Color color = new Color(10, 30, 20);
        c.setBackground(color);
        // 显示窗口
        frame.setVisible(true);

        // 设置当关闭窗口是同时结束程序。
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 创建组件
        JButton btn = new JButton("按钮1");

// 将组件添加到窗体中。。。
        this.add(btn, BorderLayout.NORTH);
        this.add(btn1, BorderLayout.WEST);
        //this.add(btn2, BorderLayout.SOUTH);
        //this.add(btn3, BorderLayout.EAST);
        this.add(btn4, BorderLayout.CENTER);

        this.setSize(300, 400);

        this.setVisible(true);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 设置布局方式(流式布局)
        this.setLayout(new FlowLayout(FlowLayout.LEADING));

this.setLayout(new GridLayout(4, 4));

-----小应用

package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class Test07 extends JFrame {

    JTextField txtNum;
    JLabel label;
    JButton btn1, btn2, btn3;
    JPanel panel;

    int num = 0;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Test07 frame = new Test07();

    }

    private void InitControl() {

        txtNum = new JTextField(20);
        label = new JLabel("请随机输入一个数(1-100)");
        btn1 = new JButton("猜数字。。。");
        btn1.addActionListener(new BtnClick());

        btn2 = new JButton("重新玩。。。");
        btn3 = new JButton("退出。。。");

        panel = new JPanel();

        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);

        this.add(txtNum, BorderLayout.NORTH);
        this.add(label, BorderLayout.CENTER);
        this.add(panel, BorderLayout.SOUTH);

    }

    public Test07() {

        InitControl();

        // 产生一个随机数。。。
        Random random = new Random();
        // 产生一个随机数。。。
        num = random.nextInt();

        this.setSize(300, 200);
        this.setVisible(true);
        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public class BtnClick implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            // 区分点击那个按钮,获取事件源。。。
            JButton btnclick = (JButton) e.getSource();

            // 获取按钮上的文字
            String text = btnclick.getText();

            if (text.equals("猜数字。。。")) {

                int input = Integer.valueOf(txtNum.getText());

                if (input == num) {

                    label.setText("恭喜你!表演节目!!!");
                } else if (input < num) {

                    label.setText("猜小了!!!");
                } else if (input > num) {

                    label.setText("猜大了!!!");
                } else if (text.equals("重新玩。。。")) {

                    // 产生一个随机数。。。
                    Random random = new Random();
                    // 产生一个随机数。。。
                    num = random.nextInt() + 1;
                } else {
                    System.exit(0);
                }
            }

        }

    }

}

package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Test08 extends JFrame {

    JButton[][] btns = new JButton[10][10];

    // 默认色
    Color defaultcolor = null;
    JButton btn = new JButton();

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Test08 frame = new Test08();

    }

    // 初始化窗体中所需的组件
    private void InitControl() {

        for (int i = 0; i < btns.length; i++) {
            for (int j = 0; j < btns[i].length; j++) {
                JButton btn = new JButton();
                // 给按钮添加所在的行和列的信息,存入command中。。。
                btn.setActionCommand(i + "," + j);
                // 注册按钮的单击事件
                btn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // 获取单击按钮
                        JButton btnclick = (JButton) e.getSource();
                        // 获取按钮的命令参数(就是这个按钮在矩阵中的行号和列号)
                        String command = btnclick.getActionCommand();
                        // 使用字符分割得到按钮的行号和列号
                        String[] strs = command.split(",");
                        // 获取整形的行号和列号
                        int row = Integer.parseInt(strs[0]);
                        int col = Integer.parseInt(strs[1]);

                        // 声明上下左右的按钮
                        JButton topbtn = null;
                        JButton downbtn = null;
                        JButton leftbtn = null;
                        JButton rigthbtn = null;

                        // 获取上方按钮
                        if (row - 1 >= 0) {
                            topbtn = btns[row - 1][col];

                        }
                        // 获取左边的按钮
                        if (col - 1 >= 0) {
                            leftbtn = btns[row][col - 1];

                        }

                        // 获取下方的按钮
                        if (row + 1 < btns.length) {
                            downbtn = btns[row + 1][col];

                        }
                        // 获取右边的按钮
                        if (col + 1 < btns.length) {
                            rigthbtn = btns[row][col + 1];
                        }

                        JButton[] arrbtns = new JButton[5];
                        arrbtns[0] = btnclick;
                        arrbtns[1] = topbtn;
                        arrbtns[2] = downbtn;
                        arrbtns[3] = leftbtn;
                        arrbtns[4] = rigthbtn;

                        changColor(arrbtns);
                        /*
                         * btnclick.setBackground(Color.red);
                         * topbtn.setBackground(Color.red);
                         * downbtn.setBackground(Color.red);
                         * leftbtn.setBackground(Color.red);
                         * rigthbtn.setBackground(Color.red);
                         */
                    }
                });

                btns[i][j] = btn;
                this.add(btn);
            }
        }

    }

    // 改变按钮和周围颜色
    private void changColor(JButton[] nearbtns) {

        for (int i = 0; i < nearbtns.length; i++) {
            if (nearbtns[i] != null) {
                if (nearbtns[i].getBackground() == Color.red) {
                    // 变成原来的颜色
                    nearbtns[i].setBackground(defaultcolor);

                } else {
                    nearbtns[i].setBackground(Color.red);
                }
            }
        }

    }

    // 构造方法
    public Test08() {

        InitControl();

        this.setLayout(new GridLayout(10, 10));

        // 获取按钮的默认色
        defaultcolor = btn.getBackground();

        this.setSize(300, 350);

        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

Java GUI编程的更多相关文章

  1. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  2. Java GUI编程中AWT/swing/SWT的优缺点

    http://www.cnblogs.com/dugang/archive/2010/10/22/1858478.html AWT AWT是Abstract Window Toolkit(抽象窗口工具 ...

  3. Java GUI编程4---标签组件JLabel

    Java GUI编程4---标签组件JLabel 2018年06月11日 22:06:58 蓝蓝223 阅读数 12103更多 个人分类: Java书籍摘抄 所属专栏: Java Swing图形界面 ...

  4. java Gui编程 事件监听机制

    1.     GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在DOS命令行中通过javac java命令启动程序. 软件的交互的方式:   1. 命令交互方式    图书管理系统 ...

  5. java GUI编程二

    java基础学习总结--GUI编程(二) 一.事件监听 测试代码一: 1 package cn.javastudy.summary; 2 3 import java.awt.*; 4 import j ...

  6. Java GUI编程-(项目代码_扫雷_弹钢琴)

    --扫雷 package com;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionLis ...

  7. [Java] GUI编程基础 绘图

    库 swing awt 过程 创建窗口JFrame JFrame-->MenuBar-->Container 屏幕坐标系:左上角为原点 Graphics2D Main.java 1 imp ...

  8. Java GUI编程SwingUtilities.invokeLater作用

    1 http://blog.micxp.com/index.php/archives/109/ 2

  9. 实验十五 GUI编程练习与应用程序部署

    实验十五  GUI编程练习与应用程序部署 实验时间 2018-12-6 一:理论部分 1.Java 程序的打包:编译完成后,程序员将.class 文件压缩打包为 .jar 文件后,GUI 界面序就可以 ...

随机推荐

  1. Appium环境搭建

    早就想学习移动端的自动化测试:今天看了些资料@虫师,整理下环境搭建流程:标"1"为最佳流程: 文字版的流程可以参见虫师的博客:

  2. mongo3.2

    arbiter配置文件 processManagement: fork: true net: bindIp: 172.16.10.1,127.0.0.1 port: storage: dbPath: ...

  3. EF 延迟加载和预先加载

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨延迟加载和预先加载 Entity Frame ...

  4. csuoj 1119: Collecting Coins

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec  Memo ...

  5. Android 自定义 view(四)—— onMeasure 方法理解

    前言: 前面我们已经学过<Android 自定义 view(三)-- onDraw 方法理解>,那么接下我们还需要继续去理解自定义view里面的onMeasure 方法 推荐文章: htt ...

  6. GZIP压缩优化

    使用gzip优化web应用(filter实现) 相关知识: gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将页面文件和其他资源,返回到客户端,客户 ...

  7. Select For update语句浅析 (转)

    Select … for update语句是我们经常使用手工加锁语句.通常情况下,select语句是不会对数据加锁,妨碍影响其他的DML和DDL操作.同时,在多版本一致读机制的支持下,select语句 ...

  8. 二十一、Java基础--------IO流之综合案例分析

    前三篇文章详细介绍了IO流体系所涉及的重点内容,为了帮助理解与学习,本片博客主要是分析一个与IO操作相关的题目. 例1:在我们观看视频时经常要关注的就是视频的时间长度,在学习了IO操作之后,就可以自己 ...

  9. C# interface abstract class

  10. bzoj 1162 network

    树上的区间第k小数,以前写的主席树是一直MLE的,后来看到一种在初始化的时候的优化:直接DFS这颗树,得到每个点的主席树,然后更新的时候另外对DFS序建主席树,答案加上初始每个点的主席树,这样在初始化 ...