----基础

// 创建一个窗体对象
        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. Issue 6: 装机系列1,PC下windows系统安装指南

    0.前言 接触电脑将近7年时间,多次说要写下这篇文章,一直未曾提笔,始终怕给人以误导.到如今,来来回回装系统的次数得超过百次了.本着不误导人的想法,本文试着总结一下装系统的基本方法和思路,但不会过多涉 ...

  2. ios通知机制

  3. 如何将一个Excel文件中的sheet移动到另外一个Excel?

    背景 工作中往往会有多个excel维护的情况,随着业务的变化, 将一个Excel合并到另外一个Excel,成为必须. 如何移动sheet,对于不会的人,这是一个好问题, 也许你经过多次尝试都没有成功. ...

  4. lua下的简单OO实现

    笔者学习了当前(文末各文献)lua下的各种OO实现方法.略作笔记. 也提出了一些自己的想法.主要还是记录供将来着之参考.   1.概述   首先[2]PIL第二版中给出了OO的基于table的实现方式 ...

  5. windows下搭建nginx+php+mysql环境

    一.下载需要的东西 1.nginx:http://nginx.org/en/download.html 2.php:http://php.net/downloads.php 3.mysql:(暂时先不 ...

  6. Leetcode: Circular Array Loop

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  7. maven打包日志输出优化-去掉泛型与过时的警告

    pom.xml配置 1.使用eclipse编译 <!-- Eclipse编译代码时,使用的是自带的JDT(Java Development Tools), 而Maven默认使用的是JAVA_HO ...

  8. GIT版本库回滚【图文版】

    git 版本库回滚,在实际开发过程中总会遇得到   1. 先找出需要回滚的commitid     git log -3   2. 重置本地版本库到指定commitid, 注意:本地改动将丢失     ...

  9. Web Word和Excel

    暂时收集点资料备用 Excel http://www.cnblogs.com/downmoon/archive/2011/05/30/2063258.html http://www.cnblogs.c ...

  10. Xstream解析XML

    <oschina> <catalog>1</catalog> <newsCount>0</newsCount> <pagesize&g ...