2016年5月28日 星期六

JavaSwing-入門、基本說明( 一 )

Java Swing 簡單的說是把 Java 的程式用圖形化輸入、顯示

以一般使用者來說 : 以下 2 張圖哪一張比較計算功能較直覺?
題目 : 2 + 3 = 5
1 . 命令提示視窗















2 . GUI ( 圖形化介面 )
























就一般使用者來說, GUI 淺顯易懂
若使用命令提示字元,想必等級一定不低吧...

工作環境請參照 : Android-1-Eclipse環境設定 X 3

如果沒有任何程式開發經驗者
可以先想像 : 我們畫一張圖畫,則需要那些用具??
Model、畫筆、
對,我們就是先要一張紙
您可以先把 JFrame 想像一張紙 
JFrame 就是我們整個 UI 的基底

我們先來測試吧
首先,新增一個 Class

import javax.swing.JFrame;

public class FirstPage {

    private JFrame jFrame;
    
    public FirstPage() {
        jFrame = new JFrame();
        // 視窗大小
        jFrame.setSize(600, 600);
        // 視窗標題
        jFrame.setTitle("FirstTitle");
        // 定義關閉視窗後的動作 ( 結束內部程式運作 )
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 是否顯示視窗
        jFrame.setVisible(true);
    }
    
    public static void main(String[] arg) {
        FirstPage firstPage = new FirstPage();
    }
}

以下就是我們的第一個範例 :















參考資料 : Java 入門指南 - GUI 的基本概念
參考資料 : 深入淺出 Java
參考資料 : JavaSwing 頭到尾

歡迎轉載,請註明出處。





2016年5月18日 星期三

RaspberryPi-安裝_應用程式

編輯器

Maven-在 Eclipse 創立 Maven 專案_執行 Jar ( 三 )

上一篇 : Maven-在 Eclipse 創立 Maven 專案_Hello Maven( 二 )

用 Maven build 生成的 Jar 我們執行看看會發生什麼事情?
在我們剛剛訊息列裡面
1 . 生成的 Jar 擺放位置














2 . 不想看的結果
原因 : 因為無法辨別主要的進入點















3 . 我們在專案內的 pom 新增
<build>
    <plugins>
        
        <!-- environment problem -->
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <fork>true</fork>
                <executable>C:\JAVA\jdk\bin\javac.exe</executable>
            </configuration>
        </plugin>

        <!-- 
        打包成 Jar 檔案
        目的 : 
        必須給進入點,否則執行會出現   
        XXX.jar 中沒有主要資訊清單屬性
          -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>first.Hello</mainClass>
                        <packageName>first</packageName>
                    </manifest>
                    <manifestEntries>
                        <mode>development</mode>
                        <url>${pom.url}</url>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

    </plugins>
</build>

4 . 我們再重新建立














5 . 重新執行















參考資料 : Eclipse Blue, Maven: Project configuration is not up-to-date with pom.xml

歡迎轉載,請註明出處。

Maven-在 Eclipse 創立 Maven 專案_Hello Maven( 二 )

上一章 : Maven-在 Eclipse 創立 Maven 專案_環境安裝( 一 )

開始執行程式
1 . 創立 Maven 專案














2 . 選擇專案樣板














3 . 輸入專案相關資料














4 . 專案創立後雖然長很多子項目,但結果如黃框














5 . 建立檔案夾目錄














6 . 建立 Main Class














7 . 測試結果可執行














8 . 使用 Maven build














9 . 建立














10 . Build maven error

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Brian: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException















解法
1 . 更新當前的 Jre
2 . 在 Maven 專案內的 pom 新增plugin
( work for me )
</project ...>

    .
    .
    .
    
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <fork>true</fork>
                    <executable>C:\JAVA\jdk\bin\javac.exe</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>    
</project>
















11 . 再次執行














參考資料 : Maven 教學-HelloMaven 第一個Maven專案 (Step by Step ~)

歡迎轉載,請註明出處。

Maven-在 Eclipse 創立 Maven 專案_環境安裝( 一 )

Maven 版本控管的的好處眾所皆知
但我們如何從無到有?
以下初始安裝教學

1 . 首先下載最新的 Maven






























2 . 存放指定位置( 後續環境變數 與 IDE 都要此路徑 )
此路徑檔案已改全大寫( 請見下圖 )











3 . 設置環境變數 :
MAVEN_HOME 複製以上檔案路徑














Path : %MAVEN_HOME%\bin






















4 . 再命令提示字元,確認環境變數設置是否正確
mvn -version














歡迎轉載,請註明出處。

2016年5月12日 星期四

Android-SQLite 使用(一)

趁最近有機會
可以有接觸 SQLite
就把小簡易程式碼放此
並且實案教學

1 . 為什麼要用 DB 存取
2 . 有需要存哪些資料
3 . 要有啥功能
....等,如有需要補充
請至下方留言

簡易專案:
名稱:筆記本
用途:使用者紀錄每天備忘錄

1 . 建立資料庫
create table counter (
  id INTEGER PRIMARY KEY   AUTOINCREMENT,
  count_date TimeStamp UNIQUE  NOT NULL,
  count_number int,
  count_note char(255)
);

2 . 新增資料
insert into counter (count_date, count_number, count_note) 
      values (Date(), 20, "1_1");

3 . 修改資料
update counter set count_number = 40, count_note = "1_2"
      where count_date = Date();

4 . 刪除資料
delete from counter where count_date = Date();

5 . 查詢資料
select * from counter;
select * from counter order by count_date ASC;  小到大
select * from counter order by count_date DESC; 大到小
select count_number from counter where count_date = "2017-12-30";


參考資料 : Android高效入門—SQLite資料庫
參考資料:How to store data locally in an Android app
參考資料:How do I view the SQLite database on an Android device? [duplicate]

Android-TextView

參考資料 : 实现TextView 文字排版,分散两端对齐

2016年5月4日 星期三

JAVA-檢查字串是否包含 中文、英文、數字

20160720 更新項目 : 添加程式碼格式
Java 文字、字串處理

清除空字串
str = str.replaceFirst(" ", "");// 前
str = str.replaceAll("\\s+$", "");// 後
str = str.trim();// 全


// The string is include number
private static boolean strIncludNumber(String str){
    boolean hasNum = false;

    if(str.matches(".*\\d+.*"))
        hasNum = true;

    return hasNum;
}


// The string is include english
private static boolean strIncludEnglish(String str){
    boolean hasEng = false;

    for(int i=0; i<str.length(); i++)
    {
        String test = str.substring(i, i+1);
        if(test.matches("[a-zA-Z]+"))
            hasEng = true;
    }

    return hasEng;
}

// The string is include chinese
private static boolean strIncludCheinese(String str){
    boolean hasChinese = false;
    for(int i=0; i<str.length(); i++)
    {
        String test = str.substring(i, i+1);
        if(test.matches("[\\u4E00-\\u9FA5]+"))
            hasChinese = true;
    }
    return hasChinese;
}

JAVA-SWING

UI排版 : 視窗程式 Java Swing - 基本設定及Layout
參考資料 : Java 的布局管理器GridBagLayout的使用方法【图文说明】