62,629
社区成员




package cjk_convert;
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Application1
{
boolean packFrame = false;
/**
* Construct and show the application.
*/
public Application1()
{
Frame1 frame = new Frame1();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
{
frame.pack();
} else
{
frame.validate();
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception)
{
exception.printStackTrace();
}
new Application1();
}
});
}
}
package cjk_convert;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.*;
import java.io.*;
public class Frame1 extends JFrame implements ActionListener,KeyListener
{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextPane jTextPane1 = new JTextPane();
JLabel jLabel2 = new JLabel();
JScrollPane jScrollPane2 = new JScrollPane();
JTextPane jTextPane2 = new JTextPane();
JCheckBox jCheckBox1 = new JCheckBox();
JButton jButton1 = new JButton();
private boolean m_fAutoTrans = false ;
public Frame1()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
private void jbInit()
{
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 320));
setTitle("CJK编码转换(\\u打头的编码)");
jLabel1.setText("请在下面输入需要转换的内容");
jLabel1.setBounds(new Rectangle(18, 12, 357, 20));
jScrollPane1.setBounds(new Rectangle(13, 31, 366, 93));
jLabel2.setText("转换结果输出");
jLabel2.setBounds(new Rectangle(14, 163, 363, 19));
jScrollPane2.setBounds(new Rectangle(13, 181, 366, 105));
jCheckBox1.setText("自动转换");
jCheckBox1.setBounds(new Rectangle(123, 127, 78, 23));
jCheckBox1.addActionListener(this);
jButton1.setBounds(new Rectangle(15, 127, 100, 23));
jButton1.setText("转换");
jButton1.addActionListener(this);
contentPane.add(jLabel1);
contentPane.add(jScrollPane1);
contentPane.add(jLabel2);
contentPane.add(jScrollPane2);
contentPane.add(jButton1);
contentPane.add(jCheckBox1);
jScrollPane2.getViewport().add(jTextPane2);
jScrollPane1.getViewport().add(jTextPane1);
jScrollPane1.addKeyListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(jCheckBox1))
{
m_fAutoTrans = jCheckBox1.isSelected() ;
} else
if (e.getSource().equals(jButton1))
{
convert() ;
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
if (e.isControlDown())
if (e.getKeyCode() == KeyEvent.VK_V)
if (m_fAutoTrans)
convert() ;
}
public void keyReleased(KeyEvent e)
{
}
public void convert()
{
String text = jTextPane1.getText() ;
jTextPane2.setText("");
try
{
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
PrintStream ps = new PrintStream(pos) ;
System.setOut(ps);
System.out.println(text);
//将文字流写入到剪贴板
byte[] b = new byte[pis.available()] ;
pis.read(b) ;
jTextPane2.setText(new String(b));
} catch (IOException ex)
{
}
}
}