0

I'm beginner of java and I study about JFrame now but I have an issue. I made a constructor with parameters of JPanel but when I invoke it with arguments, an error is happened. Could you help me to find any solutions??

import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;

public class JFlexiblePanel extends JFrame{

    private Color col1;
    private Color col2;
    private Font font1;
    private String str;
    private JPanel panel1;
    private JLabel label1;

    public JFlexiblePanel(Color col1, Color col2, Font font1, String str) {
        this.col1 = col1;
        this.col2 = col2;
        this.font1 = font1;
        this.str = str;
        panel1.setBackground(this.col1);
        panel1.setForeground(this.col2);
        label1.setFont(this.font1);
        label1.setText(this.str);
        panel1.add(label1);
    }    
}

In different class to invoke this constructor

JFlexiblePanel p1 = new JFlexiblePanel(Color.BLUE, Color.RED, new Font("Arial",Font.BOLD,12), "America");
Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26
YUKI
  • 6,274
  • 1
  • 7
  • 8

1 Answers1

0

Your class name implies that it's a JPanel class. So it can't extends a JFrame, but have to extend JPanel. I think you are misunderstanding how they both work.

To be short : JFrame is the top Level of a "window" and is composed by JPanel.

Moreover, your Jpanel1 and JLabel1 are never instanciated, then the error might be here :

panel1.setBackground(this.col1);
panel1.setForeground(this.col2);
panel1.add(label1);

But if you want more help you have to give more information like the type of the error. Also consider this tutorial : link to tutorial

Arnauld Alex
  • 339
  • 1
  • 3
  • 13