By GokiSoft.com| 20:04 28/08/2021|
Java Advanced

[Share Code] Tìm hiểu đa ngôn ngữ - lập trình Java BT2380

#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lesson08;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        showMenu();
    }
    
    static void showMenu() {
//        Locale locale = new Locale("vi", "VN");
        //globals, globals_vi_VN, globals_en_US, globals_en_UK, ...
        //abc, abc_vi_VN, abc_en_US, abc_en_UK, ...
        Locale locale = Locale.getDefault();
        ResourceBundle bundle = ResourceBundle.getBundle("java2.lesson08.globals", locale);
        
        System.out.println(bundle.getString("option_book_input"));
        System.out.println(bundle.getString("option_book_display"));
        System.out.println(bundle.getString("option_book_search"));
        System.out.println(bundle.getString("option_book_sort"));
        System.out.println(bundle.getString("exit"));
        System.out.println(bundle.getString("option"));
    }
}


#globals.properties


# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
# key = value; key: duy nhat -> all languages, value: Thay doi theo cac ngon ngu khac nhau

option_book_input = 1. Input N book
option_book_display = 2. Display all book
option_book_search = 3. Search by ID
option_book_sort = 4. Sort by book name
exit = 5. Exit
option = Choose: 

fm_book_title = Input book detail information
fm_book_name = Name:
fm_book_price = Price:
fm_book_authorname = Author Name:
fm_save = Save
fm_del = Delete


#globals_vi_VN.properties


# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.

option_book_input = 1. Nhap thong tin N quyen sach
option_book_display = 2. Hien thi thong tin sach
option_book_search = 3. Tim kiem theo id
option_book_sort = 4. Sap xep theo ten sach
exit = 5. Thoat
option = Chon: 

fm_book_title = Nhap thong tin sach
fm_book_name = Ten:
fm_book_price = Gia:
fm_book_authorname = Tac Gia:
fm_save = Luu
fm_del = Xoa


#BookFrame.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lesson08;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

/**
 * 
 * @author Diep.Tran
 */
public class BookFrame extends javax.swing.JFrame {
    Locale locale;
    ResourceBundle bundle;

    /**
     * Creates new form BookFrame
     */
    public BookFrame() {
        initComponents();
        locale = Locale.getDefault();
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputPanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        priceLabel = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        authorNameLabel = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        saveBtn = new javax.swing.JButton();
        delBtn = new javax.swing.JButton();
        vnBtn = new javax.swing.JButton();
        enBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        inputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("NHAP THONG TIN SACH"));

        nameLabel.setText("Ten:");

        priceLabel.setText("Gia:");

        authorNameLabel.setText("Tac Gia:");

        saveBtn.setText("Luu");

        delBtn.setText("Xoa");

        javax.swing.GroupLayout inputPanelLayout = new javax.swing.GroupLayout(inputPanel);
        inputPanel.setLayout(inputPanelLayout);
        inputPanelLayout.setHorizontalGroup(
            inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inputPanelLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(inputPanelLayout.createSequentialGroup()
                        .addGap(140, 140, 140)
                        .addComponent(saveBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(delBtn))
                    .addGroup(inputPanelLayout.createSequentialGroup()
                        .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(authorNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                            .addComponent(priceLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(nameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(inputPanelLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, inputPanelLayout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(47, Short.MAX_VALUE))
        );
        inputPanelLayout.setVerticalGroup(
            inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inputPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(priceLabel)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(authorNameLabel)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveBtn)
                    .addComponent(delBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        vnBtn.setBackground(new java.awt.Color(255, 153, 0));
        vnBtn.setForeground(new java.awt.Color(255, 0, 51));
        vnBtn.setText("VN");
        vnBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vnBtnActionPerformed(evt);
            }
        });

        enBtn.setText("EN");
        enBtn.setToolTipText("");
        enBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(enBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(vnBtn)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vnBtn)
                    .addComponent(enBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inputPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void enBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enBtnActionPerformed
        // TODO add your handling code here:
        locale = Locale.getDefault();
        changeLanguage();
    }//GEN-LAST:event_enBtnActionPerformed

    private void vnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vnBtnActionPerformed
        // TODO add your handling code here:
        locale = new Locale("vi", "VN");
        changeLanguage();
    }//GEN-LAST:event_vnBtnActionPerformed

    void changeLanguage() {
        bundle = ResourceBundle.getBundle("java2.lesson08.globals", locale);
        
        Border blackline = BorderFactory.createTitledBorder(bundle.getString("fm_book_title"));
        inputPanel.setBorder(blackline);
        
        nameLabel.setText(bundle.getString("fm_book_name"));
        priceLabel.setText(bundle.getString("fm_book_price"));
        authorNameLabel.setText(bundle.getString("fm_book_authorname"));
        saveBtn.setText(bundle.getString("fm_save"));
        delBtn.setText(bundle.getString("fm_del"));
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BookFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BookFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BookFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BookFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BookFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel authorNameLabel;
    private javax.swing.JButton delBtn;
    private javax.swing.JButton enBtn;
    private javax.swing.JPanel inputPanel;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JButton saveBtn;
    private javax.swing.JButton vnBtn;
    // End of variables declaration//GEN-END:variables
}


#BookFrame.form


<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
  <Properties>
    <Property name="defaultCloseOperation" type="int" value="3"/>
  </Properties>
  <SyntheticProperties>
    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
    <SyntheticProperty name="generateCenter" type="boolean" value="false"/>
  </SyntheticProperties>
  <AuxValues>
    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
  </AuxValues>

  <Layout>
    <DimensionLayout dim="0">
      <Group type="103" groupAlignment="0" attributes="0">
          <Group type="102" attributes="0">
              <EmptySpace max="-2" attributes="0"/>
              <Group type="103" groupAlignment="0" attributes="0">
                  <Component id="inputPanel" alignment="0" max="32767" attributes="0"/>
                  <Group type="102" alignment="1" attributes="0">
                      <EmptySpace min="0" pref="0" max="32767" attributes="0"/>
                      <Component id="enBtn" min="-2" max="-2" attributes="0"/>
                      <EmptySpace max="-2" attributes="0"/>
                      <Component id="vnBtn" min="-2" max="-2" attributes="0"/>
                  </Group>
              </Group>
              <EmptySpace max="-2" attributes="0"/>
          </Group>
      </Group>
    </DimensionLayout>
    <DimensionLayout dim="1">
      <Group type="103" groupAlignment="0" attributes="0">
          <Group type="102" alignment="1" attributes="0">
              <EmptySpace pref="13" max="32767" attributes="0"/>
              <Group type="103" groupAlignment="3" attributes="0">
                  <Component id="vnBtn" alignment="3" min="-2" max="-2" attributes="0"/>
                  <Component id="enBtn" alignment="3" min="-2" max="-2" attributes="0"/>
              </Group>
              <EmptySpace max="-2" attributes="0"/>
              <Component id="inputPanel" min="-2" max="-2" attributes="0"/>
              <EmptySpace max="-2" attributes="0"/>
          </Group>
      </Group>
    </DimensionLayout>
  </Layout>
  <SubComponents>
    <Container class="javax.swing.JPanel" name="inputPanel">
      <Properties>
        <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
          <Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
            <TitledBorder title="NHAP THONG TIN SACH"/>
          </Border>
        </Property>
      </Properties>

      <Layout>
        <DimensionLayout dim="0">
          <Group type="103" groupAlignment="0" attributes="0">
              <Group type="102" alignment="0" attributes="0">
                  <EmptySpace min="-2" pref="22" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="0" attributes="0">
                      <Group type="102" attributes="0">
                          <EmptySpace min="-2" pref="140" max="-2" attributes="0"/>
                          <Component id="saveBtn" min="-2" max="-2" attributes="0"/>
                          <EmptySpace type="unrelated" max="-2" attributes="0"/>
                          <Component id="delBtn" min="-2" max="-2" attributes="0"/>
                      </Group>
                      <Group type="102" alignment="0" attributes="0">
                          <Group type="103" groupAlignment="1" max="-2" attributes="0">
                              <Component id="authorNameLabel" pref="110" max="32767" attributes="0"/>
                              <Component id="priceLabel" alignment="1" max="32767" attributes="0"/>
                              <Component id="nameLabel" alignment="0" max="32767" attributes="0"/>
                          </Group>
                          <Group type="103" groupAlignment="0" attributes="0">
                              <Group type="102" alignment="0" attributes="0">
                                  <EmptySpace max="-2" attributes="0"/>
                                  <Group type="103" groupAlignment="0" attributes="0">
                                      <Component id="jTextField2" min="-2" pref="327" max="-2" attributes="0"/>
                                      <Component id="jTextField1" min="-2" pref="327" max="-2" attributes="0"/>
                                  </Group>
                              </Group>
                              <Group type="102" alignment="1" attributes="0">
                                  <EmptySpace min="-2" pref="5" max="-2" attributes="0"/>
                                  <Component id="jTextField3" min="-2" pref="327" max="-2" attributes="0"/>
                              </Group>
                          </Group>
                      </Group>
                  </Group>
                  <EmptySpace pref="47" max="32767" attributes="0"/>
              </Group>
          </Group>
        </DimensionLayout>
        <DimensionLayout dim="1">
          <Group type="103" groupAlignment="0" attributes="0">
              <Group type="102" alignment="0" attributes="0">
                  <EmptySpace min="-2" pref="24" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="nameLabel" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jTextField1" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="priceLabel" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jTextField2" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace type="separate" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="authorNameLabel" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jTextField3" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace min="-2" pref="26" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="saveBtn" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="delBtn" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace max="32767" attributes="0"/>
              </Group>
          </Group>
        </DimensionLayout>
      </Layout>
      <SubComponents>
        <Component class="javax.swing.JLabel" name="nameLabel">
          <Properties>
            <Property name="text" type="java.lang.String" value="Ten:"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JTextField" name="jTextField1">
        </Component>
        <Component class="javax.swing.JLabel" name="priceLabel">
          <Properties>
            <Property name="text" type="java.lang.String" value="Gia:"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JTextField" name="jTextField2">
        </Component>
        <Component class="javax.swing.JLabel" name="authorNameLabel">
          <Properties>
            <Property name="text" type="java.lang.String" value="Tac Gia:"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JTextField" name="jTextField3">
        </Component>
        <Component class="javax.swing.JButton" name="saveBtn">
          <Properties>
            <Property name="text" type="java.lang.String" value="Luu"/>
          </Properties>
        </Component>
        <Component class="javax.swing.JButton" name="delBtn">
          <Properties>
            <Property name="text" type="java.lang.String" value="Xoa"/>
          </Properties>
        </Component>
      </SubComponents>
    </Container>
    <Component class="javax.swing.JButton" name="vnBtn">
      <Properties>
        <Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
          <Color blue="0" green="99" red="ff" type="rgb"/>
        </Property>
        <Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
          <Color blue="33" green="0" red="ff" type="rgb"/>
        </Property>
        <Property name="text" type="java.lang.String" value="VN"/>
      </Properties>
      <Events>
        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="vnBtnActionPerformed"/>
      </Events>
    </Component>
    <Component class="javax.swing.JButton" name="enBtn">
      <Properties>
        <Property name="text" type="java.lang.String" value="EN"/>
        <Property name="toolTipText" type="java.lang.String" value=""/>
      </Properties>
      <Events>
        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="enBtnActionPerformed"/>
      </Events>
    </Component>
  </SubComponents>
</Form>


Tags:

Liên kết rút gọn:

https://gokisoft.com/2380

Bình luận