windowBuilder1.1的使用方法

来源:互联网 发布:js中的this详解 编辑:程序博客网 时间:2024/06/05 02:03

这里以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
原创粉丝点击