UTF-8 support issue to Java Swing? [duplicate]

喜夏-厌秋 提交于 2020-01-02 10:17:12

问题


Possible Duplicate:
how to implement UTF-8 format in Swing application?

In Swing application I have the send button, one text area and a text field.

If I press the send button, I need to send the text from text field to text area

It's working fine in English But not in the local language...

package package1;

import java.awt.*;
import java.awt.event.*;
import java.io.UnsupportedEncodingException;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;

class AEvent extends JFrame implements ActionListener{

    private static final long serialVersionUID = 1L;
    JTextField tf;
    JTextArea area ;
    Border border;

    AEvent(){

        area = new JTextArea(200,200);
        area.setBounds(60,200,300,200);

        border = BorderFactory.createLineBorder(Color.BLACK);
        area.setBorder(border);

        tf=new JTextField();
        tf.setBounds(60,70,150,20);

        Button b=new Button("click me");
        b.setBounds(100,120,80,30);

        b.addActionListener(this);

        add(b);
        add(tf);
        add(area);

        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we){
                System.exit(0);
            }
        });

        setSize(600,600);
        setLayout(null);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e){
        String s = null;
        try {
            s = new String(tf.getText().getBytes(), "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        area.setText(s);
    }

    public static void main(String args[]){
        new AEvent();

    }
}

Please give some Idea or some code that will help me to solve this..


回答1:


This breaks if the the platform default encoding is not utf-8. tf.getText().getBytes() gives you the bytes in the platform default encoding. new String(tf.getText().getBytes(), "UTF-8") will create a corrupt string if the actual encoding of the bytes is not utf-8.

try

public void actionPerformed(ActionEvent e){
    area.setText(tf.getText());
}



回答2:


OK, so if you are still not convinced of Phillipe's answer, here is a fully working code which demonstrates the good answer. I tried it with accented characters and it works just fine. If it breaks, please indicate how.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

class AEvent implements ActionListener {

    private JTextField tf;
    private JTextArea area;
    private JFrame frame;

    protected void initUI() {
        frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        area = new JTextArea(30, 80);
        area.setEditable(false);
        area.setFocusable(false);
        tf = new JTextField();
        JButton b = new JButton("click me");
        b.addActionListener(this);

        JScrollPane scrollPane = new JScrollPane(area);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        frame.add(scrollPane, gbc);
        gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        frame.add(tf, gbc);
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        frame.add(b, gbc);
        frame.getRootPane().setDefaultButton(b);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
        tf.requestFocusInWindow();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        area.append(tf.getText() + "\n");
        tf.setText("");
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AEvent().initUI();
            }
        });
    }
}



回答3:


Why this?

String s = null;
try {
    s = new String(tf.getText().getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
area.setText(s);

Why not just:

area.setText(tf.getText());



回答4:


I tried to do do your objective with a local language it worked fine for me. Can you please specify which local language are you considering and on what IDE are you working?



来源:https://stackoverflow.com/questions/13815479/utf-8-support-issue-to-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!