Swing
Swing
Swing Components
• Swing is a collection of libraries that contains
primitive widgets or controls used for designing
Graphical User Interfaces (GUIs).
Component
Container
JComponent Window
JPanel Frame
JFrame
Using Swing Components
• Very simple, just create object from
appropriate class – examples:
– JButton but = new JButton();
– JTextField text = new JTextField();
– JTextArea text = new JTextArea();
– JLabel lab = new JLabel();
• Many more classes. Don’t need to know
every one to get started.
• See ch. 9 Hortsmann
Adding components
• Once a component is created, it can be added to a
container by calling the container’s add method:
}
Add second button/event
class SimpleGUI extends JFrame{
SimpleGUI(){
/* .... */
JButton but1 = new JButton(“Click me”);
JButton but2 = new JButton(“exit”);
MyActionListener al = new MyActionListener();
but1.addActionListener(al);
but2.addActionListener(al);
cp.add(but1);
cp.add(but2);
show();
}
}