-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameGUI.java
More file actions
133 lines (105 loc) · 4.75 KB
/
NameGUI.java
File metadata and controls
133 lines (105 loc) · 4.75 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.awt.*;
class NameGUI extends JFrame implements ActionListener{
JTextField txtWord1;
JTextField txtWord2;
NameGUI(){
Container pane = getContentPane(); //the visible container of the JFRAME, need it later
//create a background panel to put things into, uses BorderLayout & FlowLayout Managers
JPanel panelMain = new JPanel(new BorderLayout());
JPanel panelTop = new JPanel(new FlowLayout());
JPanel panelMiddle = new JPanel(new FlowLayout());
JPanel panelBottom = new JPanel(new FlowLayout());
//create a button named btnSample with text Sample Button
JButton btnSample = new JButton("Sample Button");
JButton btnQuit = new JButton("Quit");
JButton btnSwap = new JButton("Swap");
//create sample textboxes
txtWord1 = new JTextField(15);
txtWord2 = new JTextField(15);
//create a label for display
JLabel lblSwap = new JLabel("Swap Words!");
//buttons need to say something(ActionCommand) to someone who's listening
btnSample.setActionCommand("sample"); //yours will have to be unique
btnSample.addActionListener(this);
btnQuit.setActionCommand("quit"); //your command will have to be unique
btnQuit.addActionListener(this);
btnSwap.setActionCommand("swap");
btnSwap.addActionListener(this);
//Add components to proper panels
panelTop.add(btnSample);
panelTop.add(btnQuit);
//panelMiddle.add(btnYourButton);
panelBottom.add(lblSwap);
panelBottom.add(btnSwap);
panelBottom.add(txtWord1);
panelBottom.add(txtWord2);
//Add individual panels to panelMain, applies to BORDERLAYOUT only
//PAGE_START is top of screen
//PAGE_END is bottom of screen
//LINE_START is right side of screen
//LINE_END is left side of screen
//CENTER is center
panelMain.add(panelTop,BorderLayout.PAGE_START);
panelMain.add(panelMiddle,BorderLayout.CENTER);
panelMain.add(panelBottom,BorderLayout.PAGE_END);
//created separate method to generate menu, not necessary, but cleaner
generateMenu();
//set the window size for the app
setSize(800,600);
//tells java what to do when the class object closes
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("NameGui Title (how original)");
//get visible container and add panelMain to it
//EVERYTHING has to be arranged and set before adding to ContentPane
getContentPane().add(panelMain);
//make sure you can actually see it, starts off false
setVisible(true);
}
private void generateMenu(){
//create an empty menu bar
JMenuBar menuBar = new JMenuBar();
//create a menu (file, edit, help, etc)
JMenu menuFile = new JMenu("File");
JMenu menuHelp = new JMenu("Help");
//create a menu item and set up its listeners, similar to buttons
JMenuItem miQuit = new JMenuItem("Quit");
miQuit.addActionListener(this);
miQuit.setActionCommand("quit");
JMenuItem miHelp = new JMenuItem("Help me");
miHelp.addActionListener(this);
miHelp.setActionCommand("help");
//put together the pieces
menuFile.add(miQuit);
menuHelp.add(miHelp);
menuBar.add(menuFile);
menuBar.add(menuHelp);
//add bar to this JFrame
setJMenuBar(menuBar);
}
public void actionPerformed(ActionEvent evt) {
//this method listens to the JFrame's events and performs appropriately
switch (evt.getActionCommand()){
case "sample":
JOptionPane.showMessageDialog(this,"A Sample message dialog box","A plain message",JOptionPane.PLAIN_MESSAGE);
break;
case "quit" :
System.exit(0);
break;
case "swap" :
String tempString;
tempString = txtWord1.getText();
txtWord1.setText(txtWord2.getText());
txtWord2.setText(tempString);
break;
case "help" :
JOptionPane.showMessageDialog(this,"There is no help for you.","Sorry",JOptionPane.WARNING_MESSAGE);
break;
}
}
public static void main(String [] args){
new NameGUI();
}
}