2012-01-08 9 views
1

Javaでguiを使用してアプリケーションサーバー/クライアントを作成しますが、クラスの構成方法がわかりません。私は、アプリケーションの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();}}); 
} 
} 

と私はここにReading from and Writing to a Socket

を説明するように、すべてのメソッドでServerSocketコンストラクタを実装する必要がありますので、今、このアプリケーションがサーバとして動作する必要があります私の質問は:私は同じクラスAziendaGUIまたは私は別のクラスを作成し、それをAziendaGUIのメインで呼び出す必要があるserver.inを実装する必要がありますか?

+3

私は常に、クライアントクラスを2つの別々のパッケージ、またはより良いプロジェクトでサーバークラスから分離します。確かにそれが些細なクライアント/サーバープロジェクトであれば、すべて単一のプロジェクトになる可能性があります。しかし、確かに別々のパッケージで。あなたのプロジェクトが "myproj"という名前を持っているとすれば、私はクライアントクラスを 'myproj.client'に入れ、サーバクラスは' myproj.server'に入れます。私は強く1つのクラスのすべてを持つことに反対することをお勧めします。 – DejanLekic

+0

しかし、私はどこからサーバークラスを実行しなければならないのか理解できませんでした。メインメソッドはどこですか? – Mazzy

+0

1つのJavaクラスからすべてを実行する場合は、サーバー用のスレッドとクライアント用の1つ以上のスレッドを生成する必要があります。コンストラクタにフラグを設定して、サーバやクライアントとして動作する場所をクラスで知ることができます。 – DejanLekic

答えて

0

ほとんどの場合、サーバーとクライアントはまったく異なるプロセスであり、1つのプロセス内で実行されません。通常、それらは異なるマシンで実行されます。このため、このセパレーションはアプリケーション設計にも表示する必要があります。サーバープロセスとクライアントプロセスを開始できるように、最低でもmain()メソッドを持つ2つのクラスが必要です。

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

"Seperation of Concern"という基本的な設計ガイドラインがあります。つまり、に属しているものすべてのものはです。コードユニットであり、同じコード単位で異なる性質または異なる動作を持つものを混在させないでください。 クライアントとサーバーの性質や動作が異なるため、それぞれ異なるクラスにする必要があります。 このデザインにより、最後のコードをもっと理解しやすくなります。

これらのカテゴリから抽象的なカテゴリを考えてみましょう。

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クラスに複数のスレッドがアクセスしていることを指摘します。そのため、何らかの形の同時実行制御を使用する必要があります。

関連する問題