如何在JTextField中获取文本信息。试着在监听处添加myText.getText()无法运作
public class Person170205 extends JFrame implements ActionListener{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person170205 panel=new Person170205();
}
//构造函数
public Person170205(){
// 定义组件
JLabel myLabel1 = new JLabel("用户名");
JLabel myLabel2 = new JLabel("密 码");
JButton myButton1 = new JButton("登录");
JButton myButton2 = new JButton("忘记密码");
JPasswordField myPassword = new JPasswordField(10);
JTextField myText =new JTextField(10);
JPanel newPanel1 = new JPanel();
JPanel newPanel2 = new JPanel();
JPanel newPanel3 = new JPanel();
String s=myText.getText();
System.out.println(s);
//布局管理
this.setLayout(new GridLayout(3,1));//三行一列的网格布局
//加入各个组件
newPanel1.add(myLabel1);
newPanel1.add(myText);
newPanel1.setBackground(Color.green);
newPanel2.add(myLabel2 );
newPanel2.add(myPassword);
newPanel2.setBackground(Color.green);
newPanel3.add(myButton1);
newPanel3.add(myButton2);
newPanel3.setBackground(Color.green);
//加入到JFrame
this.add(newPanel1);
this.add(newPanel2);
this.add(newPanel3);
//设置布局管理器
this.setSize(400,200);
this.setLocation(400,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//没有面板时可以用以下的代码使容器背景着色
//this.getContentPane().setBackground(Color.green);
//显示
this.setVisible(true);
//为按钮注册监听
myButton1.addActionListener(this);
//点击按钮带来的结果
myButton1.setActionCommand("PassIN");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//判断是那个按钮被点击
if(e.getActionCommand().equals("PassIN"))
{
System.out.println("登录成功。");
}
}
}