1. Trang chủ
  2. » Giáo Dục - Đào Tạo

APJI lab4

9 54 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 9
Dung lượng 128 KB

Nội dung

APJI-Lab4-Advance Controls & I18N Application Programming I Module – Swing Advanced Controls Module – I18N Lab Guide for Session Session Objectives In this session, you will be practicing with  JList  JComboBox  JTable  JTree  JTabbedPane  JSplitPane  I18N Part I: Getting Started – 30 minutes Following code makes an application using JList (Figure 1)(30 minutes) © 2009 FPT-Aptech Page / APJI-Lab4-Advance Controls & I18N Figure Scan the code first, type the code, compile, run and observe the result package djava.module5; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListModel; public class TestJList extends javax.swing.JFrame { // Variables declaration private javax.swing.JButton btnAdd; private javax.swing.JList listNames; private javax.swing.JPanel pnHeader; private javax.swing.JScrollPane spList; private javax.swing.JTextField txtName; private DefaultListModel listModel = new DefaultListModel(); // End of variables declaration /** Creates new form TestJList */ public TestJList() { initComponents(); © 2009 FPT-Aptech Page / APJI-Lab4-Advance Controls & I18N //Add action listener to button btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = txtName.getText(); } } //text field is empty if (name==null || name.equalsIgnoreCase("")) { return; } //text field is not empty, listModel.addElement(name); txtName.setText(""); }); private void initComponents() { //Init Swing component pnHeader = new javax.swing.JPanel(); txtName = new javax.swing.JTextField(); btnAdd = new javax.swing.JButton(); spList = new javax.swing.JScrollPane(); listNames = new javax.swing.JList(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); txtName.setColumns(15); pnHeader.add(txtName); btnAdd.setText("Add"); pnHeader.add(btnAdd); getContentPane().add(pnHeader, java.awt.BorderLayout.PAGE_START); listNames.setModel(listModel); spList.setViewportView(listNames); getContentPane().add(spList, java.awt.BorderLayout.CENTER); } pack(); public static void main(String args[]) { new TestJList().setVisible(true); © 2009 FPT-Aptech Page / APJI-Lab4-Advance Controls & I18N } } Following code use Resource Bundle to create an application that enables changing language by changing locale(15 Minutes) Scan the code first, type the code, compile, run and observe the result import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container;Chú import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Locale; import java.util.ResourceBundle; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; public class VietnamesePopup { public static void main(String args[]) { // Get bundle Locale locale = new Locale("vi", "VN"); ResourceBundle bundle = ResourceBundle.getBundle("i18nstudy/popup", locale); // Get strings String ok = bundle.getString("ok"); String cancel = bundle.getString("cancel"); String yes = bundle.getString("yes"); String no = bundle.getString("no"); // Localize buttons for French UIManager.put("OptionPane.cancelButtonText", cancel); UIManager.put("OptionPane.noButtonText", no); UIManager.put("OptionPane.okButtonText", ok); UIManager.put("OptionPane.yesButtonText", yes); JFrame frame = new JFrame("Vietnamese Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Ask"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { © 2009 FPT-Aptech Page / APJI-Lab4-Advance Controls & I18N Component source = (Component) actionEvent.getSource(); int result = JOptionPane.showConfirmDialog(source, ResourceBundle.getBundle("i18nstudy/popup", new Locale("vi", "VN")).getString("question"), ResourceBundle.getBundle("i18nstudy/popup", new Locale("vi", "VN")).getString("choice"), JOptionPane.YES_NO_CANCEL_OPTION); if (result == JOptionPane.YES_OPTION) { System.out.println("Yes"); } else if (result == JOptionPane.NO_OPTION) { System.out.println("No"); } else if (result == JOptionPane.CANCEL_OPTION) { System.out.println("Cancel"); } else if (result == JOptionPane.CLOSED_OPTION) { System.out.println("Closed"); } } }; button.addActionListener(actionListener); Container contentPane = frame.getContentPane(); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); } } Create a file named “popup_vi_VN.properties” by an editor (if you use Netbeans, you can type directly the Vietnamese strings) #Button captions on dialog ok=Ch\u1EA5p nh\u1EADn cancel=H\u1EE7y b\u1ECF yes=\u0110\u1ED3ng \u00FD no=Kh\u00F4ng \u0111\u1ED3ng \u00FD #Another strings question=H\u1ECDc Swing th\u00FA v\u1ECB \u0111\u1EA5y ch\u1EE9? choice=L\u1EF1a ch\u1ECDn c\u00E2u tr\u1EA3 l\u1EDDi © 2009 FPT-Aptech Page / APJI-Lab4-Advance Controls & I18N Flowing program demostrate how to apply different format, language by using different Locale (15 Minutes) Scan the code first, type the code, compile, run and observe the result import import import import import import java.text.ChoiceFormat; java.text.Format; java.text.MessageFormat; java.text.NumberFormat; java.util.Locale; java.util.ResourceBundle; public class ChoiceFormatDemo { static void displayMessages(Locale currentLocale) { System.out.println("currentLocale = " + currentLocale.toString()); System.out.println(); ResourceBundle bundle = ResourceBundle.getBundle("ChoiceBundle", currentLocale); MessageFormat messageForm = new MessageFormat(""); messageForm.setLocale(currentLocale); double[] fileLimits = { 0, 1, }; String[] fileStrings = { bundle.getString("noFiles"), bundle.getString("oneFile"), bundle.getString("multipleFiles") } ; ChoiceFormat choiceForm = new ChoiceFormat(fileLimits, fileStrings); String pattern = bundle.getString("pattern"); Format[] formats = { choiceForm, null, NumberFormat.getInstance() }; messageForm.applyPattern(pattern); messageForm.setFormats(formats); Object[] messageArguments = { null, "XDisk", null }; for (int numFiles = 0; numFiles < 4; numFiles++) { messageArguments[0] = new Integer(numFiles); messageArguments[2] = new Integer(numFiles); String result = messageForm.format(messageArguments); System.out.println(result); } } static public void main(String[] args) { © 2009 FPT-Aptech Page / APJI-Lab4-Advance Controls & I18N } displayMessages(new Locale("en", "US")); System.out.println(); displayMessages(new Locale("fr", "FR")); } Create and copy following properties files: //File: ChoiceBundle.properties noFiles = are no files oneFile = is one file multipleFiles = are {2} files pattern = There {0} on {1} File: ChoiceBundle_fr_FR.properties # Choice.properties_fr_FR noFiles = n' y a pas des fichiers oneFile = y a un fichier multipleFiles = y a {2} fichiers pattern = Il {0} sur {1} //File: ChoiceBundle_en_US.properties # Choice.properties_en_US noFiles = are no files oneFile = is one file multipleFiles = are {2} files pattern = There {0} on {1} Part – Workshops (30 minutes) • • Quickly look at workshops for Module and Module for reviewing basic steps Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part – Lab Assignment (60 minutes) Do the assignment for Module and Module carefully Discuss with your class-mates and your instructor if needed Part – Do it your self Continue creating this form © 2009 FPT-Aptech Page / APJI-Lab4-Advance Controls & I18N Create a window with menu system like the Notepad application Convert menu of Notepad application in exercise into Vietnamese by using I18N Add more menu “Language” When user changes the language radion button, change language for all menu items like picture below © 2009 FPT-Aptech Page / APJI-Lab4-Advance Controls & I18N © 2009 FPT-Aptech Page /

Ngày đăng: 27/10/2019, 09:27

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w