windowBuilder1.1的使用方法

这里以ChineseAmount.java为例,

1)新建一个JFrame文件

   打开Eclipse-->右击Package Explorer的空白区-->New / Other-->WindowBuilder / Swing Desinger-->JFrame-->Next-->命名“包和类”

   例如:package:cn.str.opera,  class:ChineseAmount

2)点击“ChineseAmount.java”下方的“Design”选项卡--》

  2.1)右击窗体--》Set Layout / Absolute Layout-->

  2.2)选择Component下的JLabel,并把JLabel放到窗体上,设置text为:"输入一段文字"

                                 JTextArea,并把JTextArea放到窗体上,设置Variable为:"ChineseArea"

                                 JButton,并把JButton放到窗体上,设置text为:"计算汉字"

                                 JTextField,并把JTextField放到窗体上,设置Variable为:"numField"

                                 JLabel,并把JLabel放到窗体上,设置text为:"个汉字"

 2.3)为按钮添加响应事件,右击JButton"计算数字"--》Add event handler--> action / actionPerformed

3)回到Source选择卡,

     3.1)为ChineseAmount.java添加两个属性:

             private JTextArea chineseArea; 

             private JTextField numField;

 

             chineseArea = new JTextArea();
             chineseArea.setBounds(115, 20, 300, 133);
             contentPane.add(chineseArea);

             numField = new JTextField();
             numField.setBounds(237, 194, 66, 21);
             contentPane.add(numField);
             numField.setColumns(10);

 

     3.2)为button编写响应事件,

JButton btnNewButton = new JButton("\u8BA1\u7B97\u6C49\u5B57");
		btnNewButton.<span style="color:#ff6666;">addActionListener</span>(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				<span style="color:#ff6666;">do_button_actionPerformed(e);
</span>			}
		});

  右击do_button_actionPerformed(e)-->Change method" private void do_button_actionPerformed(ActionEvent e)"

	private void do_button_actionPerformed(ActionEvent e) {
		String text = chineseArea.getText();//获取用户输入
		int amount = 0;//创建统计汉字的计数器
		for (int i = 0; i < text.length(); i++) {//遍历字符串每一个字符
			//使用正则表达式,判断字符是否为汉字编码
			boolean matches = Pattern.matches("^[\u4E00-\u9FA5]{0,}$", ""
					+ text.charAt(i));

			if (matches) {//如果是汉字
				amount++;//则累加
			}
		}
		numField.setText(amount + "");
	}

4)右击“ChineseAmount.java”--> run as / Java Application,就可以运行了。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanqima

一键三连,多多益善

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值