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

Write an application that implements a frame. Attach a panel to it that contains

ID: 3644139 • Letter: W

Question



Write an application that implements a frame. Attach a panel to it that contains an instance of JTextField (or, rather its extension IntField as discussed in class). When a user enters 1 into the JTextField, the color of the frame changes to yellow, if the user enters 2, the color changes to red, if the user enters 3, the color changes to blue. Set the original color to green. If a user enters any data different from 1,2,3, your application must throw an exception. You MUST create a special exception class (or classes) for this

Explanation / Answer

/* 002 The program demonstrates handling of events 003 It has 3 buttons, each buttons represent a different color 004 A push of a button changes the window background color 005 006 * 007 */ 008 009 import javax.swing.*; // Needed for the swing class 010 import java.awt.*; // Needed for color class 011 import java.awt.event.*; // Needed for event listener interface 012 013 public class ChangeBackground extends JFrame 014 { 015 JLabel msg; 016 JButton blackButton; 017 JButton yellowButton; 018 JButton greenButton; 019 JButton pinkButton; 020 JPanel panel; 021 022 public ChangeBackground() 023 { 024 // Sets the title of the window 025 026 setTitle("Change Background Window"); 027 028 // Sets the size of the window 029 030 setSize(230, 400); 031 032 // Sets the action when the window is close 033 034 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 035 036 // Creates a label that print on the window 037 038 msg = new JLabel("Click a button to change the color"); 039 040 // Creates the 4 button with names associate to it 041 042 blackButton = new JButton("Black"); 043 yellowButton = new JButton("Yellow"); 044 greenButton = new JButton("Green"); 045 pinkButton = new JButton("Pink"); 046 047 // Register an event listener with all the buttons 048 049 blackButton.addActionListener(new blackButtonListener()); 050 yellowButton.addActionListener(new yellowButtonListener()); 051 greenButton.addActionListener(new greenButtonListener()); 052 pinkButton.addActionListener(new pinkButtonListener()); 053 054 /* 055 Now we will create a panel 056 Then adds all the buttons to the panel & the label 057 058 * 059 */ 060 061 panel = new JPanel(); 062 panel.add(msg); 063 panel.add(blackButton); 064 panel.add(yellowButton); 065 panel.add(greenButton); 066 panel.add(pinkButton); 067 068 // Adds the panel to the content pane ( The JFrame window ) 069 070 add(panel); 071 072 // Sets the window to be visible 073 074 setVisible(true); 075 076 } 077 078 /* 079 Here handles the event when the user clicks the button 080 The background color will changes to black 081 082 * 083 */ 084 085 public class blackButtonListener implements ActionListener 086 { 087 public void actionPerformed(ActionEvent e) 088 { 089 // Sets the panel's background to black 090 091 panel.setBackground(Color.BLACK); 092 093 // Sets the label's text to blue 094 095 msg.setForeground(Color.BLUE); 096 097 } 098 } 099 100 // The background color changes to yellow 101 102 public class yellowButtonListener implements ActionListener 103 { 104 public void actionPerformed(ActionEvent e) 105 { 106 // Sets the panel's background to yellow 107 108 panel.setBackground(Color.YELLOW); 109 110 // Sets the label's text to red 111 112 msg.setForeground(Color.RED); 113 114 } 115 } 116 117 // The background color changes to green 118 119 public class greenButtonListener implements ActionListener 120 { 121 public void actionPerformed(ActionEvent e) 122 { 123 // Sets the panel's background to green 124 125 panel.setBackground(Color.GREEN); 126 127 // Sets the label's text to black 128 129 msg.setForeground(Color.BLACK); 130 131 } 132 } 133 134 // The background color changes to pink 135 136 public class pinkButtonListener implements ActionListener 137 { 138 public void actionPerformed(ActionEvent e) 139 { 140 // Sets the panel's background to pink 141 142 panel.setBackground(Color.PINK); 143 144 // Sets the label's text to red 145 146 msg.setForeground(Color.RED); 147 148 } 149 } 150 151 public static void main(String[] args) 152 { 153 new ChangeBackground(); 154 } 155 156 }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote