Project • Just when you thought it was safe! AWT / Swing – Project 2 will be released tomorrow. – Will go over it on Thursday Introduction – Chat Room • Threads • GUIs • Networking CS3 - AWT/Swing 1 CS3 - AWT/Swing 2 Before we begin Plan for today • Current Plan: – Today: Intro to Swing / Basic components – Thursday: Project 2 • Any questions with threads? – Monday: Event Based Programming – Tuesday: Swing Event Model / Listeners – Thursday: GUI Design Principles CS3 - AWT/Swing 3 AWT / Swing slides courtesy Paul Tyman CS3 - AWT/Swing 4 Caveat The Abstract Windowing Toolkit • Next set of lecture will present how to • Since Java was first released, its user interface create GUIs in Java facilities have been a significant weakness – NOT how to create GOOD GUIs in Java. – The Abstract Windowing Toolkit (AWT) was part of the JDK form the beginning, but it really was not • GUI design falls in the discipline of Human sufficient to support a complex user interface Computer Interaction (HCI) • JDK 1.1 fixed a number of problems, and most notably, it introduced a new event model. It did • Will give GUI Design Tips end of next not make any major additions to the basic week. components CS3 - AWT/Swing 5 CS3 - AWT/Swing 6
Java Foundation Classes Swing • In April 1997, JavaSoft announced the Java • The Swing classes are used to build GUIs Foundation Classes (JFC). – Swing does not stand for anything – Swing is built on top of the 1.1/1.2 AWT libraries – a major part of the JFC is a new set of user • Swing makes 3 major improvements on the AWT interface components called Swing. – does not rely on the platform’s native components – it supports “Pluggable Look-and-Feel” or PLAF – it is based on the Model-View-Controller (MVC) Drag Java AWT Swing Accessibility And 2D Drop JFC JDK 1.2 AWT Swing CS3 - AWT/Swing 7 CS3 - AWT/Swing 8 GUI Packages Components • Swing • A GUI consists of different graphic • AWT – javax.accessibility Component objects which are combined – java.awt – javax.swing into a hierarchy using Container objects. – java.awt.color – javax.swing.colorchooser – java.awt.datatransfer • Component class – javax.swing.event – java.awt.event – javax.swing.filechooser – An abstract class for GUI components such as menus, – javax.swing.plaf buttons, labels, lists, etc. – java.awt.font – javax.swing.table • Container – java.awt.geom – javax.swing.text.html – java.awt.image – An abstract class that extends Component. Containers – javax.swing.tree can hold multiple components. – ... – ... CS3 - AWT/Swing 9 CS3 - AWT/Swing 10 Weighing Components Heavyweight Components • Sun make a distinction between lightweight and • Heavyweight components were unwieldy for two heavyweight components reasons – Equivalent components on different platforms do not – Lightweight components are not dependent on necessarily act alike. native peers to render themselves. They are – The look and feel of each component was tied to the coded in Java. host operating system – Heavyweight components are rendered by the • Almost all Swing components are lightweight host operating system. They are resources except managed by the underlying window manager. – JApplet, JFrame, JDialog, and JWindow CS3 - AWT/Swing 11 CS3 - AWT/Swing 12
Additional Swing Features Applets versus Applications • Using Swing it is possible to create two different • Swing also provides types of GUI programs – A wide variety of components (tables, trees, sliders, – Standalone applications progress bars, internal frame, …) • Programs that are started from the command line – Swing components can have tooltips placed over them. • Code resides on the machine on which they are run – Arbitrary keyboard events can be bound to components. – Applets – Additional debugging support. • Programs run inside a web browser • Code is downloaded from a web server – Support for parsing and displaying HTML based • JVM is contained inside the web browser information. • For security purposes Applets are normally prevented from doing certain things (for example opening files) • For now we will write standalone applications CS3 - AWT/Swing 13 CS3 - AWT/Swing 14 Creating a JFrame JFrames import javax.swing.*; • A JFrame is a Window with all of the public class SwingFrame { adornments added. public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); • A JFrame provides the basic building win.show(); } block for screen-oriented applications. } // SwingFrame JFrame win = new JFrame( “title” ); CS3 - AWT/Swing 15 CS3 - AWT/Swing 16 Creating a JFrame JFrame import javax.swing.*; • Sizing a Frame public class SwingFrame { – You can specify the size public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); • Height and width given in pixels win.setSize( 250, 150 ); • The size of a pixel will vary based on the resolution win.show(); of the device on which the frame is rendered } } // SwingFrame – The method, pack() , will set the size of the frame automatically based on the size of the components contained in the content pane • Note that pack does not look at the title bar CS3 - AWT/Swing 17 CS3 - AWT/Swing 18
JFrame Swing Components Layered pane • JFrame s have several panes: • JComponent Glass pane – JComboBox, JLabel, JList, JMenuBar, JPanel, Menu bar JPopupMenu, JScrollBar, JScrollPane, JTable, JTree, JInternalFrame, JOptionPane, JProgressBar, JRootPane, JSeparator, JSlider, JSplitPane, JTabbedPane, JToolBar, JToolTip, Content pane Jviewport, JColorChooser, JTextComponent, … • Components are placed in the content pane CS3 - AWT/Swing 19 CS3 - AWT/Swing 20 JLabels Hello World • JLabels are components that you can put import javax.swing.*; text into. public class SwingFrame { public static void main( String args[] ) { • When creating a label you can specify the JFrame win = new JFrame( "My First GUI Program" ); initial value and the alignment you wish to JLabel label = new JLabel( "Hello World" ); use within the label. win.getContentPane().add( label ); • You can use getText() and win.pack(); win.show(); setText() to get and change the value of } the label. } // SwingFrame lbl = new JLabel( ”text", JLabel.RIGHT ) ; CS3 - AWT/Swing 21 CS3 - AWT/Swing 22 JButtons Buttons import javax.swing.*; • JButton extends Component , displays public class SwingFrame { a string and delivers an ActionEvent for public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); each mouse click. JButton button = new JButton( "Click Me!!" ); • Normally buttons are displayed with a win.getContentPane().add( button ); border win.pack(); win.show(); • In addition to text, JButtons can also } } // SwingFrame display icons button = new JButton( ”text“ ) ; CS3 - AWT/Swing 23 CS3 - AWT/Swing 24
Components, Containers, and Layout Managers Layout Manager • Containers may contain components (which • Layout Manager means containers can contain containers!!). – An interface that defines methods for • All containers come equipped with a layout positioning and sizing objects within a manager which positions and shapes (lays container. Java defines several default out) the container's components. implementations of LayoutManager . • Much of the action in the AWT occurs • Geometrical placement in a Container is between components, containers, and their controlled by a LayoutManager object layout managers. CS3 - AWT/Swing 25 CS3 - AWT/Swing 26 Layout Managers Changing the Layout • Layouts allow you to format components on the • To change the layout used in a container you first screen in a platform independent way need to create the layout. • The standard JDK provides five classes that • Then the setLayout() method is invoked on the implement the LayoutManager interface: container is used to use the new layout. – FlowLayout – GridLayout JPanel p = new JPanel() ; p.setLayout( new FlowLayout() ); – BorderLayout – CardLayout • The layout manager should be established before – GridBagLayout any components are added to the container • Layout managers are defined in the AWT package CS3 - AWT/Swing 27 CS3 - AWT/Swing 28 FlowLayout Flow Layout import javax.swing.*; • FlowLayout is the default layout for the import java.awt.*; JPanel class. public class SwingFrame { public static void main( String args[] ) { • When you add components to the screen, they JFrame win = new JFrame( "My First GUI Program" ); flow left to right (centered) based on the order win.getContentPane().setLayout( new FlowLayout() ); added and the width of the screen. for ( int i = 0; i < 10; i++ ) • Very similar to word wrap and full justification on win.getContentPane().add( new JButton( String.valueOf( i ) ) ); a word processor. win.pack(); • If the screen is resized, the components' flow will win.show(); change based on the new width and height } } // SwingFrame CS3 - AWT/Swing 29 CS3 - AWT/Swing 30
Recommend
More recommend