Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 7 Lab GUI Applications Objectives • Be able to create a closeable window

ID: 3641329 • Letter: C

Question

Chapter 7 Lab
GUI Applications
Objectives
• Be able to create a closeable window
• Be able to create panels containing buttons
• Be able to use different layouts
• Be able to handle button events
Introduction
In this lab, we will be creating a graphical user interface (GUI) to allow the user to
select a button that will change the color of the center panel and radio buttons that will
change the color of the text in the center panel. We will need to use a variety of Swing
components to accomplish this task.
We will build two panels, a top panel containing three buttons and a bottom panel
containing three radio buttons. Layouts will be used to add these panels to the window
in the desired positions. A label with instructions will also be added to the window.
Listeners will be employed to handle the events desired by the user.
Our final GUI should look like the following

Task #1 Creating a GUI
1. Import the required Java libraries.
2. Create a class called ColorFactory that inherits from JFrame.
3. Create named constants for a width of 500 and height of 300 for the frame.
4. Write a default constructor that does the following
a) Set the title of the window to Color Factory.
b) Set the size of the window using the constants.
c) Specify what happens when the close button is clicked.
d) Get the content pane of the JFrame and set the layout manager to border
layout.
e) Call the method to build the top panel (to be written as directed below).
f) Add the panel to the north part of the content pane.
g) Call the method to build the bottom panel (to be written as directed below).
h) Add this panel to the south part of the content pane.
i) Create a label that contains the message “Top buttons change the panel
color and bottom radio buttons change the text color.”
j) Add this label to the center part of the content pane.
62 Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects

Task #2 Writing Private Methods
1. Write a private method that builds the top panel as follows
a) Create a panel that contains three buttons, red, orange, and yellow.
b) Use flow layout for the panel and set the background to be white.
c) The buttons should be labeled with the color name and also appear in that
color.
d) Set the action command of each button to be the first letter of the color
name.
e) Add button listener that implements action listener for each button.
2. Create a bottom panel in the same way as the top panel above, but use radio
buttons with the colors green, blue, and cyan.
Chapter 7 Lab GUI Applications 63

Task #3 Writing Inner Classes
1. Write a private inner class called ButtonListener that implements
ActionListener. It should contain an actionPerformed method to handle the button events. This event handler will handle all button events, so you must get the
action command of the event and write a decision structure to determine which
color to set the background of the content pane.

2. Write another private inner class called RadioButtonListener, similar to Button
listener. It will handle all radio button events, so you will need to check the
source of the event and write a decision structure to determine which color
should be used for the text of the message.

Task #4 Running the GUI Program
1. Write a main method that declares and creates one instance of a ColorFactory,
then use the setVisible method to show it on the screen.

Explanation / Answer

package cramster; import java.util.*; import java.awt.*; import javax.swing.*; public class Cramster { public static void main(String[] args) { NewJFrame wo = new NewJFrame(); wo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); wo.setSize(500, 300); wo.setVisible(true); } } package cramster; import java.awt.Color; import java.awt.Font; import javax.swing.border.MatteBorder; public class NewJFrame extends javax.swing.JFrame { private Color WHITE; public NewJFrame() { initComponents(); } @SuppressWarnings("unchecked") // private void initComponents() { jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jRadioButton3 = new javax.swing.JRadioButton(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Colour Factory"); jButton1.setText("red"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel1.add(jButton1); jButton1.setBackground(new java.awt.Color(255, 0, 0)); jButton2.setText("orange"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jPanel1.add(jButton2); jButton2.setBackground(new java.awt.Color(255, 153, 0)); jButton3.setText("yellow"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jPanel1.add(jButton3); jButton3.setBackground(new java.awt.Color(255, 255, 0)); jRadioButton1.setText("green"); jRadioButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton1ActionPerformed(evt); } }); jPanel2.add(jRadioButton1); jRadioButton1.setBackground(new java.awt.Color(255, 0, 0)); jRadioButton2.setText("blue"); jRadioButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton2ActionPerformed(evt); } }); jPanel2.add(jRadioButton2); jRadioButton3.setText("cyan"); jRadioButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton3ActionPerformed(evt); } }); jPanel2.add(jRadioButton3); jLabel1.setText("Top buttons change the panel color and bottom radio buttons change the text color."); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(93, 93, 93) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 122, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(29, 29, 29)) ); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jLabel1.setFont(new Font("Serif", Font.BOLD, 20)); jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255))); jLabel1.setForeground(new java.awt.Color(255, 255, 255)); jLabel1.setBackground(new java.awt.Color(0, 0, 0)); jLabel1.setBorder(new MatteBorder(5, 5, 5, 5, Color.WHITE)); jLabel1.setOpaque(true); pack(); }// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jPanel1.setBackground(new java.awt.Color(255, 0, 0)); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { jPanel1.setBackground(new java.awt.Color(255, 153, 0)); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { jPanel1.setBackground(new java.awt.Color(255, 255, 0)); } private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setForeground(new java.awt.Color(0, 255, 0)); } private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setForeground(new java.awt.Color(0, 0, 255)); } private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setForeground(new java.awt.Color(155, 155, 255)); } public static void main(String args[]) { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JRadioButton jRadioButton3; // End of variables declaration } PLEASE RATE ME LIFE SAVER