客户信息界面事件编程

//客户信息界面事件编程
//<Applet code="applet.class" height=195 width=406>
//</Applet>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class PanelTest extends JApplet//面板
{
JPanel panelObj;
public PanelTest()
{
panelObj=new JPanel();
getContentPane().add(panelObj);
}
}

class ComponentTest extends PanelTest//组件
{
JLabel labelName;
JLabel labelTelNo;
JLabel labelSex;
JLabel labelAge;
JTextField textName;
JTextField textTelNo;
JComboBox comboBoxObj;
JTextField textAge;
JButton buttonObj;
public ComponentTest()
{
labelName=new JLabel("客户名称:";);
labelTelNo=new JLabel("电话号码:";);
labelSex=new JLabel("性别:";);
labelAge=new JLabel("年龄:";);
textName=new JTextField(25);
textTelNo=new JTextField(15);
String ComboBoxStr[]={"男","女"};
comboBoxObj=new JComboBox(ComboBoxStr);
textAge=new JTextField(3);
buttonObj=new JButton("录入(W)";);
panelObj.add(labelName);
panelObj.add(textName);
panelObj.add(labelTelNo);
panelObj.add(textTelNo);
panelObj.add(labelSex);
panelObj.add(comboBoxObj);
panelObj.add(labelAge);
panelObj.add(textAge);
panelObj.add(buttonObj);
}
}

//布局
class LayoutManagerTest extends ComponentTest
{
GridBagLayout gbLayoutObj;//布局对象
GridBagConstraints gbcObj;//条件约束对象

public LayoutManagerTest()
{
gbLayoutObj=new GridBagLayout();
gbcObj=new GridBagConstraints();
panelObj.setLayout(gbLayoutObj);
//左边的文本标签布局
gbcObj.anchor=GridBagConstraints.EAST;
gbcObj.gridx=1;
gbcObj.gridy=1;
gbLayoutObj.setConstraints(labelName,gbcObj);
gbcObj.gridy=2;
gbLayoutObj.setConstraints(labelTelNo,gbcObj);
gbcObj.gridy=3;
gbLayoutObj.setConstraints(labelSex,gbcObj);
gbcObj.gridy=4;
gbLayoutObj.setConstraints(labelAge,gbcObj);

//右边的输入框布局
gbcObj.anchor=GridBagConstraints.WEST;
gbcObj.gridx=2;
gbcObj.gridy=1;
gbLayoutObj.setConstraints(textName,gbcObj);
gbcObj.gridy=2;
gbLayoutObj.setConstraints(textTelNo,gbcObj);
gbcObj.gridy=3;
gbLayoutObj.setConstraints(comboBoxObj,gbcObj);
gbcObj.gridy=4;
gbLayoutObj.setConstraints(textAge,gbcObj);

gbcObj.anchor=GridBagConstraints.EAST;
gbcObj.gridy=5;
gbLayoutObj.setConstraints(buttonObj,gbcObj);
}
}

//事件类
class EventTest extends LayoutManagerTest
{
ButtonAction actionObj;
public EventTest()
{
actionObj=new ButtonAction();
comboBoxObj.addActionListener(actionObj);
buttonObj.addActionListener(actionObj);
}
//采用内部类
class ButtonAction implements ActionListener //接口
{
int i=0;
String[][] data={
{"York","85778776","男","24"},
{" "," "," "," "},
{" "," "," "," "},
{" "," "," "," "},
{" "," "," "," "},
{" "," "," "," "}, 
};

DialogEvent dEObj;//DialogEvent又是一个内部类
JButton soures;
public void actionPerformed(ActionEvent e)//事件处理函数
{
Object obj=e.getSource();//获取当前产生事件的组件
//下面的一系列if用来保证数据的完整性
if(obj==comboBoxObj)
{
data[2]=(String)comboBoxObj.getSelectedItem();
getAppletContext().showStatus("你选择的客户性别为:"+data[2]);//状态栏显示的信息
return;
}
if(obj==buttonObj) 
soures=(JButton)e.getSource();//获取当前组件对象并强制转换为JButton类型
String strButton=soures.getText();//获取按钮上的文字 
if(strButton=="清除(E)";)
{
textName.setText("";);
textTelNo.setText("";);
textAge.setText("";);
soures.setText("录入(W)";);
}
if(strButton=="录入(W)";)
{
data[0]=textName.getText();
if(data[0].length()==0)
{
getAppletContext().showStatus("警告!客户姓名不能为空";);
return;
}
if(data[1].length()==0)
{
getAppletContext().showStatus("警告!客户电话不能为空";);
return;
}
data[2]=(String)comboBoxObj.getSelectedItem();
data[3]=textAge.getText();
if(data[3].length()==0)
{
getAppletContext().showStatus("警告!客户年龄不能为空";);
return;
}
else
{
int custIntAge;
custIntAge=Integer.parseInt(data[3]);
if(custIntAge<18||custIntAge>60)
{
getAppletContext().showStatus("警告!年龄只能在18~60岁之间";);
return;

}
//当所有数据正确录入后弹出对话框
dEObj=new DialogEvent(null,"对话框",true);//开始进入DialogEvent类构造对话框
dEObj.setSize(421,265);
dEObj.setVisible(true);
}
}

//对话框类
class DialogTest extends JDialog
{
public DialogTest(Frame frame,String title,boolean modal)
{
super(frame,title,modal);
}
}

class PanelTest extends DialogTest//面板
{
JPanel mainPanel;
JPanel subPanel1;
JPanel subPanel2;
public PanelTest(Frame frame,String title,boolean modal)
{
super(frame,title,modal);
mainPanel=new JPanel();
subPanel1=new JPanel();
subPanel2=new JPanel();
}
}

class ComponentTest extends PanelTest//对话框上的组件和布局
{
JLabel labelTable;
JTable table;
JScrollPane scrollTable;
JButton button3;
JButton button4;
JButton button5;
public ComponentTest(Frame frame,String title,boolean modal)
{
super(frame,title,modal);
BorderLayout borLayout=new BorderLayout();
//subPanel1上的布局
subPanel1.setLayout(borLayout);
labelTable=new JLabel("显示客户的所有信息";);
String[] names={"客户姓名","电话号码","性别","年龄"};
table=new JTable(data,names);
table.setEnabled(false);
Dimension dim=new Dimension(350,80);
table.setPreferredScrollableViewportSize(dim);
scrollTable=new JScrollPane(table);//表格和表格滚动条已绑定在一起了
subPanel1.add("North",labelTable);
subPanel1.add("Center",scrollTable);
mainPanel.add(subPanel1);
//subPanel2上的布局
FlowLayout fLayout;
fLayout=new FlowLayout(FlowLayout.RIGHT,10,15);
subPanel2.setLayout(fLayout);
button3=new JButton("修改(E)";);
button4=new JButton("接受(W)";);
button5=new JButton("保存(S)";);
subPanel2.add(button3);
subPanel2.add(button4);
subPanel2.add(button5);
mainPanel.add(subPanel2);
//mainPanel上的布局
GridBagLayout gbLayout=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
mainPanel.setLayout(gbLayout);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbLayout.setConstraints(subPanel1,gbc);
gbc.anchor=GridBagConstraints.NORTHEAST;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbLayout.setConstraints(subPanel2,gbc);
getContentPane().add(mainPanel); 
}
}//ComponentTest类结束

//对话框事件类,继承关系:DialogEvent→ComponentTest→PanelTest→DialogTest→JDialog
class DialogEvent extends ComponentTest
{
DialogButton dBObj;
public DialogEvent(Frame frame,String title,boolean modal)
{
super(frame,title,modal);
dBObj=new DialogButton();
button3.addActionListener(dBObj);
button4.addActionListener(dBObj);
}
//内部类,对话框上按钮的事件处理在此
class DialogButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();
if(obj==button3)
{
dEObj.setVisible(false);
getAppletContext().showStatus("修改窗口内容";);
}
if(obj==button4)
{
dEObj.setVisible(false);
soures.setText("清除(E)";);
getAppletContext().showStatus("保存窗口内容";);
i++;
}
}
}//DialogButton类结束
}//DialogEvent类结束
}//ButtonAction类结束
}

public class applet extends EventTest
{
public void init()
{
new applet();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值