2016-04-27 96 views
1

我很難將課程分成兩個類,現在我在一個類中將GUI和邏輯全部組合在一起,我希望它是分開的,對象和類,但我仍然不明白我將如何分割它爲我的例子,我在這個程序中唯一的「邏輯」是3個內部類,它們確定按鈕的作用(刪除記錄,保存記錄等)從java中的GUI中分離邏輯

繼承人我的代碼,任何人都可以指向正確的方向嗎?

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

    } 
+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 
     } 

     } 
    } 


} 

你可以按照上面的方法來從桂單獨的邏輯。順便說一句,我已經使HandleAll類inner class。 HandleAll實現actionListner接口。在每個按鈕上按actionperformed在這個類將獲得通話,但通過選擇e.getsource你可以檢查哪個按鈕被按下exact.try這種做法

+0

對不起,如果我沒有意義,但我需要兩個單獨的.JAVA文件,一個用於處理GUI,另一個用於處理邏輯。我的問題是我不知道如何擁有不在同一個類中的按鈕的偵聽器。 – breadkun

+0

它是相同的概念。應該從其他類的按鈕引用,然後你可以做到這一點。 – Priyamal