2017年3月29日 星期三

JavaFX : Apache POI存取Excel


Apache POI 提供存取微軟Office文件的API,到http://poi.apache.org/download.html POI套件下載並將jar檔加入專案。



Excel 檔案結構

一份Excel檔案為一個Workbook,Workbook中有多個Sheet(即分頁),Sheet中有多個Row(即列),Row中有多個Cell(即儲存格)。

2017年3月26日 星期日

2017年3月23日 星期四

Blogger : 文章撰寫模式切換,程式碼就會產生異常的解決方法

在Blogger上撰寫程式碼多少可能會發生格式跑掉或少字的情況

引起各種異狀的原因在於 Blogger 太過聰明(或自作聰明),自動對內容進行解析,但又解析錯誤。

解決的方法就是把程式碼轉換成HTML網頁格式

2017年3月21日 星期二

JavaFX架構與Scene Builder

JavaFX可以用來設計擁有輕量且高效能使用者圖形介面(Graphical User Interface, GUI)的應用程式(Application),還能作為Rich internet application來發佈。相較於傳統Java使用Awt、Swing實作Application(或是Applet)的方式,JavaFX提供了更多、更好的工具以及函式庫協助開發應用程式,而且製作出來的程式效能更好,畫面更美!

如何在Eclipse安裝JavaFx

如何在Eclipse安裝JavaFx的套件


本人寫JavaFX的IDE是使用Eclipse,所以只針對Eclipse做介紹

2017年3月20日 星期一

Blogger : 如何使用程式碼區塊

很多Blogger初學者,第一次使用blogger開心地建好網站,卻發現要貼上程式碼時發現不知道怎麼使用程式碼區塊

Android BMI計算範例

Android BMI計算範例


先上範例圖片 : 

第一頁

第二頁

第三頁


程式碼的部分 : 


第一頁的xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="25dp"
    tools:context="tw.com.hjchen.intentbmi.MainActivity"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高 : "
            android:padding="15dp"
            android:textSize="25sp"
            android:textColor="#66B3FF"/>
        <EditText
            android:id="@+id/edtheight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="體重 : "
            android:padding="15dp"
            android:textSize="25sp"
            android:textColor="#66B3FF"/>
        <EditText
            android:id="@+id/edtweight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="BMI計算"
        android:textSize="25sp"/>

</LinearLayout>

第一頁.java :


public class MainActivity extends AppCompatActivity {

    private EditText edtheight,edtweight;
    private Button btn01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
    }
    private void findViews(){
        edtheight = (EditText)findViewById(R.id.edtheight);
        edtweight = (EditText)findViewById(R.id.edtweight);
        btn01 = (Button)findViewById(R.id.btn01);
        btn01.setOnClickListener(btn01OnClickListner);
    }
    private Button.OnClickListener btn01OnClickListner = new Button.OnClickListener(){
        public void onClick(View view){
            try {
                double height = Double.parseDouble(edtheight.getText().toString());
                double weight = Double.parseDouble(edtweight.getText().toString());
                Intent it = new Intent();
                it.setClass(MainActivity.this, Main2Activity.class);
                Bundle bundle = new Bundle();
                bundle.putDouble("height", height);
                bundle.putDouble("weight", weight);
                it.putExtras(bundle);
                startActivity(it);
            }catch (Exception e){
                Toast.makeText(MainActivity.this,"輸入錯誤",Toast.LENGTH_SHORT).show();
            }
        }
    };
}


第二頁.xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="25dp"
    tools:context="tw.com.hjchen.intentbmi.Main2Activity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="結果 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="15dp"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="60dp">
        <Button
            android:id="@+id/btnback"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="重新計算"
            android:textSize="25sp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/btnnext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="體位測量"
            android:textSize="25sp"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>

第二頁.java :


public class Main2Activity extends AppCompatActivity {

    private Button btnback,btnnext;
    private TextView txtresult;
    double height,weight,bmi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        findViews();
    }
    private void findViews(){
        btnback = (Button)findViewById(R.id.btnback);
        btnnext = (Button)findViewById(R.id.btnnext);
        txtresult = (TextView)findViewById(R.id.result);
        btnback.setOnClickListener(btnbackOnClickListener);
        btnnext.setOnClickListener(btnnextOnClickListener);
        Bundle bundle = getIntent().getExtras();
        height = bundle.getDouble("height");
        weight = bundle.getDouble("weight");
        calculate();
    }
    private void calculate(){
        bmi = weight/((height*height)/10000);
        try{
            if(bmi<18 .5="" ain2activity.this="" ain3activity.class="" bmi="" btnbackonclicklistener="new" btnnextonclicklistener="new" bundle2.putdouble="" bundle2="" bundle="" button.onclicklistener="" catch="" code="" e="" else="" finish="" height="" iew="" if="" intent="" it.putextras="" it.setclass="" it="" onclick="" private="" public="" startactivity="" txtresult.settext="" view="" void="" weight="" xception="">

第三頁.xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="25dp"
    tools:context="tw.com.hjchen.intentbmi.Main3Activity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <TextView
            android:id="@+id/txtheight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="15dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="體重 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <TextView
            android:id="@+id/txtweight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="15dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性別 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <EditText
            android:id="@+id/sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入F或M"
            android:gravity="center"
            android:textSize="25sp"
            android:textColorHint="#FF0000"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="腰圍 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <EditText
            android:id="@+id/waistline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入腰圍"
            android:gravity="center"
            android:textSize="25sp"
            android:textColorHint="#FF0000"/>
    </LinearLayout>

    <Button
        android:id="@+id/btncalculate"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="計算"
        android:textSize="25sp"
        android:layout_gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="結果 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="15dp"/>
    </LinearLayout>

</LinearLayout>

第三頁.java :


public class Main3Activity extends AppCompatActivity {

    private TextView txtheight,txtweight,txtresult;
    private EditText sex,waistline;
    private Button btncalculate;
    private double height,weight;
    private int intwaistline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        findViews();
    }
    private void findViews(){
        txtheight = (TextView)findViewById(R.id.txtheight);
        txtweight = (TextView)findViewById(R.id.txtweight);
        txtresult = (TextView)findViewById(R.id.result);
        sex = (EditText)findViewById(R.id.sex);
        waistline = (EditText)findViewById(R.id.waistline);

        btncalculate = (Button)findViewById(R.id.btncalculate);
        Bundle bundle = getIntent().getExtras();
        height = bundle.getDouble("height");
        String shieght = String.valueOf(height);
        weight = bundle.getDouble("weight");
        String sweight = String.valueOf(weight);
        txtheight.setText(shieght);
        txtweight.setText(sweight);
        btncalculate.setOnClickListener(btncalculateListner);
    }
    private Button.OnClickListener btncalculateListner = new Button.OnClickListener(){
      public void onClick(View view){
          intwaistline = Integer.parseInt(waistline.getText().toString());
          String ssex = sex.getText().toString();
          switch (ssex){
              case "F":
                  if(intwaistline<80 break="" case="" code="" default:="" else="" if="" intwaistline="" txtresult.settext="">