gpt4 book ai didi

java - 带 GUI 的应用程序服务器/客户端

转载 作者:行者123 更新时间:2023-11-30 04:55:11 25 4
gpt4 key购买 nike

我会用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

我的问题是:我应该在哪里实现服务器。在同一个 AziendaGUI 类中,或者我必须创建另一个类并在 AziendaGUI 的 main 中调用它?

最佳答案

有一个 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 类是由多个线程访问的,因此您必须使用某种形式的并发控制。

关于java - 带 GUI 的应用程序服务器/客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8777472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com