-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLauncherGUI.java
More file actions
99 lines (80 loc) · 3.12 KB
/
LauncherGUI.java
File metadata and controls
99 lines (80 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// launcher GUI 구현
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class LauncherGUI {
private JFrame frame;
private JTextField hostField;
private JTextField portField;
public LauncherGUI() {
frame = new JFrame("Launcher");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4,1));
panel.add(new JLabel("서버 호스트 (클라이언트 모드용):"));
hostField = new JTextField("localhost");
panel.add(hostField);
panel.add(new JLabel("포트 번호 (기본:9999):"));
portField = new JTextField("9999");
panel.add(portField);
JPanel buttonPanel = new JPanel(new GridLayout(1,2));
JButton serverButton = new JButton("서버 모드");
JButton clientButton = new JButton("클라이언트 모드");
buttonPanel.add(serverButton);
buttonPanel.add(clientButton);
frame.add(panel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
serverButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
runServerMode();
}
});
clientButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
runClientMode();
}
});
}
private void runServerMode() {
final TransactionHistory transactionHistory = new TransactionHistory();
final AccountManager accountManager = new AccountManager(transactionHistory);
final TransactionManager transactionManager = new TransactionManager(transactionHistory);
int p = 9999;
try {
p = Integer.parseInt(portField.getText());
} catch (NumberFormatException ignored) {}
// p를 final로 받기 위해 finalPort로 변수 선언
final int finalPort = p;
// 별도 스레드로 서버 시작
new Thread(() -> {
NetworkServer server = new NetworkServer(finalPort, accountManager, transactionManager, transactionHistory);
try {
server.startServer();
} catch (IOException ex) {
ex.printStackTrace();
}
}).start();
JOptionPane.showMessageDialog(frame, "서버가 포트 " + finalPort + "에서 실행 중입니다.");
}
private void runClientMode() {
String host = hostField.getText();
int port = 9999;
try {
port = Integer.parseInt(portField.getText());
} catch (NumberFormatException ignored) {}
// 클라이언트 GUI 실행
FinancialManagementGUI gui = new FinancialManagementGUI(host, port);
gui.show();
}
public void show() {
frame.setVisible(true);
}
public static void main(String[] args) {
LauncherGUI launcher = new LauncherGUI();
launcher.show();
}
}