Q2. [20 points] In this question, you are required to complete the code of a simple GUI program that will help GUI desig

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Q2. [20 points] In this question, you are required to complete the code of a simple GUI program that will help GUI desig

Post by answerhappygod »

Q2 20 Points In This Question You Are Required To Complete The Code Of A Simple Gui Program That Will Help Gui Desig 1
Q2 20 Points In This Question You Are Required To Complete The Code Of A Simple Gui Program That Will Help Gui Desig 1 (75.56 KiB) Viewed 97 times
public
class GUIFrame extends JFrame
{
private JPanel mContentPane;
private JTextField txtGreen;
private JTextField txtBlue;
private JTextField txtRed;
public GUIFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
setResizable(false);
//initialize the JPanel
mContentPane =
new JPanel();
mContentPane.setLayout(null);
//set the JFrame's JPanel
setContentPane(mContentPane);
//(a) i ‐ Add btnDisplay JButton. Set the label to "Display"
//(a) ii ‐ Set its bounds to 33,
138, 117, 25
//(a) iii ‐ Add it to the JPanel of
the program
//(b)
//Register an anonymous object that
implements ActionListener
//and add it to the action listeners
of btnDisplay
//Make sure that when users click on
the button,
//the JPanel's background is changed
to the new color
txtRed = new
JTextField(); txtRed.setBounds(104, 10, 66, 30);
mContentPane.add(txtRed);
txtGreen =
new JTextField(); txtGreen.setBounds(104, 52, 66,
30); mContentPane.add(txtGreen);
txtBlue = new
JTextField(); txtBlue.setBounds(104, 94, 66, 30);
mContentPane.add(txtBlue);
JLabel lblRed = new
JLabel("RED"); lblRed.setBounds(23, 17, 70, 15);
mContentPane.add(lblRed);
JLabel lblGreen =
new JLabel("GREEN"); lblGreen.setBounds(23, 59,
70, 15); mContentPane.add(lblGreen);
JLabel lblBlue =
new JLabel("BLUE"); lblBlue.setBounds(23, 101, 70,
15); mContentPane.add(lblBlue);
//(d) Register your KeyListener to the
JTextFields in the program
}
//(c) Add an inner class that
implements KeyListener here}
Q2 20 Points In This Question You Are Required To Complete The Code Of A Simple Gui Program That Will Help Gui Desig 2
Q2 20 Points In This Question You Are Required To Complete The Code Of A Simple Gui Program That Will Help Gui Desig 2 (30.25 KiB) Viewed 97 times
Q2 20 Points In This Question You Are Required To Complete The Code Of A Simple Gui Program That Will Help Gui Desig 3
Q2 20 Points In This Question You Are Required To Complete The Code Of A Simple Gui Program That Will Help Gui Desig 3 (46.49 KiB) Viewed 97 times
Q2. [20 points] In this question, you are required to complete the code of a simple GUI program that will help GUI designers pick the correct RGB (Red, Green and Blue) values for a certain color. The program accepts three integer values for the red, green, and blue and then sets the background of the GUI to the specified color. Figure 1 shows the complete GUI and Figure 2 shows what happens when the user clicks the "Display" button. In Figure 2, the GUI background changes to the corresponding color. - х х RED RED 255 GREEN GREEN BLUE BLUE 0 Display Display Figure 1 Figure 2 The programmer's code below is incomplete. Read the code in order to complete its functionality in the questions that follow.

points] a) [3 points] At the location of the comment / (a) in the code, add the missing JButton "Display" [3 points) i. Declare the JButton btnDisplay and set its text to "Display". ii. Set its bounds to 33, 138, 117, 25. iii. Add it to the JPanel of the program. b) [7 You know the following: You can register action listeners to any addActionListener(ActionListener listener) Component by using the method: You can create Color objects using the constructor: Color(int red, int green, int blue) You can set the background on any Component using the instance method: setBackground (Color color) At the comment section //(b), register an anonymous object, with the button btnDisplay, that implements an ActionListener, overriding the required method. Your task is to make sure that when the user clicks on the button btnDisplay (from (a)), a color object is created using the RGB values in the text fields. Then, the JPanel background is set to this new color.

points) c) [7 At the // (c) comment in the code, write an inner class KeyHandler that implements the interface KeyListener. KeyListener interface is given below. Notice that, contrary to ActionListener, KeyListener interface has three methods instead of one. public interface KeyListener extends EventListener { public void kexIxpediKeyEvent e); public void kexpressed KeyEvent e); public void keyReleased KeyEvent e); } You want to make sure that whenever a user types anything in the textfields that is not a digit, an error dialog is displayed and the text in the textfield that has the problem is reset. You can achieve this by making sure that when a key typed event happens (a user types a key on the keyboard), the text in the TextField the user is typing in is parsable to integers. If they are not, then you should (a) display a JOptionPane dialog displaying the error message "Please enter a number" and (b) reset the text inside the TextFields. Some hints: 1. Use a try catch statement to determine whether you can parse the text or not. 2. A Number FormatException is thrown whenever the parse operation fails. 3. Use method JOptionPane.showMessageDialog(Component argo, Object message) to display the error message to your users. 4. KeyEvent has a method that will return the Component that fired it. The method is called getSourcel). For example: JTextField field = (ITextField) e.getsourse(); will return the text field that fired the method (e.g. txtGreen, txtBlue, or txtRed) d) [3 At the comment // (d), register the KeyHandler class with the three JTextField(s) of the program. Every component has a method addKeyListener(KeyListener 1) designated for that task.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply