----基础

// 创建一个窗体对象
        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. sprintf()函数的用法

    Visual C++ sprintf()函数用法 转:http://blog.csdn.net/masikkk/article/details/5634886 在将各种类型的数据构造成字符串时,spr ...

  2. 流量三角形:并非简单的"统计学"

    又忙了一周多,今天过来再整理一些东西.国内做产险精算的,准备金的居多,从精算部落中的帖子的跟帖情况可见一斑.既然准备金更容易受到大家的关注,今天再整理一个关于准备金的个人看法,给精算部落收敛一下人气, ...

  3. DataTable 转 List<T>

    最近在做一个项目,表的数据巨多,而且表的字段一般都在30个以上.公司规定不能用Nhibernate以及ef等ORM框架. 所以查询绑定时的工作量极为痛苦.没有办法,自己写了个DataTableToLi ...

  4. 点餐系统3个sprint的团队贡献分

    第一次冲刺贡献分   团员名字 贡献分 麦锦俊 18分 冯婉莹 21分 李康梅 19分 张鑫相 20分 曹嘉琪 22分     第二次冲刺贡献分   团员名字 贡献分 麦锦俊 19分 冯婉莹 20分 ...

  5. css元素排列

    有时候元素的排列没有预想的效果,考虑是不是margin和padding的影响

  6. C/C++ 结构体 指针 简单输入输出

    #include <stdio.h> #include <stdlib.h> struct student{ int num; ]; double dec; }; int ma ...

  7. Ext中renderer用法及参数

    转载处:http://blog.csdn.net/yangxiaojun9238/article/details/8240139 setHeader是设置http恳求的头是MIME 和谈的拓展可以实现 ...

  8. VB.NET中Form窗体运行时,按ESC退出全屏状态

    1.在其KeyDown事件添加: If e.KeyValue = 27 Then Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable ...

  9. SSH框架使用中存在的诡异异常

    背景 相信大多数人目前都在使用Spring + Struts2/SpringMVC + Hibernate来构建项目的整体架构,但是在使用中经藏会遇到一些诡异的问题,不知道如果解决,今天我遇到了一个非 ...

  10. phpcms v9联动菜单的调用方法及get_linkage函数简单过程

    {get_linkage($r['areaid'],1,' >> ',1)} 复制代码 当然这个 $r 也不是绝对的.要看你的loop 是如何写的. {loop $data $n $r} ...