📄 staticcharmethods.java
字号:
// StaticCharMethods.java
// Demonstrates the static character testing methods
// and case conversion methods of class Character
// from the java.lang package.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StaticCharMethods extends JFrame {
private char c;
//以下定义几个图形界面组件
private JLabel prompt;
private JTextField input;
private JTextArea outputArea;
public StaticCharMethods()
{
//super代表调用基类JFrame的构造方法,其结果是设置了窗口的标题栏
super( "Static Character Methods" );
//getContentPane是基类提供的方法,它获取窗体中可存放图形控件的空白区域
//所有的可容纳其它图形控件的区域被称为----"容器",它有一个基类,称为Container
Container container = getContentPane();
//设置布局方式为流布局方式:即从左到右地放置控件,一行放不下,就移到下一行
container.setLayout( new FlowLayout() );
prompt =
new JLabel( "Enter a character and press Enter" );
container.add( prompt ); //将控件加入容器中
input = new JTextField( 5 );
//设定文本框对回车键响应
input.addActionListener(
new ActionListener() {
//在下面的函数中定义要干的事
public void actionPerformed( ActionEvent e )
{
String s = e.getActionCommand();
c = s.charAt( 0 );
buildOutput(); //构造输出结果
}
}
);
container.add( input );
outputArea = new JTextArea( 10, 20 );
container.add( outputArea );
setSize( 300, 250 ); // set the window size
show(); // show the window
}
public void buildOutput()
{
outputArea.setText(
"is defined: " + Character.isDefined( c ) +
"\nis digit: " + Character.isDigit( c ) + //是否数字
"\nis Java letter: " +
Character.isJavaIdentifierStart( c ) + //是否可作为标识符的第一个字符
"\nis Java letter or digit: " +
Character.isJavaIdentifierPart( c ) + //是否可作为标识符的字符
"\nis letter: " + Character.isLetter( c ) +
"\nis letter or digit: " +
Character.isLetterOrDigit( c ) +
"\nis lower case: " + Character.isLowerCase( c ) +
"\nis upper case: " + Character.isUpperCase( c ) +
"\nto upper case: " + Character.toUpperCase( c ) +
"\nto lower case: " + Character.toLowerCase( c ) );
}
//程序入口点
public static void main( String args[] )
{
StaticCharMethods application = new StaticCharMethods();
//只有加上这一句,才能实现单击窗口关闭按钮关闭程序
application.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 ); //退出系统
}
}
);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -