题目如图,代码都在下面窗体代码:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CustomerFrame extends JFrame {
private JLabel labCustomerID;
private JLabel labName;
private JLabel labAdress;
private JTextField txtCustomerID;
private JTextField txtName;
private JTextField textAdress;
private JButton btnFirst;
private JButton btnNext;
private JButton btnPrior;
private JButton btnLast;
private JButton btnUpdate;
private JButton btnAdd;
private JButton btnDelete;
//-----------------------------------------------------------------------------------------------------
public CustomerFrame() {
createComponents();
layoutComponents();
w();
setTitle("地址簿");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
//-----------------------------------------------------------------------------------------------------
private void createComponents() {
labCustomerID=new JLabel("雇员ID");
labName=new JLabel("姓名 ");
labAdress =new JLabel ("地址 ");
txtCustomerID=new JTextField(15);
txtName=new JTextField(15);
textAdress=new JTextField(38);
btnFirst=new JButton("第一条");
btnNext=new JButton("下一条");
btnPrior=new JButton("上一条");
btnLast=new JButton("最后一条");
btnUpdate=new JButton("修改");
btnAdd=new JButton("添加");
btnDelete=new JButton("删除");
}
//-----------------------------------------------------------------------------------------------------
private void layoutComponents() { //布局
Container c=this.getContentPane();
c.setLayout(new GridLayout(4, 1));
JPanel EmployeeIDPane=new JPanel(new FlowLayout(FlowLayout.LEFT));
EmployeeIDPane.add(labCustomerID);
EmployeeIDPane.add(txtCustomerID);
JPanel NamePane=new JPanel(new FlowLayout(FlowLayout.LEFT));
NamePane.add(labName);
NamePane.add(txtName);
JPanel AdressPane=new JPanel(new FlowLayout(FlowLayout.LEFT));
AdressPane.add(labAdress);
AdressPane.add(textAdress);
JPanel btnPane=new JPanel(new FlowLayout());
btnPane.add(btnFirst);
btnPane.add(btnNext);
btnPane.add(btnPrior);
btnPane.add(btnLast);
btnPane.add(btnUpdate);
btnPane.add(btnAdd);
btnPane.add(btnDelete);
c.add(EmployeeIDPane);
c.add(NamePane);
c.add(AdressPane);
c.add(btnPane);
}
//-----------------------------------------------------------------------------------------------------
private class btnFirst implements ActionListener {
public void actionPerformed(ActionEvent e) {
CustomerDB a=new CustomerDB();
CustomerDB.GetFirstCustomer();
}
}
private class btnNext implements ActionListener {
public void actionPerformed(ActionEvent e) {
CustomerDB c=new CustomerDB();
CustomerDB.GetNextCustomer();
}
}
private class btnPrior implements ActionListener {
public void actionPerformed(ActionEvent e) {
CustomerDB d=new CustomerDB();
CustomerDB.GetPrevCustomer();
}
}
private class btnLast implements ActionListener {
public void actionPerformed(ActionEvent e) {
CustomerDB g=new CustomerDB();
CustomerDB.GetLastCustomer();
}
}
private class btnUpdate implements ActionListener {
public void actionPerformed(ActionEvent e) {
CustomerDB f=new CustomerDB();
txtCustomerID.setText("");
txtName.setText("");
textAdress.setText("");
}
}
private class btnAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
Customer b=new Customer();
txtCustomerID.setText(""+b.getID());
txtName.setText(""+b.getName());
textAdress.setText(""+b.getAddress());
}
}
private class btnDelete implements ActionListener {
public void actionPerformed(ActionEvent e) {
Customer h=new Customer();
CustomerDB.DeleteCustomer(h);
}
}
//-----------------------------------------------------------------------------------------------------
private void w() {
btnFirst h1 = new btnFirst();
btnFirst.addActionListener(h1);
btnNext h2 = new btnNext();
btnNext.addActionListener(h2);
btnAdd h3 = new btnAdd();
btnAdd.addActionListener(h3);
btnUpdate h4 = new btnUpdate();
btnUpdate.addActionListener(h4);
btnDelete h5 = new btnDelete();
btnDelete.addActionListener(h5);
btnPrior h6 = new btnPrior();
btnPrior.addActionListener(h6);
btnLast h7 = new btnLast();
btnLast.addActionListener(h7);
}
//-----------------------------------------------------------------------------------------------------
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
CustomerFrame frame=new CustomerFrame();
frame.setVisible(true);
}
}这是地址簿的代码:package ch08;
public class Customer {
private String id ;
private String name ;
private String address ;
public Customer(){};
public Customer(String ID , String Name , String Adress){
id = ID;
name = Name;
address =Adress;
}
public String getID() {
return id;
}
public void SetID(String ID){
id= ID;
}
public String getName() {
return name;
}
public void SetName(String Name){
name = Name;
}
public String getAddress() {
return address;
}
public void SetAddress( String Address){
address = Address;
}
}
//-------------------------------------------------------------------------------------------------------------
package ch08;
import java.util.ArrayList;
public class CustomerDB {
private static ArrayList CustomerList = new ArrayList();
private static int i = 0;
public static void AddCustomer( Customer aCustomer){
CustomerList.add(aCustomer);
}
public static void DeleteCustomer(Customer aC){
for(int i = 0; i< CustomerList.size();i++){
Customer aCustomer = (Customer)CustomerList.get(i);
if (aC.getID().equals(aCustomer.getID())) {
CustomerList.remove(i);
break;
}
}
}
public static Customer GetNextCustomer() {
if (i < CustomerList.size() - 1 )
i = i + 1;
return (Customer)CustomerList.get(i);
}
public static Customer GetPrevCustomer() {
if (i > 0 )
i = i - 1;
return (Customer)CustomerList.get(i);
}
public static Customer GetFirstCustomer() {
i = 0;
return (Customer)CustomerList.get(i);
}
public static Customer GetLastCustomer(){
i = CustomerList.size() - 1;
return (Customer)CustomerList.get(i);
}
}按了按钮总是报错,我已经混乱了,求大神指点,
添加回答
举报
0/150
提交
取消