package TestJText;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CJText extends JFrame {
Box boxH, boxV1, boxV2,boxV3;
public CJText(){
init();
setTitle("Unicode字符查询器");
setBounds(100, 100, 400, 100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
getContentPane().setBackground(Color.cyan);
setVisible(true);
}
void init() {
boxV2 = Box.createVerticalBox();
boxV3 = Box.createVerticalBox();
boxH = Box.createHorizontalBox();
JPanel jPanel = new JPanel();
jPanel.setLayout(new GridLayout(2,1,10,10));
jPanel.setBackground(Color.cyan);
JLabel jLabel = new JLabel(" 字符");
JLabel jLabel1 = new JLabel("Unicode码");
JTextField jTextField = new JTextField(10);
JTextField jTextField1 = new JTextField(10);
JButton button = new JButton("查询Unicode码");
JButton button1 = new JButton("查询字符");
jPanel.add(jLabel);
jPanel.add(jLabel1);
add(jPanel);
boxV2.add(jTextField);
boxV2.add(jTextField1);
boxV3.add(button);
boxV3.add(button1);
boxH.add(Box.createHorizontalStrut(10));
boxH.add(boxV2);
boxH.add(boxV3);
this.add(boxH);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jTextField.getText().length() == 0) {
jTextField.setText("<值为空重新输入>");
return;
}
char c = jTextField.getText().charAt(0);//得到第一个字符
jTextField1.setText("" + (int) c);//输出字符的Unicode码
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(jTextField1.getText().length()==0)
return;
jTextField.setText(""+(char)Integer.parseInt(jTextField1.getText()));//将数字转化为Unicode码对应的字符
}
});
}
}
package TestJText;
public class CJTextTest {
public static void main(String[] args) {
new CJText();
}
}
运行结果 :