----基础

// 创建一个窗体对象
        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. iOS - UIButton设置文字标题下划线以及下划线颜色

    创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...

  2. LuaSrcDiet工具介绍(lua源码处理软件)

    Diet Food Diet (nutrition), the sum of the food consumed by an organism or group Dieting, the delibe ...

  3. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  4. 【VirtualBox】 Failed to open/create the internal network 'HostInterfaceNetworking-VirtualBox Host

    win10 VirtualBox_5.0.24.8355_Win 安装后导入.ova 文件后 虚拟机不能正常启动 ===> 解决: “打开网络和共享中心” “更多适配器设置” 选择 对应的网络适 ...

  5. 画布清理////////////////////////////zzzz

    HTML5画布清理,重绘,旧的线条也会出现 <!DOCTYPE html> <html> <head> <title>HTLM5</title&g ...

  6. HDU-1274 展开字符串

    Problem Description 在纺织CAD系统开发过程中,经常会遇到纱线排列的问题.该问题的描述是这样的:常用纱线的品种一般不会超过25种,所以分别可以用小写字母表示不同的纱线,例如:abc ...

  7. 最好的cpm广告联盟哪里有

    最好的cpm广告联盟哪里有,58传媒广告联盟还要提醒众位站长的是网站在经营发展中必须最大化的扩展自己的优势力量.每个网站都有属于自己的优势魅力,这些优势特点只有得到最大化的发挥才能为网站带来意想不到的 ...

  8. Oracle EBS APIs

    参考链接: http://blog.csdn.net/pan_tian/article/details/7754598 http://blog.itpub.net/26687597/viewspace ...

  9. Djunit工作记录Mock时出现为null的情况setReturnValueAt|MockObjectManager.addReturnValue不起作用

    最近工作要对代码进行局部测试并编写测试文档,可是偶的环境还没搞好哦(开始不太乐意直接请教前辈,还是选择自己先搞一下了)经过电脑重装jdk ,eclipse,djunit工具包ver***,等等确定不是 ...

  10. infoq - neo4j graph db

    My name is Charles Humble and I am here at QCon New York 2014 with Ian Robinson. Ian, can you introd ...