//使用Swing组件显示一个整数的个、十、百位数字。

//使用JOptionPane消息框作为对话框。


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class DigitJFrame extends JFrame implements ActionListener

{

   private JTextField texts[];


   public DigitJFrame()

   {

       super("显示整数的各位数字");      

       this.setBounds(400,400,160,140);

       this.setResizable(false);                          //窗口大小不能改变

       this.setColor.lightGray);< /p>

       this.setDefaultCloseOperation(EXIT_ON_CLOSE);      //单击窗口关闭按钮时,结束程序运行

       this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));    //流布局且左对齐


       String labelstr[]={"整数","百位","十位","个位"};

       texts = new JTextField[labelstr.length];

       for (int i=0; i<texts.length; i++)

       {

           this.getContentPane().add(new JLabel(labelstr[i]));

           texts[i] = new JTextField(10);

           texts[i].setEditable(false);                   //只能显示,不允许编辑

           this.getContentPane().add(texts[i]);

       }

       texts[0].setEditable(true);                        //允许编辑

       texts[0].setText("123");

       texts[0].addActionListener(this);               //注册单击事件监听器

       this.actionPerformed(null);

       this.setVisible(true);

   }


   public void actionPerformed(ActionEvent e)             //文本行中单击回车键

   {

       try

       {

           int i = Integer.parseInt(texts[0].getText());

           texts[1].setText(""+(i / 100));                //百位

           texts[2].setText(""+(i/10 % 10));              //十位

           texts[3].setText(""+(i % 10));                 //个位

       }

       catch(NumberFormatException nfe)

       {

           JOptionPane.showMessageDialog(this,"\""+texts[0].getText()+"\" 不能转换成整数,请重新输入!");

       }

       finally{}

   }


   public static void main(String arg[])

   {

       new DigitJFrame();

   }

}