如何在JTextField中获取文本信息。试着在监听处添加myText.getText()无法运作

qq_37492382 2017-02-09 08:43:42
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("登录成功。");
}
}
}
...全文
243 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
shaoshiyang 2017-02-09
  • 打赏
  • 举报
回复
JTextField类的源码中重载了5个JTextField方法,分别是 public JTextField() { this(null, null, 0); } public JTextField(String text) { this(null, text, 0); } public JTextField(int columns) { this(null, null, columns); } public JTextField(String text, int columns) { this(null, text, columns); } public JTextField(Document doc, String text, int columns) { if (columns < 0) { throw new IllegalArgumentException("columns less than zero."); } visibility = new DefaultBoundedRangeModel(); visibility.addChangeListener(new ScrollRepainter()); this.columns = columns; if (doc == null) { doc = createDefaultModel(); } setDocument(doc); if (text != null) { setText(text); } } 根据你提供的代码JTextField myText =new JTextField(10);中只有int,没有text,所以肯定取不到啊。 欢迎指正。

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧