-3

This small code in java is giving me 4 errors. I dont understand what they mean. Please help me.

class Frames extends Frame implements ActionListener {

    Frames() {
        JFrame jf = new JFrame("Welcome");
        Container c = jf.getContentPane();
        JPanel jp = new JPanel();
        c.add(jp);
        JLabel jl = new JLabel("Please enter your name");
        jp.add(jl);
        JTextField jtf = new JTextField(30);
        jp.add(jtf);
        JButton jb = new JButton("Submit");
        jp.add(jb);
        jb.addActionListener(this);
        jl.setForeground(Color.white);
        jp.setBackground(Color.black);
        jf.setBounds(200, 200, 400, 400);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

        public void actionPerformed(ActionEvent ae) {
            JOptionPane.showMessageDialog(frame, "Hello");
        }
    }

    public static void main (String[] args) {
        Frames f = new Frames();
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63
  • 2
    I'd just like to point out that your are extending from `java.awt.Frame`, but creating a second `javax.swing.JFrame` within the class...In this case, you don't need to extend from `java.awt.Frame` – MadProgrammer Aug 09 '13 at 05:25
  • Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Aug 09 '13 at 06:12
  • -1 for not including import statements. Normally, I won't downvote on such thingies, though, the question you asking in the comments to the answers, relates to that aspect of your programming knowledge :-) In future, when you are presenting a code example for seeking help, always do provide import statements, they are the means to tell the other person, of exactly what you referring to, in your code, from the standard library or likewise. – nIcE cOw Aug 09 '13 at 10:28

2 Answers2

3

Should close the braces in Frames() constructor

Frames() {
    JFrame jf = new JFrame("Welcome");
    Container c = jf.getContentPane();
    JPanel jp = new JPanel();
    c.add(jp);
    JLabel jl = new JLabel("Please enter your name");
    jp.add(jl);
    JTextField jtf = new JTextField(30);
    jp.add(jtf);
    JButton jb = new JButton("Submit");
    jp.add(jb);
    jb.addActionListener(this);
    jl.setForeground(Color.white);
    jp.setBackground(Color.black);
    jf.setBounds(200,200,400,400);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Thanks. But now it shows the error could not find symbol in JoptionPane – Chinmay Dabke Aug 09 '13 at 05:28
  • 2
    Umm.. since this question is about *"Illegal start of expression.."* it now seems to be answered. Select an answer and ask a question specific to the new problem. But a few tips: Don't come running to us the first moment you see a new error. Do some research on it first. If you cannot figure out how to fix it from your research, explain what you've done & copy/paste the error or exception message. – Andrew Thompson Aug 09 '13 at 06:14
2

You don't close the constructor Frames with a } before starting the method actionPerformed

The } is incorrectly after the method.

Tom
  • 43,583
  • 4
  • 41
  • 61
  • Thanks. But now it shows the error could not find symbol in JoptionPane. Please help! – Chinmay Dabke Aug 09 '13 at 05:29
  • There is no JOptionPane in that code, note my capitalization - you will need to import ( javax.swing.JOptionPane ) it at the top of the file. – Tom Aug 09 '13 at 05:32