import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; import java.awt.Insets; import java.awt.Component; import java.awt.Font; import java.awt.Dimension; /* Author: James Tam Version: March 18, 2014 Concepts illustrated: 1) Container that handles events (button) so it can access graphical controls as the event occurs (rather than using the difficult to use swing accessors. 2) Swing components that include graphical icons (ImageIcon) 3) Changing the visual characteristics of swing components. 4) Using the layout manager class GridBagConstraints 5) Changing graphical attributes of components (color, image, font) 6) Using threads to pause a program */ public class MyFrame extends JFrame implements ActionListener { private static final String DEFAULT_TITLE = "Icon example by JT"; private static final String ENHANCED_TITLE = "GUI ENHANCED VERSION"; private static final int DEFAULT_WIDGET_WIDTH = 200; private static final int DEFAULT_WIDGET_HEIGHT = 20; private static final int PADDING = 10; public static final int DEFAULT_FRAME_WIDTH = 400; public static final int DEFAULT_FRAME_HEIGHT = 300; private JButton aButton; private JLabel aLabel; private GridBagLayout aLayout; private GridBagConstraints aConstraint; private boolean enhanced; public MyFrame() { enhanced = false; setTitle(DEFAULT_TITLE); aButton = new JButton("Press for enhanced version"); aButton.setSize(DEFAULT_WIDGET_WIDTH,DEFAULT_WIDGET_HEIGHT); aButton.addActionListener(this); aLabel = new JLabel("Go ahead press the button, I know you wanna"); aLabel.setSize(DEFAULT_WIDGET_WIDTH,DEFAULT_WIDGET_HEIGHT); this.setSize(DEFAULT_FRAME_WIDTH,DEFAULT_FRAME_HEIGHT); aLayout = new GridBagLayout(); aConstraint = new GridBagConstraints(); aConstraint.insets = new Insets(PADDING,PADDING,PADDING,PADDING); // TA: Comment out this line, recompile and show effect aConstraint.anchor = GridBagConstraints.WEST; this.setLayout(aLayout); this.addWidgets(); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent anEvent) { double width; double height; Dimension d; setTitle("Enhanced version activating"); enhanceButton(); enhanceLabel(); setTitle(ENHANCED_TITLE); // Sleep time = value in milliseconds try { Thread.sleep(2000); } catch (InterruptedException ex) { System.out.println("Program interupted when paused"); } setTitle(DEFAULT_TITLE); if (this.enhanced == false) // Doubles in size just once { d = this.getSize(); width = d.getWidth()*2; height = d.getHeight()*2; this.setSize((int) width, (int) height); } this.enhanced = true; } public void addWidget(Component c, int x, int y, int width, int height) { aConstraint.gridx = x; aConstraint.gridy = y; aConstraint.gridwidth = width; aConstraint.gridheight = height; aLayout.setConstraints(c,aConstraint); add(c); } public void addWidgets() { // JT: Local components as well as attributes can be // added. Don't need to access the locals in the event // listener method so they don't have to be attributes. addWidget(new JLabel("Labels useless control"),0,0,1,1); addWidget(aLabel,1,0,1,1); addWidget(new JButton("No event listener"),0,1,1,1); addWidget(aButton,1,1,1,1); } public void enhanceButton() { // Path is relative to the location of the source code ImageIcon buttonIcon = new ImageIcon("images/alert.png"); aButton.setIcon(buttonIcon); aButton.setBackground(Color.red); } public void enhanceLabel() { ImageIcon buttonIcon = new ImageIcon("images/logo.gif"); aLabel.setIcon(buttonIcon); Font aFont = new Font(Font.SANS_SERIF,Font.BOLD,14); aLabel.setFont(aFont); } }