在java

时间:2016-04-27 17:51:21

标签: java user-interface logic

我很难将我的班级分成两个班级,现在我将GUI和逻辑一起放在一个班级中,我希望它是分开的,我对对象和类进行了阅读但我仍然不明白我是如何将它拆分为我的例子,我唯一的逻辑"在这个程序中有3个内部类,它们决定按钮的作用(删除记录,保存记录等......)

继承我的代码,有人能指出我正确的方向吗?

private String name;
    private String surname;
    private String phone;
    private String address;
    private String postcode;
    private String email;
    static String summary;
    private JPanel panel;
    private JPanel buttonspanel;
    private JPanel labelspanel;
    private JPanel tablepanel;
    private JFrame frame;
    private JLabel lblName;
    private JLabel lblEmail;
    private JLabel lblPostcode;
    private JLabel lblAddress;
    private JLabel lblSurname;
    private JLabel lblPhone;
    private JTextField txtName;
    private JTextField txtSurname;
    private JTextField txtPostcode;
    private JTextField txtEmail;
    private JTextField txtPhone;
    private JTextField txtAddress;
    private JButton btnSave;
    private JButton btnUpload;
    private JButton btnDelete;


    String columns[] = {"Name","Surname","Phone","Address","Postcode","Email"};



    private DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);       



    public AddressBook() {
        createForm();
        createLabels();
        createTextField();
        createButton();
        createTable();
        frame.add(panel);
        frame.setVisible(true);

    }
    public void createLabels(){
        lblName = new JLabel ("Name");
        lblName.setBounds(10, 30, 100, 20);
        labelspanel.add (lblName);

        lblSurname = new JLabel ("Surname");
        lblSurname.setBounds(10, 50, 100, 20);
        labelspanel.add (lblSurname);

        lblAddress = new JLabel ("Address");
        lblAddress.setBounds(10, 70, 100, 20);
        labelspanel.add (lblAddress);

        lblPhone = new JLabel ("Phone");
        lblPhone.setBounds(10, 90, 100, 20);
        labelspanel.add (lblPhone);

        lblPostcode = new JLabel ("Postcode");
        lblPostcode.setBounds(10, 110, 100, 20);
        labelspanel.add (lblPostcode);

        lblEmail = new JLabel ("Email");
        lblEmail.setBounds(10, 130, 100, 20);
        labelspanel.add (lblEmail);

    }

    public void createTextField(){

        txtName = new JTextField (null);
        txtName.setBounds(110, 30, 150, 20);
        labelspanel.add (txtName);

        txtSurname = new JTextField (null);
        txtSurname.setBounds(110, 50, 150, 20);
        labelspanel.add (txtSurname);

        txtAddress = new JTextField (null);
        txtAddress.setBounds(110, 70, 150, 20);
        labelspanel.add (txtAddress);

        txtPhone = new JTextField (null);
        txtPhone.setBounds(110, 90, 150, 20);
        labelspanel.add (txtPhone);

        txtPostcode = new JTextField (null);
        txtPostcode.setBounds(110, 110, 150, 20);
        labelspanel.add (txtPostcode);

        txtEmail = new JTextField (null);
        txtEmail.setBounds(110, 130, 150, 20);
        labelspanel.add (txtEmail);

    }

    public void createForm(){

        frame = new JFrame();
        frame.setTitle("Address Book"); 
        frame.setSize(800,800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel();
        buttonspanel = new JPanel();
        buttonspanel.setLayout(new FlowLayout());
        buttonspanel.setBorder (BorderFactory.createEtchedBorder ());
        labelspanel = new JPanel();
        labelspanel.setBorder (BorderFactory.createEtchedBorder ());
        labelspanel.setLayout(null);
        labelspanel.setPreferredSize(new Dimension (400,200));
        tablepanel = new JPanel();
        tablepanel.setLayout(new BorderLayout());
        panel.add(buttonspanel);
        panel.add(labelspanel);
        panel.add(tablepanel);

    }

    public void createButton(){
        btnSave = new JButton ("Save to a file");
        btnSave.addActionListener(new SaveHandler());  
        buttonspanel.add (btnSave);


        btnDelete = new JButton ("Delete from the table");
        btnDelete.addActionListener(new DeleteHandler());  
        buttonspanel.add (btnDelete);

        btnUpload = new JButton ("Add details to table");
        btnUpload.addActionListener(new AddHandler());  
        buttonspanel.add (btnUpload);



    }

    public void createTable(){
        model.setColumnIdentifiers(columns);
        JScrollPane scrollPane = new JScrollPane(table);
        tablepanel.add(scrollPane, BorderLayout.SOUTH);
    }

    class SaveHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==btnSave)
            {
                name = ("");
                surname = ("");
                phone = ("");
                address = ("");
                postcode = ("");
                email = ("");

                name = txtName.getText();
                surname = txtSurname.getText();
                phone = txtPhone.getText();
                address = txtAddress.getText();
                postcode = txtPostcode.getText();
                email = txtEmail.getText();

                summary = ("Name:" +name)+("Surname:" +surname)+("Phone:" + phone)+("Address:" + address)+("Postcode:" + postcode)+("Email:" + email);
                String saveFile = summary;

                try {
                    BufferedWriter reader = new BufferedWriter(new FileWriter(new File("userinfo.txt"), true));
                    reader.write(saveFile);
                    reader.newLine();
                    reader.close();
                    JOptionPane.showMessageDialog(frame, "The details were sucessfuly saved");

                } catch (IOException e1) {
                    JOptionPane.showMessageDialog(frame, "Something went wrong, please try again");
                }
            }
        }

    }


    class AddHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==btnUpload)
            {

                String nameRow = txtName.getText();
                String surnameRow = txtSurname.getText();
                String phoneRow = txtPhone.getText();
                String addressRow = txtAddress.getText();
                String postcodeRow = txtPostcode.getText();
                String emailRow = txtEmail.getText();
                Object[] row = {nameRow,surnameRow,phoneRow,addressRow,postcodeRow,emailRow};
                model.addRow(row);

            }

        }
    }

    class DeleteHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==btnDelete)
            {
                int i = table.getSelectedRow();
                if(i >= 0){
                    model.removeRow(i);
                }
            }
        }
    }



    public static void main(String[] args) {

        new AddressBook();

    }

1 个答案:

答案 0 :(得分:0)

    class GUI{      


     Jbutton btnsave;
     Jbutton Delete;
     jbutton update

     HandleAll btnHandler = new  HandleAll(); 

     btnSave.addActionListener(btnHandler);  
     Deletea.ddActionListener(btnHandler);
     update.addActionListener(btnHandler);


      class HandleAll implements ActionListener {

       public void actionPerformed(ActionEvent e) {
       if(e.getSource()==btnSave){
       //on click save button
       }else if(e.getSource()==Delete){
       //on click delete button
       }else if(e.getSource()==update){
      //on click update button
       }

       }
     }


}

你可以按照上面的方法来分离Gui的逻辑。顺便说一下,我把HandleAll类变成inner class。 HandleAll实现actionListner接口。按下此课程上的每个按钮actionperformed都可以拨打电话,但是选择e.getsource可以确定按下哪个按钮。这种方法