PDA

View Full Version : [Q] Hỏi về tiếng Việt trong Java



noname2003
26-02-2003, 18:18
Hiện nay mình không biết cách làm thế nào để hiển thị tiếng Việt trong các Form, Menu .v.v... của Java. Bạn nào có kinh nghiệm về vấn đề này thì có thể chỉ giáo cho mình chút ít về vấn đề này một chút.
Cám ơn nhiều

quangvu
27-02-2003, 15:13
Các Tools hỗ trợ Unicode của Java còn nhiều hạn chế .Do đó ,cách hay nhất là bạn chuyển mã từ Char -> String
Ví dụ ,bạn có thể dùng các kí hiệu \122 hay \233 để hiện các chử Việt .
Chúc thành công !

QuanN
27-02-2003, 15:25
Đây là 1 thí dụ đơn giản. Nếu chạy trên Win98 thì cần có JDK từ 1.4 trở lên vì 1.3.1 bị bug không hiển thị đầy đủ chữ Việt được; nếu trên WinNT/2K/XP thì 1.3.1 OK. System cần có font Arial compatible với Unicode.

Dùng VietIME (http://vietime.sf.net) để nhập chữ Việt vào Java source code. Có thể save source file trong UTF-8 để giữ chữ Việt trong nguyên dạng thay vì trong Unicode escape sequences \u***X nếu để default ASCII encoding.


/*
* VietGUI.java
*
* Created on February 27, 2003, 12:08 AM
*/

/**
*
* @author quan
*/
public class VietGUI extends javax.swing.JFrame {

/** Creates new form VietGUI */
public VietGUI() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem7 = new javax.swing.JMenuItem();

setTitle("VietGUI Example");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jTextArea1.setFont(new java.awt.Font("Arial", 0, 12));
getContentPane().add(jTextArea1, java.awt.BorderLayout.CENTER);

jMenu1.setFont(new java.awt.Font("Arial", 1, 12));
jMenu1.setLabel("H\u1ed3 S\u01a1");
jMenuItem1.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem1.setText("M\u1edbi");
jMenu1.add(jMenuItem1);

jMenuItem2.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem2.setText("M\u1edf");
jMenu1.add(jMenuItem2);

jMenuItem6.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem6.setText("L\u01b0u");
jMenu1.add(jMenuItem6);

jMenuBar1.add(jMenu1);

jMenu2.setText("Duy\u1ec7t B\u00e0i");
jMenu2.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem3.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem3.setText("Sao");
jMenu2.add(jMenuItem3);

jMenuItem4.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem4.setText("C\u1eaft");
jMenu2.add(jMenuItem4);

jMenuItem5.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem5.setText("D\u00e1n");
jMenu2.add(jMenuItem5);

jMenuBar1.add(jMenu2);

jMenu3.setFont(new java.awt.Font("Arial", 1, 12));
jMenu3.setLabel("Gi\u00fap \u0110\u1ee1");
jMenuItem7.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem7.setText("Gi\u1edbi Thi\u1ec7u");
jMenu3.add(jMenuItem7);

jMenuBar1.add(jMenu3);

setJMenuBar(jMenuBar1);

pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize ();
setSize(new java.awt.Dimension(400, 300));
setLocation((screenSize.width-400)/2,(screenSize.height-300)/2);
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new VietGUI().show();
}


// Variables declaration - do not modify
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuBar jMenuBar1;
// End of variables declaration

}

noname2003
27-02-2003, 17:11
Cám ơn các bạn nhiều!

meomuop
27-02-2003, 18:17
QuanN ơi !

Cho tui hỏi một chút xíu nữa coi :

Nếu muốn lưu trữ Unicode trong MySQL thì phải làm sao ? Tui có thử chuyển Unicode qua UTF8 rồi lưu trong MySQL (dạng String) nhưng khi lấy nó ra chuyển lại thành Unicode thì không được.

HELP ME ! (code càng tốt). Thanhx.

knighthood
12-03-2003, 02:41
Bạn còn cần thông tin về tiếng Việt trong Java thì mail cho tôi

Nobboo
19-03-2003, 13:03
Bài viết được gửi bởi QuanN
Đây là 1 thí dụ đơn giản. Nếu chạy trên Win98 thì cần có JDK từ 1.4 trở lên vì 1.3.1 bị bug không hiển thị đầy đủ chữ Việt được; nếu trên WinNT/2K/XP thì 1.3.1 OK. System cần có font Arial compatible với Unicode.

Dùng VietIME (http://vietime.sf.net) để nhập chữ Việt vào Java source code. Có thể save source file trong UTF-8 để giữ chữ Việt trong nguyên dạng thay vì trong Unicode escape sequences \u***X nếu để default ASCII encoding.


/*
* VietGUI.java
*
* Created on February 27, 2003, 12:08 AM
*/

/**
*
* @author quan
*/
public class VietGUI extends javax.swing.JFrame {

/** Creates new form VietGUI */
public VietGUI() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem7 = new javax.swing.JMenuItem();

setTitle("VietGUI Example");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jTextArea1.setFont(new java.awt.Font("Arial", 0, 12));
getContentPane().add(jTextArea1, java.awt.BorderLayout.CENTER);

jMenu1.setFont(new java.awt.Font("Arial", 1, 12));
jMenu1.setLabel("H\u1ed3 S\u01a1");
jMenuItem1.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem1.setText("M\u1edbi");
jMenu1.add(jMenuItem1);

jMenuItem2.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem2.setText("M\u1edf");
jMenu1.add(jMenuItem2);

jMenuItem6.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem6.setText("L\u01b0u");
jMenu1.add(jMenuItem6);

jMenuBar1.add(jMenu1);

jMenu2.setText("Duy\u1ec7t B\u00e0i");
jMenu2.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem3.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem3.setText("Sao");
jMenu2.add(jMenuItem3);

jMenuItem4.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem4.setText("C\u1eaft");
jMenu2.add(jMenuItem4);

jMenuItem5.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem5.setText("D\u00e1n");
jMenu2.add(jMenuItem5);

jMenuBar1.add(jMenu2);

jMenu3.setFont(new java.awt.Font("Arial", 1, 12));
jMenu3.setLabel("Gi\u00fap \u0110\u1ee1");
jMenuItem7.setFont(new java.awt.Font("Arial", 1, 12));
jMenuItem7.setText("Gi\u1edbi Thi\u1ec7u");
jMenu3.add(jMenuItem7);

jMenuBar1.add(jMenu3);

setJMenuBar(jMenuBar1);

pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize ();
setSize(new java.awt.Dimension(400, 300));
setLocation((screenSize.width-400)/2,(screenSize.height-300)/2);
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new VietGUI().show();
}


// Variables declaration - do not modify
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuBar jMenuBar1;
// End of variables declaration

}
em ngu dốt, cho em hỏi dùng CT trên ra sao, ai bỏ chút thời gian chỉ em với

knighthood
20-03-2003, 02:30
Tiếng Việt trong Java cũng khá ngộ, tôi có cách tà đạo này hay lắm, ai quan tâm gởi mail cho tôi

dragon43
30-03-2003, 21:05
Chao ban QuanN va Knightgood!!!!
Lam on send cho minh bang ma Tieng Viet Trong Java viet theo cach cua chuong trinh tren nhe
mail to:------------- lightstar41@yahoo.com


Vi du bang ma nhu sau:
\u01b0=ư
\u01edf=ở
......

Cam on rat nhieu

QuanN
31-03-2003, 02:03
Bảng mã Việt Unicode có thể lấy từ http://vietunicode.sf.net.

Trong NetBeans/Forte, Form Editor sẽ tạo Unicode trong escape sequences trong Source Editor; còn JBuilder 8 thì Design giữ Unicode trong nguyên dạng, Unicode native format, cho source code. Để giữ Unicode native format (để có thể thấy tiếng Việt, thay vì escape sequences) ta cần save source file trong UTF8 encoding.