2

可能重复:
如何在 Swing 应用程序中实现 UTF-8 格式?

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

如果我按下发送按钮,我需要将文本从文本字段发送到文本区域

它在英语中运行良好,但不是在当地语言中......

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

    }
}

请给出一些想法或一些代码来帮助我解决这个问题..

4

4 回答 4

1

好的,所以如果您仍然不相信 Phillipe 的答案,这里有一个完整的代码,它展示了一个很好的答案。我用重音字符试过它,它工作得很好。如果它坏了,请说明如何。

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();
            }
        });
    }
}
于 2012-12-11T08:54:29.497 回答
1

如果平台默认编码不是utf-8. tf.getText().getBytes()为您提供平台默认编码中的字节。new String(tf.getText().getBytes(), "UTF-8")如果字节的实际编码不是 ,将创建一个损坏的字符串utf-8

尝试

public void actionPerformed(ActionEvent e){
    area.setText(tf.getText());
}
于 2012-12-11T07:43:35.027 回答
0

为什么这个?

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

为什么不只是:

area.setText(tf.getText());
于 2012-12-11T07:44:30.820 回答
-1

我试图用当地语言完成你的目标,这对我来说很好。您能否指定您正在考虑使用哪种本地语言以及您在使用哪种 IDE?

于 2012-12-11T07:38:01.083 回答