UTF-8支持问题到Java Swing? [英] UTF-8 support issue to Java Swing?

查看:135
本文介绍了UTF-8支持问题到Java Swing?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何在Swing应用程序中实现UTF-8格式?

在Swing应用程序中,我有发送按钮,一个文本区域和一个文本字段。

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();

    }
}

请提供一些Idea或某些代码这将帮助我解决这个问题。

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

推荐答案

确定,所以如果你仍然不相信Phillipe的答案,完全工作代码,演示了好的答案。我尝试它与重音字符,它的工作很好。如果中断,请说明如何。

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();
            }
        });
    }
}

这篇关于UTF-8支持问题到Java Swing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆