this is my code. I have no clue what could be wrong with it. The error that keeps coming up is: class, interface, or enum expected. The five errors it pointed out are the 'v' in public void actionPerformed at the bottom of the code, the 'g' in the green = Integer.parseInt....., the 'b' in the blue = Integer.parseInt..... the 'r' in the repaint().. and '}' at the very end of the code... anyone can help?

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


public class UpdatedColorApplet extends JApplet implements ActionListener {

    private JTextField txtRed = new JTextField("255", 3);
    private JTextField txtGreen = new JTextField("255", 3);
    private JTextField txtBlue = new JTextField("255", 3);
    private JButton cmdEnter = new JButton("Enter");

    private int red=255;
    private int green=255;
    private int blue=255;


    /**
     * The entry point for the applet.
     */
    public UpdatedColorApplet(){

        // get contentPane & set layout manager
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());


        // text components & labels
        cp.add(new JLabel("CPT 237 - Java - Color Class RGB Component Demonistration Applet"));
        cp.add(new JLabel("Red"));
        cp.add(txtRed);
        cp.add(new JLabel("Green"));
        cp.add(txtGreen);
        cp.add(new JLabel("Blue"));
        cp.add(txtBlue);
        cp.add(new NewLabel());

        // add button and set up handler
        cp.add(cmdEnter);
        cmdEnter.addActionListener(this);

}

    public static void main(String[] args) {
        UpdatedColorApplet frame = new UpdatedColorApplet();
        frame.setSize(250, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
}
}

    class NewLabel extends JLabel {
        protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        // draw the large rectangle
        g.setColor(new Color(red, green, blue));
        g.fillRect(100, 100, 200, 150);

        // draw the red component rectangle
        g.setColor(new Color(red, 0, 0));
        g.fillRect(120, 120, 40, 100);

        // draw the green component rectangle
        g.setColor(new Color(0, green, 0));
        g.fillRect(180, 120, 40, 100);

        // draw the blue component rectangle
        g.setColor(new Color(0, 0, blue));
        g.fillRect(240, 120, 40, 100);

}
}


    public void actionPerformed(ActionEvent e) {
        // get color components
        red = Integer.parseInt(txtRed.getText());
        green = Integer.parseInt(txtGreen.getText());
        blue = Integer.parseInt(txtBlue.getText());

        // repaint the applet
        repaint();
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

I did that and this is the code below. I'm pretty sure i got it all right but i just can't seem to find what i am missing. It's giving me 2 errors now referring to the dots in the frame.setLocation... and frame.setDefault.... saying cannot find symbol.

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


public class UpdatedColorApplet extends JApplet implements ActionListener {

    private JTextField txtRed = new JTextField("255", 3);
    private JTextField txtGreen = new JTextField("255", 3);
    private JTextField txtBlue = new JTextField("255", 3);
    private JButton cmdEnter = new JButton("Enter");

    private int red=255;
    private int green=255;
    private int blue=255;
    /**
     * The entry point for the applet.
     */
    public UpdatedColorApplet(){

        // get contentPane & set layout manager
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());


        // text components & labels
        cp.add(new JLabel("CPT 237 - Java - Color Class RGB Component Demonistration Applet"));
        cp.add(new JLabel("Red"));
        cp.add(txtRed);
        cp.add(new JLabel("Green"));
        cp.add(txtGreen);
        cp.add(new JLabel("Blue"));
        cp.add(txtBlue);
        cp.add(new NewLabel());

        // add button and set up handler
        cp.add(cmdEnter);
        cmdEnter.addActionListener(this);




}

    public static void main(String[] args) {
        UpdatedColorApplet frame = new UpdatedColorApplet();
        frame.setSize(250, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

}
    public void actionPerformed(ActionEvent e) {
                // get color components
                red = Integer.parseInt(txtRed.getText());
                green = Integer.parseInt(txtGreen.getText());
            blue = Integer.parseInt(txtBlue.getText());
}

    class NewLabel extends JLabel {
        protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        // draw the large rectangle
        g.setColor(new Color(red, green, blue));
        g.fillRect(100, 100, 200, 150);

        // draw the red component rectangle
        g.setColor(new Color(red, 0, 0));
        g.fillRect(120, 120, 40, 100);

        // draw the green component rectangle
        g.setColor(new Color(0, green, 0));
        g.fillRect(180, 120, 40, 100);

        // draw the blue component rectangle
        g.setColor(new Color(0, 0, blue));
        g.fillRect(240, 120, 40, 100);

        // repaint the applet
        repaint();

}
}
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

You are extending/subclassing your class to JApplet class which doesnt have the method used by you. You need to extend JFrame to use

frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I changed it to JFrame and when i compile there are no errors, but when i run it the error message is exception in thread main java.lang.NoSuchMethodError: main.

I changed it to JFrame and when i compile there are no errors, but when i run it the error message is exception in thread main java.lang.NoSuchMethodError: main.

Well its working fine for me. TRy to catch the exception using printStackTrace() and then tell which line you are getting exception

This article has been dead for over six months. Start a new discussion instead.