2012-01-08 187 views
1

我會用java中的gui創建一個應用服務器/客戶端,但我還沒有清楚如何組織這個類。我創建的應用程序界面:gui的應用服務器/客戶端

這裏是代碼

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

import java.util.Collection; 

public class AziendaGUI implements ActionListener { 

private JButton view_list; 
private JButton save_list; 
private JTextArea text_area; 
private JScrollPane scrollpane; 
private JPanel pane; 

private JFrame frame; 
private GridBagLayout grid; 

private Azienda company; 

public AziendaGUI() { 

    company = new Azienda(); 

    frame = new JFrame("Immobiliari s.p.a"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    view_list = new JButton("View Property"); 
    view_list.setActionCommand("view_list"); 
    view_list.addActionListener(this); 

    save_list = new JButton("Save List"); 
    save_list.setActionCommand("save_list"); 
    save_list.addActionListener(this); 

    text_area = new JTextArea(); 
    scrollpane = new JScrollPane(text_area); 
    scrollpane.setPreferredSize(new Dimension(250,350)); 

    grid = new GridBagLayout(); 
    pane = new JPanel(grid); 

    /* Set Constraints view_list button */ 
    grid.setConstraints(view_list, new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)); 
    pane.add(view_list); 

    /* Set Constraints save_list button */ 
    grid.setConstraints(save_list,new GridBagConstraints(1,0,1,1,0.0,0.0,GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)); 
    pane.add(save_list); 

    /* Set Constraint text area */ 
    grid.setConstraints(scrollpane, new GridBagConstraints(0,1,2,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)); 
    pane.add(scrollpane); 

    frame.setLayout(new FlowLayout()); 
    frame.add(pane); 

    frame.pack(); 
    frame.setVisible(true); 
} 

private void viewList(Collection<Immobile> list){ 

    text_area.setText(""); //Evita che venga ripetuto tutto il contenuto 

    for(Immobile imb : list){ 

     text_area.append(imb.toString()+"\n"); 
    } 
} 

private void store(){ 

    String file_name = JOptionPane.showInputDialog("Inserisci il nome del file"); 

    company.store(file_name); 
} 

@Override 
public void actionPerformed(ActionEvent e){ 

    String s = e.getActionCommand(); 

    if(s.equals("view_list")){ 

     viewList(company.getImmobili()); 
    } 
    if(s.equals("save_list")){ 

     store(); 
    } 
} 


public static void main(String[] args) { 

    SwingUtilities.invokeLater(new Runnable(){@Override 
               public void run(){new AziendaGUI();}}); 
} 
} 

現在這個應用程序應該工作作爲服務器,所以我必須與所有的方法來實現的ServerSocket構造如下Reading from and Writing to a Socket

解釋我的問題是:我應該在哪裏實現server.in在同一個類AziendaGUI ora我必須創建另一個類,並在AziendaGUI的主要調用它?

+3

我總是將服務器類中的客戶端類分成兩個獨立的包,甚至更好的項目。當然,如果它是一個微不足道的客戶端/服務器項目,所有這些都可以是單個項目。但絕對在單獨的軟件包中。假設您的項目名稱爲「myproj」,我會將客戶端類放入'myproj.client'中,並將服務器類放入'myproj.server'中。我強烈建議不要在一堂課中全部參加。 – DejanLekic 2012-01-08 12:31:14

+0

但我不明白從哪裏運行服務器類。哪裏應該是'主要方法'? – Mazzy 2012-01-08 12:39:08

+0

如果您想要從單個Java類完成所有任務,則必須爲服務器生成一個線程,併爲客戶端生成一個或多個線程。你可能會想在你的構造函數中有一個標誌,所以這個類知道在哪裏作爲服務器或客戶端。 – DejanLekic 2012-01-08 12:48:22

回答

0

在大多數情況下,服務器和客戶端是完全不同的進程,不在一個進程內運行......他們通常甚至運行在不同的機器上。所以這種分離也應該在應用程序設計中可見。最低限度,您需要兩個類,並使用它們自己的main()方法,以便可以啓動服務器進程和客戶端進程。

public Class Server { 
    public static void main(...) { 
     //Start a Thread that opens ServerSocket and listen for requests. 
    } 
} 


public Class Client { 
    public static void main(...) { 
     //Start your GUI and conect to the Server 
    } 
} 

有一個基本的設計指導線稱爲:「分離關注」。這意味着,把所有的東西,屬於一個事情一個代碼單元和不混合的東西,具有不同的性質,或在相同的代碼單元不同的行爲。 你的客戶和你的服務器有不同的性質和不同的行爲,所以他們需要在不同的類別中。 這種設計使得最終理解代碼變得更容易...

嘗試在抽象類別中思考並從這些類別創建您的類。

2

擁有Main類,Server類,Client類和GUI類。然後你給你的服務器和客戶端類提供你的GUI類的引用,以便在需要時更新GUI。

以下是服務器的外觀示例代碼。

public class AziendaGUI { 
    // GUI objects 
    private JFrame someWindow; 

    public AziendaGUI() { 
     // create and display the GUI 
    } 

    // export public methods for various GUI updates you want your 
    // server class to perform 

    public someGUIupdate(String s) { 
     // update the GUI 
     // for example, add some text to a textbox 

     // keep in-mind that this code is being run on the 
     // server thread and NOT the event dispatch thread 
     // so you need to consider concurrency issues 

     // you will need to use either SwingUtilities.invokeLater() 
     // or synchronized() 
    } 
} 

public class Server { 
    private AziendaGUI gui; 

    public Server(AziendaGUI gui) { 
     this.gui = gui; 
    } 

    public start() { 
     // start server threads 

     // when you want to update the GUI 
     gui.someGUIupdate("hello world"); 
     // these calls will probably be in other methods in your server class 
     // that do the actual IO handling 
    } 
} 

public class Main { 

    Main() { 
      // create and display GUI 
      AziendaGUI gui = new AziendaGUI(); 

      // create and start server 
      Server s = new Server(gui); 
      s.start(); 
    } 

    public static void main(String args[]) { 
     Main m = new Main(); 
    } 
} 

大多數人可能會把這些類放在單獨的文件中。

同樣,我想點出了GUI類是被多個線程訪問,所以你必須使用某種形式的併發控制。