Create a class named JPanelOptions that extends JPanel and whose constructor acc
ID: 3697831 • Letter: C
Question
Create a class named JPanelOptions that extends JPanel and whose constructor accepts two colors and a String. Use the colors for background and foreground to display the String. Create an application named JTeamColors with GridLayout. Display four JPanelOptions JPanels that show the names, in their team colors, of four of your favorite sports teams. Save the files as JPanelOptions.java and JTeamColors.java.
Goals:
Create a program that meets the above requirements and is consistent with textbook code.
Create a JFrame and add the setDefaultCloseOperation method
Add labels to the JFrame
Use the GridLayout Manager.
As usual, at the beginning of the program add the standard comments.
Explanation / Answer
//JTeamColors.java
import javax.swing.*;
import mypanel.*;
import java.awt.GridLayout;
import java.awt.Color;
public class JTeamColors {
public static void main(String[] args) {
//new MyFirstJPanel();
JFrame jf = new JFrame("title"); // or JFrame()
jf.setSize(300, 200); // width, height in pixels (required)
jf.setVisible(true); // (required)
jf.setTitle("New Title");
jf.setLocation(50, 100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new GridLayout(2, 2));
Color red = new Color(255,0,0);
Color green = new Color(0,255,0);
Color blue = new Color(0,0,255);
Color black = new Color(0,0,0);
Color white = new Color(255,255,255);
JPanelOption mfjp1=new JPanelOption(red,white,"INDIA");
JPanelOption mfjp2=new JPanelOption(red,black,"AMERICA");
JPanelOption mfjp3=new JPanelOption(blue,black,"CHINA");
JPanelOption mfjp4=new JPanelOption(blue,white,"AUSTRALIA");
//panel1.set[Preferred/Maximum/Minimum]Size()
jf.add(mfjp1);
jf.add(mfjp2);
jf.add(mfjp3);
jf.add(mfjp4);
}
}
//JPanelOption.java
package mypanel;
import javax.swing.*;
import java.awt.Color;
public class JPanelOption extends JPanel {
public JPanelOption(Color a, Color b,String str) {
// Create a JLabel object
JLabel label = new JLabel();
label.setText(str);
label.setOpaque(true);
label.setBackground(a);
label.setForeground(b);
// Create a JPanel object
JPanel panel = new JPanel();
this.add(label);
// Set the properties of the frame
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.