-1

Here's my program:

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

public class FinalsLabExam extends JFrame implements ActionListener,
      MouseListener {

   JLabel labelMc = new JLabel("McElvin R. Liceralde", SwingConstants.CENTER);

   String[] fNames = { "arial", "calibri", "tahoma", "verdana", "broadway" };
   JComboBox cfNames = new JComboBox(fNames);

   JCheckBox bold, italic;
   Font f = new Font("Times New Roman", Font.BOLD, 35);
   Font font1 = new Font("Arial", Font.BOLD, 35);
   Font font2 = new Font("Calibri", Font.BOLD, 35);
   Font font3 = new Font("Tahoma", Font.BOLD, 35);
   Font font4 = new Font("Verdana", Font.BOLD, 35);
   Font font5 = new Font("Broadway", Font.BOLD, 35);

   JMenuBar menubar = new JMenuBar();
   JMenu mFormat, mFont, mFont2, mFontName, mFontSize, mColor, mColor2, mText,
         mBColor;
   JMenuItem miArial, miCalibri, miTahoma, miVerdana, miBroadway, miBlue,
         miGreen, miRed, miYellow, miBlack, miOrange;

   JPopupMenu pumenu = new JPopupMenu();
   JToolBar tbar = new JToolBar();

   public FinalsLabExam() {
      mFormat = new JMenu("Format");
      mFont = new JMenu("Font");
      mFont2 = new JMenu("Font");
      mFontName = new JMenu("Font Name");
      mFontSize = new JMenu("Font Size");
      mColor = new JMenu("Color");
      mColor2 = new JMenu("Color");
      mText = new JMenu("Text");
      mBColor = new JMenu("Background");

      mFormat.setMnemonic('f');
      mFont.setMnemonic('f');
      mFont2.setMnemonic('f');
      mFontName.setMnemonic('n');
      mFontSize.setMnemonic('s');
      mColor.setMnemonic('c');
      mColor2.setMnemonic('c');
      mText.setMnemonic('t');
      mBColor.setMnemonic('b');

      miArial = new JMenuItem("Arial", 'a');
      miCalibri = new JMenuItem("Calibri", 'c');
      miTahoma = new JMenuItem("Tahoma", 't');
      miVerdana = new JMenuItem("Verdana", 'v');
      miBroadway = new JMenuItem("Broadway", 'w');
      // miBlue = new JMenuItem("Blue", 'l');
      miBlue = new JMenuItem((new ImageIcon("blue.jpg")) + "Blue", 'b');
      miGreen = new JMenuItem("Green", 'g');
      miRed = new JMenuItem("Red", 'r');
      miYellow = new JMenuItem("Yellow", 'y');
      miBlack = new JMenuItem("Black", 'k');
      miOrange = new JMenuItem("Orange", 'o');

      setJMenuBar(menubar);
      menubar.add(mFormat);
      mFormat.add(mFont);
      mFormat.add(mFont2);
      mFont.add(mFontName);
      mFont2.add(mFontName);
      mFontName.add(miArial);
      mFontName.add(miCalibri);
      mFontName.add(miTahoma);
      mFontName.add(miVerdana);
      mFontName.add(miBroadway);
      mFont.add(mFontSize);
      mFont2.add(mFontSize);
      mFormat.add(mColor);
      mFormat.add(mColor2);
      mColor.add(mText);
      mColor2.add(mText);
      mText.add(miBlue);
      mText.add(miGreen);
      mText.add(miRed);
      mColor.add(mBColor);
      mColor2.add(mBColor);
      mBColor.add(miYellow);
      mBColor.add(miBlack);
      mBColor.add(miOrange);

      miArial.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2));
      miCalibri.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 2));
      miTahoma.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, 2));
      miVerdana.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2));
      miBroadway.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 2));

      miBlue.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, 2));
      miGreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 2));
      miRed.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, 2));
      miYellow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 2));
      miBlack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, 2));
      miOrange.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2));

      addMouseListener(this);

      miArial.addActionListener(this);
      miCalibri.addActionListener(this);
      miTahoma.addActionListener(this);
      miVerdana.addActionListener(this);
      miBroadway.addActionListener(this);
      miBlue.addActionListener(this);
      miGreen.addActionListener(this);
      miRed.addActionListener(this);
      miYellow.addActionListener(this);
      miBlack.addActionListener(this);
      miOrange.addActionListener(this);

      Container pane = getContentPane();
      pane.setLayout(new BorderLayout());
      pane.setOpaque(true);

      labelMc.setBounds(350, 400, 600, 100);
      pane.add(labelMc);
      labelMc.setFont(f);

      pane.add(tbar, BorderLayout.NORTH);
      tbar.add(mFont);
      tbar.add(mColor);

      pumenu.add(mFont);
      pumenu.add(mColor);

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      setVisible(true);
      setTitle("Finals Laboratory Exam");
      setSize(500, 200);
      setLocation(500, 300);
      setResizable(false);
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getSource() == miArial)
         labelMc.setFont(font1);
      else if (e.getSource() == miCalibri)
         labelMc.setFont(font2);
      else if (e.getSource() == miTahoma)
         labelMc.setFont(font3);
      else if (e.getSource() == miVerdana)
         labelMc.setFont(font4);
      else if (e.getSource() == miBroadway)
         labelMc.setFont(font5);

      else if (e.getSource() == miBlue)
         labelMc.setForeground(Color.BLUE);
      else if (e.getSource() == miGreen)
         labelMc.setForeground(Color.GREEN);
      else if (e.getSource() == miRed)
         labelMc.setForeground(Color.RED);

      else if (e.getSource() == miYellow) {
         pane.setBackground(Color.YELLOW);
      } else if (e.getSource() == miBlack) {
         setBackground(Color.BLACK);
      } else if (e.getSource() == miOrange) {
         setBackground(Color.ORANGE);
      }
   }

   public void mouseExited(MouseEvent e) {
   }

   public void mouseEntered(MouseEvent e) {
   }

   public void mouseReleased(MouseEvent e) {

      if (e.isPopupTrigger())
         pumenu.show(e.getComponent(), e.getX(), e.getY());

   }

   public void mousePressed(MouseEvent e) {
   }

   public void mouseClicked(MouseEvent e) {
   }

   public static void main(String[] args) {
      new FinalsLabExam();
   }
}

but it doesn't change the background color :( and when I add pane.setBackground(Color.YELLOW);

It says...CANNOT FIND SYMBOL :( help me please :)

Thank you for all who will answer and help me. It's my 1st time posting a question here :)

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • 3
    Please show the **complete error message** and enough code to allow us to fully understand your error. Also, you must be sure that your JLabel is opaque usually by calling `myLabel.setOpaque(true);` if myLabel is to display any background color. – Hovercraft Full Of Eels Mar 13 '15 at 15:35
  • C:\Users\Acer\Documents\JCreator LE\MyProjects\FinalsLab\src\FinalsLab.java:168: error: cannot find symbol {pane.setBackground(Color.YELLOW);} ^ symbol: variable pane location: class FinalsLab Note: C:\Users\Acer\Documents\JCreator LE\MyProjects\FinalsLab\src\FinalsLab.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error – Blade Matrix Mar 13 '15 at 15:37
  • 1
    did you do `label.setOpaque(true);`? See [similar](http://stackoverflow.com/questions/2380314/how-do-i-set-a-jlabels-background-color) – singhakash Mar 13 '15 at 15:37
  • 3
    Please don't post code or error messages in comments since it loses its formatting making it unreadable. Instead, post any new code to the bottom of your original question by [editing your question](http://stackoverflow.com/posts/29036286/edit). – Hovercraft Full Of Eels Mar 13 '15 at 15:38
  • There's a way to do this if you use html in the text you set to the JLabel – ControlAltDel Mar 13 '15 at 15:38
  • I added setOpaque but it's also an error :( – Blade Matrix Mar 13 '15 at 15:40
  • why cant you just edit your question with error details and more code.It difficult for us to read – singhakash Mar 13 '15 at 15:42
  • Again, please post enough pertinent code for us to be able to understand (and best of all, reproduce) your error, and the error message as an [edit to your question](http://stackoverflow.com/posts/29036286/edit). Doing this will help both you and us as we'll better understand your problem and you'll stand a chance of getting a quick, relevant and helpful answer. – Hovercraft Full Of Eels Mar 13 '15 at 15:43
  • I edited it and posted the full code :) – Blade Matrix Mar 13 '15 at 15:49

2 Answers2

1

You've got a variable scope issue. The pane variable is declared inside of a constructor, and is thus only visible inside its scope of declaration, the constructor, and remains completely invisible to the rest of the class. To solve this, declare the variable as a field, in the class, not in the method or constructor.

For example, not:

public class FinalsLabExam {

   public FinalsLabExam() {
      // ...
      Container pane = getContentPane():
      //....
   }

but rather:

public class FinalsLabExam {
   // declare this as a field
   // declare as a JPanel, since that's what it really is
   private JPanel pane; 

   public FinalsLabExam() {
      // ...
      pane = (JPanel) getContentPane(); // cast to JPanel
      //....
   }

Note that pane is not a JLabel but rather is a Container, and this makes your question a bit confusing -- what are you trying to set the color of? A JLabel or a Container?

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
1

I agree to @Hovercarft you are doing only setBackground(Color.YELLOW); not for any Container .So declate it outside the constructor so that it can be accesible in other methods and Do pane.setBackground(Color.YELLOW);.

Here is full code in case you find any trouble

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

public class FinalsLabExam  extends JFrame implements ActionListener, MouseListener
    {

    JLabel labelMc = new JLabel ("McElvin R. Liceralde", SwingConstants.CENTER);

    String [] fNames = {"arial", "calibri", "tahoma", "verdana", "broadway"};
    JComboBox cfNames = new JComboBox(fNames);

    JCheckBox bold, italic;
    Font f = new Font("Times New Roman", Font.BOLD, 35);
    Font font1 = new Font("Arial", Font.BOLD, 35);
    Font font2 = new Font("Calibri", Font.BOLD, 35);
    Font font3 = new Font("Tahoma", Font.BOLD, 35);
    Font font4 = new Font("Verdana", Font.BOLD, 35);
    Font font5 = new Font("Broadway", Font.BOLD, 35);

    JMenuBar menubar = new JMenuBar();
    JMenu mFormat, mFont, mFont2, mFontName, mFontSize, mColor, mColor2, mText, mBColor;
    JMenuItem miArial, miCalibri, miTahoma, miVerdana, miBroadway, miBlue, miGreen, miRed, miYellow, miBlack, miOrange;

    JPopupMenu pumenu = new JPopupMenu();
    JToolBar tbar = new JToolBar();
    Container pane;// = getContentPane();
    public FinalsLabExam()
    {
        mFormat = new JMenu("Format");
        mFont = new JMenu("Font");
        mFont2 = new JMenu("Font");
        mFontName = new JMenu("Font Name");
        mFontSize = new JMenu("Font Size");
        mColor = new JMenu("Color");
        mColor2 = new JMenu("Color");
        mText = new JMenu("Text");
        mBColor = new JMenu("Background");

        mFormat.setMnemonic('f');
        mFont.setMnemonic('f');
        mFont2.setMnemonic('f');
        mFontName.setMnemonic('n');
        mFontSize.setMnemonic('s');
        mColor.setMnemonic('c');
        mColor2.setMnemonic('c');
        mText.setMnemonic('t');
        mBColor.setMnemonic('b');

        miArial = new JMenuItem("Arial", 'a');
        miCalibri = new JMenuItem("Calibri", 'c');
        miTahoma = new JMenuItem("Tahoma", 't');
        miVerdana = new JMenuItem("Verdana", 'v');
        miBroadway = new JMenuItem("Broadway", 'w');
        //miBlue = new JMenuItem("Blue", 'l');
        miBlue = new JMenuItem((new ImageIcon("blue.jpg")) + "Blue", 'b');
        miGreen = new JMenuItem("Green", 'g');
        miRed = new JMenuItem("Red", 'r');
        miYellow = new JMenuItem("Yellow", 'y');
        miBlack = new JMenuItem("Black", 'k');
        miOrange = new JMenuItem("Orange", 'o');

        setJMenuBar(menubar);
        menubar.add(mFormat);
            mFormat.add(mFont);
            mFormat.add(mFont2);
                mFont.add(mFontName);
                mFont2.add(mFontName);
                    mFontName.add(miArial);
                    mFontName.add(miCalibri);
                    mFontName.add(miTahoma);
                    mFontName.add(miVerdana);
                    mFontName.add(miBroadway);
                mFont.add(mFontSize);
                mFont2.add(mFontSize);
            mFormat.add(mColor);
            mFormat.add(mColor2);
                mColor.add(mText);
                mColor2.add(mText);
                    mText.add(miBlue);
                    mText.add(miGreen);
                    mText.add(miRed);
                mColor.add(mBColor);
                mColor2.add(mBColor);
                    mBColor.add(miYellow);
                    mBColor.add(miBlack);
                    mBColor.add(miOrange);

    miArial.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2));
    miCalibri.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 2));
    miTahoma.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, 2));
    miVerdana.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2));
    miBroadway.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, 2));

    miBlue.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, 2));
    miGreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 2));
    miRed.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, 2));
    miYellow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 2));
    miBlack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, 2));
    miOrange.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2));      


    addMouseListener(this);

    miArial.addActionListener(this);
    miCalibri.addActionListener(this);
    miTahoma.addActionListener(this);
    miVerdana.addActionListener(this);
    miBroadway.addActionListener(this);
    miBlue.addActionListener(this);
    miGreen.addActionListener(this);
    miRed.addActionListener(this);
    miYellow.addActionListener(this);
    miBlack.addActionListener(this);
    miOrange.addActionListener(this);           
    pane = getContentPane();

    pane.setLayout(new BorderLayout());
    //pane.setOpaque(true);

    labelMc.setBounds(350,400,600,100);
    pane.add(labelMc);
    labelMc.setFont(f);

    pane.add(tbar, BorderLayout.NORTH);
    tbar.add(mFont);
    tbar.add(mColor);

    pumenu.add(mFont);
    pumenu.add(mColor);

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setVisible(true);
    setTitle("Finals Laboratory Exam");
    setSize(500,200);
    setLocation(500,300);
    setResizable(false);
    }


public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == miArial)
        labelMc.setFont(font1);
    else if (e.getSource() == miCalibri)
        labelMc.setFont(font2);
    else if (e.getSource() == miTahoma)
        labelMc.setFont(font3);
    else if (e.getSource() == miVerdana)
        labelMc.setFont(font4);
    else if (e.getSource() == miBroadway)
        labelMc.setFont(font5);

    else if (e.getSource() == miBlue)
        labelMc.setForeground(Color.BLUE);
    else if (e.getSource() == miGreen)
        labelMc.setForeground(Color.GREEN); 
    else if (e.getSource() == miRed)
        labelMc.setForeground(Color.RED);

    else if (e.getSource() == miYellow)
    {pane.setBackground(Color.YELLOW);}
    else if (e.getSource() == miBlack)
    {pane.setBackground(Color.BLACK);}
    else if (e.getSource() == miOrange)
    {pane.setBackground(Color.ORANGE);}

}

public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}   
public void mouseReleased(MouseEvent e)
{   

if (e.isPopupTrigger())  
    pumenu.show(e.getComponent(), e.getX(), e.getY()); 

}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){}

public static void main(String[] args) 
{
    new FinalsLabExam();
}
}

After selecting yellow you get

enter image description here

singhakash
  • 7,613
  • 4
  • 25
  • 59
  • @BladeMatrix if you got your answer then consider accepting it so that other having same issue should know that the question is answered. – singhakash Mar 14 '15 at 05:09