package com.wulala;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class SwingComponent {

public static int TOTAL_DOT = 6 * 17;
    public boolean ifPressed = false;

LightBtn lightBtns[];
    JFrame frame = new JFrame("6×17点阵");

JButton clearButton = new JButton("清空");
    JButton getResultButton = new JButton("计算结果数组");
    JTextArea textField = new JTextArea();
    public JFrame jf = new JFrame("BorderLayout");

public static void showMe(JFrame jf) {
        jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        jf.setSize(300, 300);
        jf.setVisible(true);
    }

public void setBorderLayout() {

jf.setLayout(new BorderLayout());// BorderLayout是Swing容器的默认布局管理器,它的含义是采用东南西北中5个方位来进行布局,可以分别往这些方位上放置组件。
        jf.add(new JButton("east"), BorderLayout.EAST);
        jf.add(new JButton("south"), BorderLayout.SOUTH);
        jf.add(new JButton("west"), BorderLayout.WEST);
        jf.add(new JButton("north"), BorderLayout.NORTH);
        jf.add(new JButton("center"), BorderLayout.CENTER);
        SwingComponent.showMe(jf);
    }

public void init() {
        lightBtns = new LightBtn[TOTAL_DOT];
        bondListerner4Button();
        textField.setLineWrap(true);
        frame.setLayout(new BorderLayout());
        JPanel eastPanel = new JPanel();
        JPanel westPanel = new JPanel();
        eastPanel.setLayout(new BorderLayout());
        westPanel.setLayout(new GridLayout(6, 17));
        for (int i = 0; i < TOTAL_DOT; i++) {
            LightBtn button = new LightBtn();
            button.setBackground(Color.BLACK);
            lightBtns[i] = button;
            westPanel.add(button);
        }

// button.setSize(100, 30);
        eastPanel.add(clearButton, BorderLayout.NORTH);
        eastPanel.add(getResultButton, BorderLayout.SOUTH);
        // eastPanel.add(new JButton("清空"));
        // textField.setSize(300,300);
        eastPanel.add(textField, BorderLayout.CENTER);
        eastPanel.setBackground(Color.BLACK);
        eastPanel.setPreferredSize(new Dimension(300, 150));
        frame.add(eastPanel, BorderLayout.EAST);
        frame.add(westPanel, BorderLayout.CENTER);
        frame.setSize(900, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        // frame.setLocation(400, 400);
        frame.setResizable(false);
        // frame.pack();
        frame.setVisible(true);
    }

private void bondListerner4Button() {
        getResultButton.addMouseListener(new MouseListener() {

@Override
            public void mouseClicked(MouseEvent e) {
                String resultStr = "";
                String lineStr = "[";
                for (int i = 0; i < TOTAL_DOT; i++) {

int bit = 0;
                    if (lightBtns[i].ifWhite) {
                        bit = 1;
                    }
                    lineStr = lineStr + bit + ",";
                    if ((i % 17) == 16 && (i > 0)) {
                        lineStr = lineStr.substring(0, lineStr.length() - 1);
                        resultStr = resultStr + lineStr + "]\n";
                        lineStr = "[";
                    }
                }
                textField.setText(resultStr);
            }

@Override
            public void mouseEntered(MouseEvent e) {
            }

@Override
            public void mouseExited(MouseEvent e) {
            }

@Override
            public void mousePressed(MouseEvent e) {
            }

@Override
            public void mouseReleased(MouseEvent e) {
            }

});

clearButton.addMouseListener(new MouseListener() {

@Override
            public void mouseClicked(MouseEvent e) {
                for (int i = 0; i < TOTAL_DOT; i++) {
                    lightBtns[i].turnOff();
                }
                textField.setText("");
            }

@Override
            public void mouseEntered(MouseEvent e) {
            }

@Override
            public void mouseExited(MouseEvent e) {
            }

@Override
            public void mousePressed(MouseEvent e) {
            }

@Override
            public void mouseReleased(MouseEvent e) {
            }

});
    }

public static void main(String[] args) {
        SwingComponent sc = new SwingComponent();
        sc.init();
        // sc.setBorderLayout();
    }

class LightBtn extends JButton implements MouseListener {

public boolean ifWhite = false;

public void turnOff() {
            ifWhite = false;
            this.setBackground(Color.BLACK);
        }

public void switchBackgroundColor() {

if (ifWhite == true) {
                this.setBackground(Color.BLACK);
                ifWhite = false;
            } else {
                this.setBackground(Color.WHITE);
                ifWhite = true;
            }
        }

public LightBtn() {
            this.addMouseListener(this);
        }

@Override
        public void mouseClicked(MouseEvent arg0) {
            // System.out.println("clicked");
            switchBackgroundColor();
        }

@Override
        public void mouseEntered(MouseEvent arg0) {
            if (ifPressed) {
                switchBackgroundColor();
            }
        }

@Override
        public void mouseExited(MouseEvent arg0) {

}

@Override
        public void mousePressed(MouseEvent arg0) {
            ifPressed = true;
        }

@Override
        public void mouseReleased(MouseEvent arg0) {
            ifPressed = false;
        }
    }
}

6*17点阵的Window程序, Java写的。的更多相关文章

  1. 基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写

    基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写 专业程序代写服务(QQ:928900200) 随着社会的进步.服务行业的服务水平不断发展与提高,宾馆.酒店.旅游等服务行业的信息量和工作 ...

  2. 五:用JAVA写一个阿里云VPC Open API调用程序

    用JAVA写一个阿里云VPC Open API调用程序 摘要:用JAVA拼出来Open API的URL 引言 VPC提供了丰富的API接口,让网络工程是可以通过API调用的方式管理网络资源.用程序和软 ...

  3. 终于解决了用JAVA写窗口程序在不同的windows界面下的显示保持一致。

    好像是两三年前的时候发现这个问题. 由于在windows经典界面与windows xp界面下,窗口的标题栏的高度是不一样的. 所以我们在用Java写GUI程序的时候,会遇到一个问题. 当我把一个JFr ...

  4. 用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1

    package com.ljn.base; /** * @author lijinnan * @date:2013-9-12 上午9:55:32 */ public class IncDecThrea ...

  5. 程序代写, CS代写, 代码代写, CS编程代写, java代写, python代写, c++/c代写, R代写, 算法代写, web代写

    互联网一线工程师程序代写 微信联系 当天完成 查看大牛简介特色: 学霸代写,按时交付,保证原创,7*24在线服务,可加急.用心代写/辅导/帮助客户CS作业. 客户反馈与评价 服务质量:保证honor ...

  6. CS代码代写, 程序代写, java代写, python代写, c/c++代写,csdaixie,daixie,作业代写,代写

    互联网一线工程师程序代写 微信联系 当天完成特色: 互联网一线工程师 24-48小时完成.用心代写/辅导/帮助客户CS作业. 客户反馈与评价 服务质量:保证honor code,代码原创.参考课程sl ...

  7. 模拟游客一天的生活与旅游java程序代写源码

    在某个城市的商业区里,有一家首饰店,一家饭店,一家面馆,一家火锅店,一家银行,一家当铺 现在有一群来自四川的游客,一群陕西的游客,一群上海的游客,和以上各店家的工作人员在此区域里,请模拟他们一天的生活 ...

  8. Java写的斗地主游戏源码

    源码下载在最后 我们的前年的课设要求做一个斗地主程序,当时正在愁如何做界面,当时刚好在学习C#,于是就用C#完成了这个程序.一方面,当时我C#功底还很差(其实现在也不怎么样),很多地方用了“笨办法”, ...

  9. 学了编译原理能否用 Java 写一个编译器或解释器?

    16 个回答 默认排序​ RednaxelaFX JavaScript.编译原理.编程 等 7 个话题的优秀回答者 282 人赞同了该回答 能.我一开始学编译原理的时候就是用Java写了好多小编译器和 ...

随机推荐

  1. openssl下载

    openssl的windows版本.微信支付开发中退款接口中使用到. http://pan.baidu.com/s/1c0vQy6O

  2. WebApi 找到了与该请求匹配的多个操作

    在同一个命名空间内,报错 错误的路由代码: namespace CRM4S { public static class WebApiConfig { public static void Regist ...

  3. canvas 基础知识整理(二)

    html部分: <canvas id="myCanvas" width="800" height="800" ></can ...

  4. MongoDB分片集群还原

    从mongodb 3.0开始,mongorestore还原的时候,需要一个运行着的实例.早期的版本没有这个要求. 1.为每个分片部署一个复制集 (1)复制集中的每个成员启动一个mongod mongo ...

  5. mysql查找字符串出现位置

    MySQL中的LOCATE和POSITION函数使用方法 FIND_IN_SET(str,strlist) 假如字符串str 在由N 子链组成的字符串列表strlist 中,则返回值的范围在 1 到 ...

  6. Lintcode: Segment Tree Query

    For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...

  7. HTML调用servlet(二)

    5.修改数据 5.1编写查询条件页面 修改单条数据的时候,首先是查询出单个数据的详细信息,然后根据实际需要部分修改或者全部修改.修改之后,数据会提交到数据库,数据库中保存更新以后的数据. 查询出单条数 ...

  8. acm算法模板(5)

    STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...

  9. 卸载了mysql之后,mysql服务仍在,显示读取描述失败,错误代码2

    卸载了mysql之后,mysql服务仍在,显示读取描述失败,错误代码2 用360软件管家,卸载mysql5.5,卸载了mysql之后,再依次删除 mysql的安装目录.c盘下的隐藏文件夹Program ...

  10. 使用git做服务器端代码的部署

    传统部署方案     windows 远程桌面     FTP/SFTP     登录服务器pull github代码     Phing(PHP专业部署工具) git 自动部署流程图   服务器端准 ...