事件5:小综合

来源:互联网 发布:淘宝订杂志 编辑:程序博客网 时间:2024/05/01 04:04
import javax.swing.*;
import java.awt.
*;
import java.awt.
event.*;

public class Customer extends JApplet
{
    
//Variable for the panel 
    JPanel panelObject;

      
//variables for labels
    JLabel labelCustName; 
    JLabel labelCustCellNo;
    JLabel labelCustPackage; 
    JLabel labelCustAge;

    
//variables for data entry controls 
    JTextField textCustName;  
    JTextField textCustCellNo;
    JComboBox comboCustPackage;
     JTextField textCustAge;

    
//Variables for the layout
    GridBagLayout gbObject;
    GridBagConstraints gbc;
    
    JButton submit ;
    JButton reStart;
    
public void init()
    
{
        
//Initialize the layout variables
        gbObject = new GridBagLayout();
        gbc 
= new GridBagConstraints();
        panelObject 
= (JPanel)this.getContentPane();
    
        panelObject.setLayout(gbObject);
            
            
//Initialize label controls 
        labelCustName = new JLabel("Customer Name");
        labelCustCellNo 
= new JLabel("Cell Number");
        labelCustPackage 
= new JLabel("Package");
        labelCustAge 
= new JLabel("Age");

        
//Initialize data entry controls
        textCustName = new JTextField(30);
        textCustCellNo 
= new JTextField(15);
            textCustAge 
= new JTextField(2);
        String packages[] 
= "-----------------","Executive""Standard"};
        comboCustPackage 
= new JComboBox(packages);
        
        submit 
= new JButton("Submit");
        reStart
=new JButton("reStart");
        
//Add controls for Customer Name 
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 1;
        gbc.gridy 
= 5;
        gbObject.setConstraints(labelCustName,gbc);
        panelObject.add(labelCustName);
    
        gbc.anchor 
= GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 4;
        gbc.gridy 
= 5;
        gbObject.setConstraints(textCustName,gbc);
        panelObject.add(textCustName);

        
//Add controls for Cell Number
        gbc.anchor = GridBagConstraints.NORTHWEST;    
        gbc.gridx 
= 1;
        gbc.gridy 
= 8;
        gbObject.setConstraints(labelCustCellNo,gbc);
        panelObject.add(labelCustCellNo);

        gbc.anchor 
= GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 4;
        gbc.gridy 
= 8;
        gbObject.setConstraints(textCustCellNo,gbc);
        panelObject.add(textCustCellNo);


        
//Add controls for Package
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 1;
        gbc.gridy 
= 11;
        gbObject.setConstraints(labelCustPackage,gbc);
        panelObject.add(labelCustPackage);

        gbc.anchor 
= GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 4;
        gbc.gridy 
= 11;
        gbObject.setConstraints(comboCustPackage,gbc);
        panelObject.add(comboCustPackage);

            
//Add controls for Customer Age
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 1;
        gbc.gridy 
= 14;
        gbObject.setConstraints(labelCustAge,gbc);
        panelObject.add(labelCustAge);

        gbc.anchor 
= GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 4;
        gbc.gridy 
= 14;
        gbObject.setConstraints(textCustAge,gbc);
        panelObject.add(textCustAge);

        gbc.anchor 
= GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 4;
        gbc.gridy 
= 17;
        gbObject.setConstraints(submit,gbc);
        panelObject.add(submit);
        
        gbc.anchor 
= GridBagConstraints.NORTHWEST;
        gbc.gridx 
= 8;
        gbc.gridy 
= 17;
        gbObject.setConstraints(reStart,gbc);
        panelObject.add(reStart);
        
        reStart.addActionListener(
new reStartAction());
        submit.addActionListener(
new validate());
    }

    
    
class validate implements ActionListener 
    
{
        
public void actionPerformed(ActionEvent e)
         
{
            
if( textCustName.getText().equals("")
               
|| textCustCellNo.getText().equals("")
             
|| textCustAge.getText().equals("") )  
             
{
                  JOptionPane.showMessageDialog(Customer.
this,"文本框不能有空");
              }

              
if(comboCustPackage.getSelectedIndex()<=0{
                  JOptionPane.showMessageDialog(Customer.
this,"组合框必须选择一项!");
              }

              
else
               JOptionPane.showMessageDialog(Customer.
this,comboCustPackage.getSelectedIndex());
              
                    
try{
                             Integer.parseInt( textCustCellNo.getText());
                    }

                    
catch(NumberFormatException ne) {
                        JOptionPane.showMessageDialog(Customer.
this,"学号必须是数字");
                    }

                    
try{
                             Integer.parseInt(textCustAge.getText());
                    }

                    
catch(NumberFormatException ne) {
                        JOptionPane.showMessageDialog(Customer.
this,"年龄必须是数字");
                    }

        }

    
    }

    
    
class reStartAction implements ActionListener{
        
public void actionPerformed(ActionEvent e){
            textCustName.setText(
"");
            textCustCellNo.setText(
"");
            textCustAge.setText(
"");
        }

    }

}

 
原创粉丝点击