Modziek
Modziek

Reputation: 61

2 Errors in Java and no clue

I am a beginner in programming, and I am trying to finish one of my first small programs in java, already solved a lot of errors, and I have 2 errors left. I think I've already imported all needed things, but the errors still appears.

The error:

  1. The method SetLayout(FlowLayout) is undefined for the type Container
  2. B1 cannot be resolved to a type ( happens also for B2 and B5 )

Here's my code:

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;

    class Przyciski3 extends JFrame{
    JTextField t = new JTextField(20);
    JButton b1 = new JButton("B");
    JButton b2 = new JButton("A");
    JButton b5 = new JButton("Reset");
    int i=0;
    int j=0;


    Przyciski3(){
    setTitle("Przyciski3");
    Container cp = getContentPane();
    cp.SetLayout(new FlowLayout());
    cp.add(b1);
    cp.add(b2);
    cp.add(t);
    cp.add(b5);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(500,200);
    setVisible(true);
    b1.addActionListener(new B1());
    b2.addActionListener(new B2());
    b5.addActionListener(new B5());
    b1.setBackground(Color.green);
    b2.setBackground(Color.blue);
    b5.setBackground(Color.black);
    }
    }
class B1 implements ActionListener{
public void actionPerformed(ActionEvent 0){
i++;
t.setText(""+i);
}
}

class B2 implements ActionListener {
public void actionPerformed(ActionEvent 0){
j++;
t.setText(""+j);
}
}
public static void main (String[] arg){
JFrame f = new Przyciski();
}

Any suggestions?

Upvotes: 3

Views: 242

Answers (3)

Pshemo
Pshemo

Reputation: 124215

  • Change cp.SetLayout(...) to cp.setLayout(...). Java is case sensitive.
  • In public void actionPerformed(ActionEvent 0){ you are trying to name ActionEvent reference by 0. In Java variable names cant start with numbers, so try changing it to something like ActionEvent action.
  • What is i and t in your B1 and B2 class?

Upvotes: 2

PermGenError
PermGenError

Reputation: 46398

java is case sensitive .

cp.SetLayout(new FlowLayout());

should be

cp.setLayout(new FlowLayout());[Container class API][1]

Upvotes: 1

Chris
Chris

Reputation: 23171

It's just the wrong case... you need cp.setLayout(new FlowLayout());

Upvotes: 1

Related Questions