事件5:小综合

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(
"");
        }

    }

}

 
阅读更多
文章标签: class
个人分类: j2se技术
想对作者说点什么? 我来说一句

基本的arcgis api使用方法

2017年11月13日 25KB 下载

WeChat小程序事件

2017年11月14日 5KB 下载

JDBC连接MySQL的练习小项目

2011年07月07日 18KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭