Swing UI by Vlad Costel Ungureanu for Learn Stuff
User Interface Command Line Graphical User Interface (GUI) Tactile User Interface (TUI) Multimedia (voice) Intelligent (gesture recognition) 2
Making the User Interface Design Designing containment surface Creating and placing visual components Functionality Defining actions Binding actions to components Listening and managing events 3
Making the User Interface 4
Swing A simple JFrame class MyFrame extends JFrame { public MyFrame (String title) { super(title);... // in a main class MyFrame myframe = new MyFrame ( A new window!"); myframe.setvisible(true); 5
Swing Adding components // create and add a new Button myframe.add(new Button("Hello")); // create a panel Panel mypanel = new Panel(); // add components to the panel mypanel.add(new Label("Name:")); mypanel.add(new TextField()); // add panel to frame myframe.add(panel); 6
Swing Event Driven Programming Atomic JLabel, JButton, JCheckBox, JRadioButton, JToggleButton, JScrollBar, JSlider, JProgressBar, JSeparator Complex JTable, JTree, JComboBox, JSpinner, JList, JFileChooser, JColorChooser, JOptionPane Content Editing JTextField, JFormattedTextField, JPasswordField, JTextArea, JEditorPane, JTextPane Menus JMenuBar, JMenu, JPopupMenu, JMenuItem, JCheckboxMenuItem, JRadioButtonMenuItem Containers JPanel, JScrollPane, JSplitPane, JTabbedPane, JDesktopPane, JToolBar High Level JFrame, JDialog, JWindow, JInternalFrame, JApplet 7
Swing Layout Manager A layout manager is an object that: Controls dimension and positioning of elements Each container has associated a layout manager. Container may have different layout managers as default: Frames: BorderLayout Panes: FlowLayout 8
Swing Border Layout import java.awt.*; public class TestBorderLayout { public static void main ( String args []) { Frame f = new Frame (" Border Layout "); f.setlayout (new BorderLayout()); f.add(new Button(" Nord "), BorderLayout.NORTH ); f.add(new Button(" South"), BorderLayout.SOUTH ); f.add(new Button(" Est"), BorderLayout.EAST ); f.add(new Button(" West"), BorderLayout.WEST ); f.add(new Button(" Center"), BorderLayout.CENTER ); f.pack (); f.setvisible(true); 9
Swing BridBag Layout GridBagLayout gridbag = new GridBagLayout(); container.setlayout(gridbag); GridBagConstraints c = new GridBagConstraints(); //restrictions c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; gridbag.setconstraints(componenta, c); container.add(componenta); 10
Swing Event Driven Programming Event: Pressing a button Selection or focus change Value change Property Change Source: The component generating the event Listener: Intercepts events Consumes events. 11
Swing Events 12
Swing Event Listeners class MyFrame extends Frame { public MyFrame ( String title) { super (title); setlayout (new FlowLayout ()); setsize (200, 100) ; Button b1 = new Button ("OK"); Button b2 = new Button ("Cancel"); add(b1); add(b2); listener = new MyButtonListener (this); b1.addactionlistener ( listener ); b2.addactionlistener ( listener ); 13
Swing Event Driven Programming class MyButtonListener implements ActionListener { private MyFrame frame; public MyButtonListener (MyFrame frame) { this.frame = frame; // Metoda interfetei ActionListener public void actionperformed (ActionEvent e) { frame.settitle ( You pressed" + e. getactioncommand ()); 14
Assignments Create a small Swing application that offers the following functionalities: Login Window Login functionality Main Window Two tabs: event form and event list Event form functionality Event form list Persist events serialized in a file 15
THANK YOU! Vlad Costel Ungureanu ungureanu_vlad_costel@yahoo.com This is a free course from LearnStuff.ro not for commercial use 16