顯示具有 Java UI 標籤的文章。 顯示所有文章
顯示具有 Java UI 標籤的文章。 顯示所有文章

2014年1月13日 星期一

[ Java 常見問題 ] Swing: JSplitPane splitting 50% precisely

來源自 這裡
Question:
In Swing, what's the best way to make the JSplitPane to split two jpanels with 50% size each. It looks like if I don't set preferred sizes on the panels it always makes the first panel almost invisible (2%) and the second one (98%)

Answer:
可以透過 API:setResizeWeight(weight). 該 API 的使用說明如下:
Specifies how to distribute extra space when the size of the split pane changes. A value of 0, the default, indicates the right/bottom component gets all the extra space (the left/top component acts fixed), where as a value of 1 specifies the left/top component gets all the extra space (the right/bottom component acts fixed). Specifically, the left/top component gets (weight * diff) extra space and the right/bottom component gets (1 - weight) * diff extra space.

假設你已經有一個 JSplitPane 物件 jSplitPaneEdit, 則你可以如下設定該 JSplitPane 的物件透過 divider 50/50 切割左右或上下平面:
  1. jSplitPaneEdit.setDividerLocation(-1);  
  2. jSplitPaneEdit.setResizeWeight(0.5);  
  3. jSplitPaneEdit.repaint();  

This message was edited 1 time. Last update was at 13/01/2014 16:44:07

2012年11月2日 星期五

[ Java 套件 ] JDatePicker - Java Swing Date Picker : Swing 的日期套件

Preface: 
最近在開發桌面 UI 程式, 需要讓使用者可以視覺化的選擇日期, 但是很遺憾的是 Swing 內並沒有類似的物件 orz. 在拜完 Google 大神後發現了一個不錯用的套件 JDatePicker
 

到 sourceforge 下載套件後, 開始要使用時, 才發現完全不知道怎麼著手. orz. 文件也沒有提到如何使用. 因此就自己撰寫了範例程式 "JDatePickerDemo" 提供有興趣的人可以快速的入門. 

Sample code: 
透過這邊的範例, 你將知道如何整合 JDatePicker 套件到你的 UI. 假設你有個 UI 使用JFrame, 上面有 JTextField 與按鈕, 透過按下該按鈕會出現 Date Picker. 在你挑選完日期後, 該日期會自動的設定到你期望的 JTextField 文字框中. 這邊我客製化了一個類別 "JDatePickerDialog" 包裝了 JDatePicker 裡面常用的功能. 它是一個 JDialog 用來與使用者互動, 並且可以將使用者選擇的日期回傳. 要使用它首先要先建立它: 
  1. datePicker = new JDatePickerDialog(thistrue"Demo"); // arg1=Interactive frame; arg2=mode, arg3=title  
在建構子提供三個參數: 
- arg1: Interactive JFrame
- arg2: JDialog modality
- arg3: Title of dialog

接著我們要向它註冊一個 JTextField, 讓使用者在挑選完日期後, 將結果寫回該文字框: 
  1. datePicker.registerTF(jTextFieldDate);  
接著如果你有特殊日期格式的需求, 則可以如下設定寫回的日期格式: 
  1. datePicker.applyDateFormat(new SimpleDateFormat("yyyy-MM-dd"));  
最後下面是一個完整的使用範例: 
  1. package net.sourceforge.jdatepicker.demo;  
  2. import java.awt.BorderLayout;  
  3. import java.awt.event.ActionEvent;  
  4. import java.awt.event.ActionListener;  
  5. import java.text.SimpleDateFormat;  
  6.   
  7. import javax.swing.ImageIcon;  
  8. import javax.swing.JButton;  
  9. import javax.swing.JPanel;  
  10. import javax.swing.JTextField;  
  11. import javax.swing.SwingUtilities;  
  12. import javax.swing.WindowConstants;  
  13.   
  14. import net.sourceforge.jdatepicker.impl.JDatePickerDialog;  
  15.   
  16. public class JDatePickerDemo extends javax.swing.JFrame {  
  17.     private static final long serialVersionUID = 1L;  
  18.     private JPanel jPanelMain;  
  19.     private JTextField jTextFieldDate;  
  20.     private JButton jButtonDate;  
  21.       
  22.     //  Customized  
  23.     private JDatePickerDialog datePicker;  
  24.   
  25.     /** 
  26.     * Auto-generated main method to display this JFrame 
  27.     */  
  28.     public static void main(String[] args) {  
  29.         SwingUtilities.invokeLater(new Runnable() {  
  30.             public void run() {  
  31.                 JDatePickerDemo inst = new JDatePickerDemo();  
  32.                 inst.setLocationRelativeTo(null);  
  33.                 inst.setVisible(true);  
  34.             }  
  35.         });  
  36.     }  
  37.       
  38.     public JDatePickerDemo() {  
  39.         super();  
  40.         initGUI();  
  41.         // Setup JDatePicker here.  
  42.         datePicker = new JDatePickerDialog(thistrue"Demo"); // arg1=Interactive frame; arg2=mode, arg3=title  
  43.         datePicker.registerTF(jTextFieldDate);  
  44.         datePicker.applyDateFormat(new SimpleDateFormat("yyyy-MM-dd"));  
  45.     }  
  46.       
  47.     private void initGUI() {  
  48.         try {  
  49.             setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  
  50.             {  
  51.                 jPanelMain = new JPanel();  
  52.                 getContentPane().add(jPanelMain, BorderLayout.CENTER);  
  53.                 jPanelMain.setBackground(new java.awt.Color(0,255,128));  
  54.                 jPanelMain.setLayout(null);  
  55.                 jPanelMain.setSize(30080);  
  56.                 {  
  57.                     jTextFieldDate = new JTextField();  
  58.                     jPanelMain.add(jTextFieldDate);  
  59.                     jTextFieldDate.setText("Date");  
  60.                     jTextFieldDate.setBounds(121231432);  
  61.                 }  
  62.                 {  
  63.                     jButtonDate = new JButton();  
  64.                     jPanelMain.add(jButtonDate);  
  65.                     jButtonDate.setBounds(332124132);  
  66.                     jButtonDate.setIcon(newImageIcon(getClass().getClassLoader().getResource("net/sourceforge/jdatepicker/demo/images/date.png")));  
  67.                     jButtonDate.addActionListener(new ActionListener() {  
  68.                         public void actionPerformed(ActionEvent evt) {  
  69.                             jButtonDateActionPerformed(evt);  
  70.                         }  
  71.                     });  
  72.                 }  
  73.             }  
  74.             pack();  
  75.             this.setSize(400100);  
  76.         } catch (Exception e) {  
  77.             //add your error handling code here  
  78.             e.printStackTrace();  
  79.         }  
  80.     }  
  81.       
  82.     private void jButtonDateActionPerformed(ActionEvent evt) {  
  83.         datePicker.setVisible(true);  
  84.     }  
  85.   
  86. }  
執行後會出現下面 UI: 
 

接著點取右邊日期圖案, 會出現下面 Dialog 提供使用者挑選日期: 
 

接著使用者可以在日期上 double click 滑鼠左鍵進行挑選, 這時會回到原先 UI 並在文字框已指定格式出現選擇日期: 
 

客製化的套件可以在 這裡 下載. 

Supplement: 
JDatePicker - Professional date components for Swing 
[ Java 代碼範本 ] java.text.SimpleDateFormat : 日期物件的格式化/文字化/標準化

2012年10月29日 星期一

[ Java 代碼範本 ] UI - Progress bar sample code

來源自 這裡 
Preface: 
在使用 Java Swing 開發 UI 程式時, 有時候需要進行耗時工作時, 可以使用 Progress bar 提示使用者目前工作進度或是告訴使用者目前程式的工作狀態. 

Demo Code: 
下面代碼執行會開啟一個 UI, 並在上面顯示 Progress bar. 在背景使用 SwingWorker (類別 UpdateWorker) 動態的更新 Progress bar 的進度條: 
 

當進度條上的數字達到 100, 則 SwingWorker 方法 done() 會被執行並關閉 UI 與結束程式. 

Implementation: 
  1. package swing.demo;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Container;  
  5. import java.util.Random;  
  6.   
  7. import javax.swing.BorderFactory;  
  8. import javax.swing.JFrame;  
  9. import javax.swing.JProgressBar;  
  10. import javax.swing.SwingWorker;  
  11. import javax.swing.border.Border;  
  12.   
  13. public class ProgressSample {  
  14.     public static void main(String args[]) {  
  15.         JFrame f = new JFrame("JProgressBar Sample");  
  16.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  17.         Container content = f.getContentPane();  
  18.         JProgressBar progressBar = new JProgressBar();  
  19.         progressBar.setValue(0);  
  20.         progressBar.setStringPainted(true);  
  21.         Border border = BorderFactory.createTitledBorder("Reading...");  
  22.         progressBar.setBorder(border);  
  23.         content.add(progressBar, BorderLayout.NORTH);  
  24.         f.setSize(300100);  
  25.         f.setVisible(true);  
  26.         UpdateWorker updateWorker = new UpdateWorker(f, progressBar);  
  27.         updateWorker.execute();  
  28.     }  
  29.       
  30.     public static class UpdateWorker extends SwingWorker   
  31.     {  
  32.         JProgressBar bar = null;  
  33.         JFrame f=null;  
  34.         public UpdateWorker(JFrame f, JProgressBar bar)  
  35.         {  
  36.             this.bar = bar;  
  37.             this.f = f;  
  38.         }         
  39.           
  40.         @Override  
  41.         protected String doInBackground() throws Exception {  
  42.             Random rdm = new Random();  
  43.             int pv = 0;  
  44.             while(pv<100)  
  45.             {  
  46.                 Thread.sleep(rdm.nextInt(500)+500);  
  47.                 pv+=rdm.nextInt(5);  
  48.                 bar.setValue(pv);  
  49.             }  
  50.             return null;  
  51.         }   
  52.           
  53.         @Override    
  54.         protected void done()   
  55.         {   
  56.             f.setVisible(false);      
  57.             f.dispose();  
  58.         }  
  59.     }  
  60. }  
Supplement: 
The Java Tutorial > How to Use Progress Bars 
Sometimes a task running within a program might take a while to complete. A user-friendly program provides some indication to the user that the task is occurring, how long the task might take, and how much work has already been done. One way of indicating work, and perhaps the amount of progress, is to use an animated image...

[ Java 代碼範本 ] SwingWorker Example

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...