今天刚学图形用户界面编程这一块,下面的代码、函数都是我一个个刚搞清楚意思才写进去的,很生疏,题目要求是编一个简单的图形界面程序,里面有两个按钮和一个文本框,按不同的按钮文本框里要有不同的信息,,我的错误代码如下:
import java.awt.event.*;
import javax.swing.*;
public class ButtonInfoShowDemo implements ActionListener{
JTextField t=new JTextField(20);
JButton b=new JButton("b1");
JButton b2=new JButton("b2");
public ButtonInfoShowDemo(){
JFrame f=new JFrame("我的第五个java窗口程序");
Container c=f.getContentPane();
c.setLayout(new FlowLayout());
c.add(t);
b.setBounds(30,20,20,20);
b2.setBounds(50,20,20,20);
b.addActionListener(this);
b2.addActionListener(this);
c.add(b);
c.add(b2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,100);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSourse()==b)
{
JOptionPane.showMessageDialog(null,"曹尼玛给爷编译通过","恭喜",JOptionPane.INFORMATION_MESSAGE);
t.setText("卧槽成功了!");
}
else if(e.getSourse()==b2)
{ t.setText("奈斯兄弟");
}
}
public static void main(String args[])
{ new ButtonInfoShowDemo();
}
}
发现了,是source,不是sourse、