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

Create graphics classes based on the Java Swing Library. myJPanel.java and other

ID: 639636 • Letter: C

Question

Create graphics classes based on the Java Swing Library. myJPanel.java and other classes as requested below. Because you cannot do much with a standard JPanel we need to create a smart version of JPanel. Once we have our own JPanel we can

-           add JButtons to it.
-           set different colors to the background.

For this lab:

-create your own version of a JPanel using inheritance
-Change our myJFrame.java so that it now creates your version of a JPanel instead of the standard JPanel.

Suggestions
In order to add a JButton to a JPanel:
JButton j1 = new JButton (

Explanation / Answer

// MyPanel.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// text panel

class textPanel extends JPanel {
// override the paintComponent method
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello Panel!", 75, 100);
} //paintComponent
} //class textPanel

class MyFrame extends JFrame {
public MyFrame(String s) {
   // Frame Parameters
   setTitle(s);
   setSize(300,200); // default size is 0,0
   setLocation(10,200); // default is 0,0 (top left corner)

   // Window Listeners
   addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
       dispose();
       System.exit(0); // exit is still needed after dispose()!
   } //windowClosing
   }); //addWindowLister

   // Add Panels
   Container contentPane = getContentPane();
   contentPane.add(new textPanel());
   contentPane.setBackground(Color.pink);
} //constructor MyFrame
} //class MyFrame

public class MyPanel {
public static void main(String[] args) {
JFrame f = new MyFrame("My Hello Panel!");
f.setVisible(true); // equivalent to f.show()!
} //main
} //class MyPanel


/* NOTES:
   WindowAdapter() is class that implements WindowListers
   with null methods for all the 7 methods of WindowListeners!
   It is found in java.awt.event.*.
*/

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