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
|
// CombineNameGUI.java - Generates "Last, First" from separate parts.
// Fred Swartz, 2004-04-14
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
///////////////////////////////////////////////////////////// CombineNameGUI
public class CombineNameGUI extends JFrame {
//=================================================== instance variables
private JTextField _fName = new JTextField(12);
private JTextField _lName = new JTextField(12);
private JTextField _combinedName = new JTextField(20);
//========================================================== constructor
public CombineNameGUI() {
//-- 1. Create or set attributes of components.
_combinedName.setEditable(false); // Don't let user change output.
JButton btn_combine = new JButton("Combine");
//--- 2. Add listener(s).
btn_combine.addActionListener(new CombineAction());
//--- 3. Create a panel and add components to it
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(new JLabel("First"));
p.add(_fName);
p.add(new JLabel("Last"));
p.add(_lName);
p.add(btn_combine);
p.add(new JLabel("Combined Name"));
p.add(_combinedName);
//-- 4. Set the content panel of window and perform layout.
this.setContentPane(p);
this.pack(); // Do layout.
}//end constructor
///////////////////////////////////// inner listenre class CombineAction
class CombineAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String first = _fName.getText();
String last = _lName.getText();
_combinedName.setText(last + ", " + first);
}//end actionPerformed
}//end inner class
}//end class CombineNameGUI
|