3

我是一个编程初学者,我正在尝试用java完成我的第一个小程序,已经解决了很多错误,还剩下2个错误。我想我已经导入了所有需要的东西,但错误仍然出现。

错误:

  1. Container 类型的方法 SetLayout(FlowLayout) 未定义
  2. B1 无法解析为类型(也发生在 B2 和 B5 中)

这是我的代码:

    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();
}

有什么建议么?

4

3 回答 3

2
  • 更改cp.SetLayout(...)cp.setLayout(...)。Java 区分大小写。
  • public void actionPerformed(ActionEvent 0){您尝试ActionEvent通过0. 在 Java 中,变量名不能以数字开头,因此请尝试将其更改为ActionEvent action.
  • 什么是it在你B1B2班级?
于 2012-11-01T16:15:15.247 回答
1

这只是错误的情况......你需要cp.setLayout(new FlowLayout());

于 2012-11-01T16:15:37.320 回答
1

java区分大小写。

cp.SetLayout(new FlowLayout());

应该

cp.setLayout(new FlowLayout());[Container class API][1]
于 2012-11-01T16:16:22.063 回答