应用程序服务器/客户端与gui

时间:2012-01-08 12:26:20

标签: java

我会在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();}});
}
}

现在这个应用程序应该作为一个服务器工作,所以我必须用Reading from and Writing to a Socket所述的所有方法实现ServerSocket构造函数

我的问题是:我应该在哪里实现server.in同一类AziendaGUI ora我必须创建另一个类并在AziendaGUI的主要部分调用它?

2 个答案:

答案 0 :(得分: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类,因此您必须使用某种形式的并发控制。

答案 1 :(得分: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
    }
}

有一条基本的设计指南,称为“关注分离”。这意味着,将属于一个事物的所有内容放在一个代码单元中,并且不要在同一代码单元中混合具有不同性质或不同行为的内容。 您的客户端和服务器具有不同的性质和不同的行为,因此它们需要位于不同的类中。 这种设计使得最终更容易理解代码......

尝试在抽象类别中思考并根据这些类别创建类。