熱門文章 (Popular Post)

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

By :
GroupLayout example:

在使用Group Layout的時候,必須去計算物件的水平與垂直相對位置,
以下圖為範例,有4個Jlabel與4個JTextField。



首先設定水平群組:
在同一個水平線上,我們有兩個元件。
分別是1個Jlabel與1個JTextField,而每一個水平群組中,
各有4個Jlabel(lbl1,lbl2,lbl3,lbl4)與JTextField(tf1,tf2,tf3,tf4)。
所以可以撰寫程式碼如下:





layout.setHorizontalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup()
           .addComponent(lbl1)
           .addComponent(lbl2)
           .addComponent(lbl3)
           .addComponent(lbl4))
        .addGroup(layout.createParallelGroup()
           .addComponent(tf1)
           .addComponent(tf2)
           .addComponent(tf3)
           .addComponent(tf4))
);



其次我們設定垂直群組:

















共有4個垂直群組,每個垂直群組各有一個Jlabel與JTextField。
所以可以撰寫程式碼如下:

layout.setVerticalGroup(layout.createSequentialGroup()
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
           .addComponent(lbl1)
           .addComponent(tf1))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl2)
           .addComponent(tf2))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl3)
           .addComponent(tf3))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl4)
           .addComponent(tf4))
        .addGap(10)
     );


package SwingDemo;

import java.awt.BorderLayout;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


/***
 * 
 * @author Duran
 * 
 * GroupLayout Demo
 *
 */
public class GroupLayoutDemo {
 
 private static JFrame frame;
 
    public static void main(String[] args) {
     
     frame = new JFrame("Group Layout Demo");
     frame.setSize(200, 170);
     frame.setResizable(true);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     JPanel panel = new JPanel();
     GroupLayout layout = new GroupLayout(panel);
     panel.setLayout(layout);
     layout.setAutoCreateGaps(true);
     layout.setAutoCreateContainerGaps(true);
     
     
     JLabel lbl1 = new JLabel("lbl1");
     JLabel lbl2 = new JLabel("lbl2");
     JLabel lbl3 = new JLabel("lbl3");
     JLabel lbl4 = new JLabel("lbl4");
     
     JTextField tf1 = new JTextField();
     JTextField tf2 = new JTextField();
     JTextField tf3 = new JTextField();
     JTextField tf4 = new JTextField();

     //group layout
     layout.setHorizontalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup()
           .addComponent(lbl1)
           .addComponent(lbl2)
           .addComponent(lbl3)
           .addComponent(lbl4))
        .addGroup(layout.createParallelGroup()
           .addComponent(tf1)
           .addComponent(tf2)
           .addComponent(tf3)
           .addComponent(tf4))
  );

     layout.setVerticalGroup(layout.createSequentialGroup()
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
           .addComponent(lbl1)
           .addComponent(tf1))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl2)
           .addComponent(tf2))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl3)
           .addComponent(tf3))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl4)
           .addComponent(tf4))
        .addGap(10)
     );
  
     //BorderLayout
     frame.getContentPane().add(BorderLayout.CENTER,panel);

     frame.setVisible(true);

    }
}   


Demo
Tag : ,

By :
1.錯誤訊息:Referenced file contains errors (http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd)

解決方法:移除 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd"

原因:不明

日期:2013/3/18



2.錯誤訊息:javax.servlet.jsp.PageContext cannot be resolved to a type

解決方法:某些情況下,右鍵點選 project ->點選 properties -> 選擇 Project Facets -> 選擇 Dynamic Web Module -> 右邊的Runtimes 內容勾選錯誤

原因:設定錯誤

日期:2013/3/18



3.錯誤訊息:tomcat server garbled 亂碼

解決方法:設定server.xml中,<connector color="#ff0000" connectiontimeout="20000" font="" port="8080" protocol="HTTP/1.1" redirectport="8443">


原因:設定

日期:2013/3/18




--
如果覺得文章不錯,請幫忙點選廣告,
謝謝!



Tag : , , ,

By :
import:
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;


複製到剪貼簿(copy to clipboard):
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection data = new StringSelection("Copy string");
clipboard.setContents(data, data);



貼上剪貼簿內容(paste from clipboard):
Transferable pasteData = clipboard.getContents(clipboard);

if (pasteData != null) {
    try {
        if (pasteData.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String s = (String)(pasteData.getTransferData(DataFlavor.stringFlavor));
            //output s
            }
        } catch (UnsupportedFlavorException ex) {
            ex.printStackTrace();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
}

範例(example):
package SwingDemo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/***
 * 
 * @author Duran
 * 
 * Clipboard Demo
 *
 */
public class ClipboardExample {
 
 private static JFrame frame;
 
    public static void main(String[] args) {
     
     frame = new JFrame("Group Layout Demo");
     frame.setSize(400, 200);
     frame.setResizable(false);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     JPanel panel = new JPanel();
     GroupLayout layout = new GroupLayout(panel);
     panel.setLayout(layout);
     layout.setAutoCreateGaps(true);
     layout.setAutoCreateContainerGaps(true);
     
     JLabel lbl1 = new JLabel("lbl1");
     JLabel lbl2 = new JLabel("lbl2");

        final JTextArea tf1 = new JTextArea();
        tf1.setRows(3);
        tf1.setText("copy me");

        
        final JTextArea tf2 = new JTextArea();
        tf2.setRows(3);
        
        //Clipboard example
        final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        JButton jb1 = new JButton("cpoy");
        jb1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
          //*****************************************************************
          //copy string
          StringSelection data = new StringSelection(tf1.getText());
          clipboard.setContents(data, data);
          //*****************************************************************
         }
        });
        
        JButton jb2 = new JButton("paste");
        jb2.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
          
          //*****************************************************************
          Transferable pasteData = clipboard.getContents(clipboard);
           
          if (pasteData != null) {
              try {
                  if (pasteData.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                   tf2.setText( (String)(pasteData.getTransferData(DataFlavor.stringFlavor)));
                      }
                  } catch (UnsupportedFlavorException ex) {
                      ex.printStackTrace();
                  } catch (IOException ex1) {
                      ex1.printStackTrace();
                  }
          }
          //*****************************************************************
          
         }
        });

        //group layout
  layout.setHorizontalGroup(layout.createSequentialGroup()
   .addGroup(layout.createParallelGroup()
    .addComponent(lbl1)
    .addComponent(lbl2))
   .addGroup(layout.createParallelGroup()
    .addComponent(tf1)
    .addComponent(tf2))
   .addGroup(layout.createParallelGroup()
    .addComponent(jb1)
    .addComponent(jb2))
  );

  layout.setVerticalGroup(layout.createSequentialGroup()
   .addGap(10)
   .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    .addComponent(lbl1)
    .addComponent(tf1)
    .addComponent(jb1))
    
   .addGap(10)
   .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    .addComponent(lbl2)
    .addComponent(tf2)
    .addComponent(jb2))
   .addGap(10)

  );
  
  //BorderLayout
  frame.getContentPane().add(BorderLayout.CENTER,panel);

  frame.setVisible(true);

    }
}   




--
如果覺得文章不錯,請幫忙點選廣告,
謝謝!
Tag : ,

By :
JNLP :Java Network Launch Protocol (Java網路執行協定)
原來是使用NetBeans依據JAR檔案產生,也能透過使用者自行定義產生。
(說明參考資料出處:http://sofree.cc/java-web-start/)

Step1: Export excutable jar from Eclipse






Step2: 產生簽證
keytool -genkeypair -validity 3650 -keystore jyhshin.keystore

-alias:別名,預設值 mykey
-genkeypair:產生密鑰及憑證
-validity:憑證的有效時間,預設值只有 90 天
-keystore:儲存檔案



Step3: jarsigner
jarsigner -keystore xxx -digestalg SHA1 aaa.jar xxx





Step4: JNLP file
codebase:文件所在位置
href: url
title:城市名稱
vendor:來源
shortcut online:捷徑設定
resource: 其他jar(資源)位址
application-desc:應用程式名稱與主類別位置


下列為JNLP描述檔案


    
        應用程式名稱
        發行人
   
   
 
  
  
 
    
    
        
    
    
        
        
                
                
       
    
     
     

將JNLP、resource資料夾放置好,即可執行JNLP


Problem:
Problem 1. Base64 encode and decode that could cause the execution failed
org.apache.commons.codec.binary.Base64 
-> sun.misc.BASE64Decoder or sun.misc.BASE64Encoder

Problem 2. Problem accessing an image file from a jar file
example:
URL url = this.getClass().getClassLoader().getResource("imgs/no_photo.png");
Tag : ,

By :
DefaultTableModel  

取出 JTable 的 TableModel  
//get table model
DefaultTableModel tableModel = (DefaultTableModel) TablePanel.getTable().getModel();

新增 Row:
tableModel.addRow(Object[]  object);

移除 Row:
tableModel.removeRow(Index);

(使用迴圈由最後一列開始移除)

新增 Column:
public void addColumn(Object columnName, Object[] columnData)

事件:AddListSelectionListener
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
         @Override
         public void valueChanged(ListSelectionEvent e) {
          // row selection changed
          if (e.getValueIsAdjusting()) {
           //get index and value
           //method 1
           System.out.println(e.getSource());
           int selectRow = ((ListSelectionModel)e.getSource()).getLeadSelectionIndex();
           System.out.println(selectRow);
           System.out.println(table.getValueAt(selectRow, 0));
          
           // method 2
           selectRow =((JTable)  e.getSource()).getSelectedRow();
           ((JTable)  e.getSource()).getValueAt(selectRow, 0);
           }
       }

JList  

事件:AddListSelectionListener
JList list = new JList();
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(new ListSelectionListener(){
    public void valueChanged(ListSelectionEvent e) {
        Object selectedData = ((JList) e.getSource()).getSelectedValue();
    }
});
//list.setModel(model);

AbstractListModel
public class TestAbstractListModel extends AbstractListModel{
 private String[] testArray;
 @Override
 public Object getElementAt(int index) {
  // TODO Auto-generated method stub
  return testArray[index];
 }

 @Override
 public int getSize() {
  // TODO Auto-generated method stub
  return testArray.length;
 }
}



--
如果覺得文章不錯,請幫忙點選廣告,
謝謝!
Tag : ,

By :

jar: 
org.apache.commons.logging-1.1.1.jar
httpclient-4.2.2.jar
httpcpre-4.2.2.jar

download link: http://hc.apache.org/downloads.cgi



---------

package Test;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class Test {
public static void main(String[] args) throws Exception{
//GET method
DefaultHttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet("http://");

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println("Response Status: " + response.getStatusLine());

//POST method
HttpPost httpost = new HttpPost("http://");
httpclient.execute(httpost);

List <namevaluepair>  nvps = new ArrayList&lt <namevaluepair>();
nvps.add(new BasicNameValuePair("userId", "username"));
nvps.add(new BasicNameValuePair("password", "password"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();

System.out.println("Response Status: " + response.getStatusLine());

}
---------


--
如果覺得文章不錯,請幫忙點選廣告,
謝謝!
Tag : ,

- Copyright © Duran Hsieh @ Duran 的技術冶煉廠 - Date A Live - Powered by Blogger - Designed by Johanes Djogan -