-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainApp.java
More file actions
75 lines (66 loc) · 2.47 KB
/
Copy pathMainApp.java
File metadata and controls
75 lines (66 loc) · 2.47 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainApp extends JFrame {
public MainApp() {
setTitle("Tree Visualization App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null); // Center the frame on the screen
initComponents();
}
private void initComponents() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 1)); // Menu with 4 rows
JButton btnBST = new JButton("BST Visualizer");
btnBST.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openProgram("BSTVisualizer");
}
});
panel.add(btnBST);
JButton btnAVLTree = new JButton("AVL Tree Visualizer");
btnAVLTree.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openProgram("AVLTreeVisualizer");
}
});
panel.add(btnAVLTree);
JButton btnBFS_DFS = new JButton("BFS/DFS Visualizer");
btnBFS_DFS.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openProgram("bfs_dfs");
}
});
panel.add(btnBFS_DFS);
// Add the panel to the frame
add(panel);
}
private void openProgram(String programName) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
Class<?> programClass = Class.forName(programName);
JFrame programInstance = (JFrame) programClass.newInstance();
programInstance.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Set custom close operation
programInstance.setVisible(true);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainApp().setVisible(true);
}
});
}
}