没有合适的资源?快使用搜索试试~ 我知道了~
判断字符串是否是标识符
5星 · 超过95%的资源 需积分: 42 4 下载量 83 浏览量
2015-06-15
16:28:15
上传
评论
收藏 5KB TXT 举报
温馨提示
试读
6页
判断一个字符串是否是标识符,将字符串输出,带有异常处理:JAVA关键字不能作为标识符
资源推荐
资源详情
资源评论
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
public class WineJFrame extends JFrame implements CaretListener,ActionListener
{
private JTextField text_str,text_char,text_str1;
private JButton button_char,button_uni;
private int temp=0,i,t=0;
public WineJFrame()
{
super("标识符查询器");
this.setBounds(300,280,300,90);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3,3,3,3));
this.getContentPane().add(new JLabel("请输入字符串",JLabel.RIGHT));
text_str=new JTextField("abcd1234",22);
this.getContentPane().add(text_str);
text_str.addCaretListener(this);
button_char=new JButton("判断");
this.add(button_char);
button_char.addActionListener(this);
this.getContentPane().add(new JLabel("是否是标识符",JLabel.RIGHT));
text_char=new JTextField(22);
text_char.setHorizontalAlignment(JTextField.RIGHT);
text_char.setEditable(false);
this.getContentPane().add(text_char);
caretUpdate(null);
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
public class WineJFrame extends JFrame implements CaretListener,ActionListener
{
private JTextField text_str,text_char,text_str1;
private JButton button_char,button_uni;
private int temp=0,i,t=0;
public WineJFrame()
{
super("标识符查询器");
this.setBounds(300,280,300,90);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3,3,3,3));
this.getContentPane().add(new JLabel("请输入字符串",JLabel.RIGHT));
text_str=new JTextField("abcd1234",22);
this.getContentPane().add(text_str);
text_str.addCaretListener(this);
button_char=new JButton("判断");
this.add(button_char);
button_char.addActionListener(this);
this.getContentPane().add(new JLabel("是否是标识符",JLabel.RIGHT));
text_char=new JTextField(22);
text_char.setHorizontalAlignment(JTextField.RIGHT);
text_char.setEditable(false);
this.getContentPane().add(text_char);
caretUpdate(null);
this.setVisible(true);
button_uni=new JButton("输出标识符");
this.add(button_uni);
button_uni.addActionListener(this);
this.getContentPane().add(new JLabel("输出标识符",JLabel.RIGHT));
text_str1=new JTextField(22);
text_str1.setHorizontalAlignment(JTextField.RIGHT);
text_str1.setEditable(false);
this.getContentPane().add(text_str1);
caretUpdate(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ev)
{
if(ev.getSource()==button_char)
{
String str=text_str.getText();
if(str!=null&&str.length()>0)
{
char ch=str.charAt(0);
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z'||ch=='_'||ch=='$')
{
for(int i=1;i<str.length();i++)
{
ch=str.charAt(i);
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z'||ch=='_'||ch=='$'||ch>='0'&&ch<='9')
{
if(i==(str.length()-1))
button_uni=new JButton("输出标识符");
this.add(button_uni);
button_uni.addActionListener(this);
this.getContentPane().add(new JLabel("输出标识符",JLabel.RIGHT));
text_str1=new JTextField(22);
text_str1.setHorizontalAlignment(JTextField.RIGHT);
text_str1.setEditable(false);
this.getContentPane().add(text_str1);
caretUpdate(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ev)
{
if(ev.getSource()==button_char)
{
String str=text_str.getText();
if(str!=null&&str.length()>0)
{
char ch=str.charAt(0);
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z'||ch=='_'||ch=='$')
{
for(int i=1;i<str.length();i++)
{
ch=str.charAt(i);
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z'||ch=='_'||ch=='$'||ch>='0'&&ch<='9')
{
if(i==(str.length()-1))
剩余5页未读,继续阅读
檀一狼
- 粉丝: 7
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页