DOC PREVIEW
Penn CIT 591 - GUI building with Swing

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

GUI building with Swing Jan 14 2019 How to build a GUI Create a window in which to display things usually a JFrame for an application or a JApplet Use the setLayout LayoutManager manager method to specify a layout manager Create some Components such as buttons panels etc Add your components to your display area according to your chosen layout manager Write some Listeners and attach them to your Components Interacting with a Component causes an Event to occur A Listener gets a message when an interesting event occurs and executes some code to deal with it Display your window 2 Import the necessary packages The Swing components are in javax swing so you always need to import that for a Swing application Swing is built on top of AWT and uses a number of AWT packages including most of the layout managers so you need to import java awt Most listeners also come from the AWT so you also need to import java awt event A few listeners such as DocumentListener and ListSelectionListener are specific to Swing so you may need to import javax swing event For more complex GUIs there are additional java awt something and javax swing something packages that you may need to import 3 Make a Container For an application your container is typically a JFrame JFrame frame new JFrame JFrame frame new JFrame Text to put in title bar You can create a JFrame in your main class It s often more convenient to have your main class extend JFrame For an applet your main class must extend JApplet Once your application or applet is up and running it create and display various dialogs 4 Add a layout manager The most important layout managers are BorderLayout FlowLayout Creates a horizontal row or a vertical stack This can be a little weird to use GridBagLayout Components are put in a rectangular grid All areas are the same size and shape BoxLayout Components are added left to right top to bottom GridLayout Provides five areas into which you can put components This is the default layout manager for both JFrame and JApplet Too complex and a danger to your sanity SpringLayout New very flexible I don t have experience with this one yet 5 Add components to containers The usual command is container add component For FlowLayout GridLayout and BoxLayout this adds the component to the next available location For BorderLayout this puts the component in the CENTER by default For BorderLayout it s usually better to use container add component BorderLayout position position is one of NORTH SOUTH EAST WEST or CENTER 6 Create components JButton button new JButton Click me JLabel label new JLabel This is a JLabel JTextField textField1 new JTextField This is the initial text JTextField textField2 new JTextField Initial text columns JTextArea textArea1 new JTextArea Initial text JTextArea textArea2 new JTextArea rows columns JTextArea textArea3 new JTextArea Initial text rows columns JCheckBox checkbox new JCheckBox Label for checkbox JRadioButton radioButton1 new JRadioButton Label for button ButtonGroup group new ButtonGroup group add radioButton1 group add radioButton2 etc This is just a sampling of the available constructors see the javax swing API for all the rest 7 BorderLayout public class BorderLayoutExample extends JApplet public void init setLayout new BorderLayout add new JButton One BorderLayout NORTH add new JButton Two BorderLayout WEST add new JButton Three BorderLayout CENTER add new JButton Four BorderLayout EAST add new JButton Five BorderLayout SOUTH add new JButton Six 8 FlowLayout public class FlowLayoutExample extends JApplet public void init setLayout new FlowLayout add new JButton One add new JButton Two add new JButton Three add new JButton Four add new JButton Five add new JButton Six 9 GridLayout public class GridLayoutExample extends JApplet public void init setLayout new GridLayout 2 4 add new JButton One add new JButton Two add new JButton Three add new JButton Four add new JButton Five 10 BoxLayout public class BoxLayoutExample extends JApplet public void init Box box new Box BoxLayout Y AXIS add box box add new JButton One box add new JButton Two box add new JButton Three box add new JButton Four box add new JButton Five box add new JButton Six 11 Nested layouts A JPanel is both a JContainer and a Component Because it s a container you can put other components into it Because it s a component you can put it into other containers All but the very simplest GUIs are built by creating several JPanels arranging them and putting components possibly other JPanels into them A good approach is to draw on paper the arrangement you want then finding an arrangement of JPanels and their layout managers that accomplishes this 12 An example nested layout Container container new JFrame or JApplet JPanel p1 new JPanel p1 setLayout new BorderLayout p1 add new JButton A BorderLayout NORTH also add buttons B C D E JPanel p2 new JPanel p2 setLayout new GridLayout 3 2 p2 add new JButton F also add buttons G H I J K JPanel p3 new JPanel p3 setLayout new BoxLayout p3 BoxLayout Y AXIS p3 add new JButton L also add buttons M N O P container setLayout new BorderLayout container add p1 BorderLayout CENTER container add p2 BorderLayout SOUTH container add p3 BorderLayout EAST 13 Create and attach listeners JButton okButton new JButton OK okButton addActionListener new MyOkListener class MyOkListener implements ActionListener public void actionPerformed ActionEvent event code to handle okButton click A small class like this is often best implemented as an anonymous inner class 14 Using an anonymous inner class Instead of okButton addActionListener new MyOkListener class MyOkListener implements ActionListener public void actionPerformed ActionEvent event code to handle OK button click You can do this okButton addActionListener new ActionListener public void actionPerformed ActionEvent event code to handle OK button click Keep anonymous inner classes very short typically just a call to one of your methods as they can really clutter up the code 15 Suggested program arrangement 1 class SomeClass Declare components as instance variables JFrame frame Can also define them here if you prefer JButton button public static void main String args new SomeClass createGui Define components and attach listeners in a method void createGui frame new JFrame button new JButton OK frame add button uses default BorderLayout button addActionListener new MyOkListener Use an inner class as your listener class MyOkButtonListener implements ActionListener


View Full Document

Penn CIT 591 - GUI building with Swing

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Loading Unlocking...
Login

Join to view GUI building with Swing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view GUI building with Swing and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?