Topic: 求助!!Java GUI问题:在按下Button后,如何调用TextField中的值?

  Print this page

1.求助!!Java GUI问题:在按下Button后,如何调用TextField中的值? Copy to clipboard
Posted by: hyzou
Posted on: 2007-05-15 11:47

希望能在按下Button后,调用TextField中的值,并修改Button上显示的名字,但不知如何处理。

程序如下:

import java.awt.*;

public class myButtons {

public static void main(String[] args) {
Frame f=new Frame("SoftPhone");
f.setLayout(new FlowLayout());
TextField tf1=new TextField("Tserver IP",30);
TextField tf2=new TextField("Tserver Port",20);
TextField tf3=new TextField("PlaceID",20);
TextField tf4=new TextField("AgentID",20);
TextField tf5=new TextField("PhoneNumber",20);
ConnectButton button0=new ConnectButton("Connect");
Button button1=new Button("Login");
Button button2=new Button("Ready");
Button button3=new Button("Make call");
Button button4=new Button("Release call");
MyButton button5=new MyButton("sub test");

f.add(tf1);
f.add(tf2);
f.add(button0);
f.add(tf3);
f.add(tf4);
f.add(button1);
f.add(button2);
f.add(tf5);
f.add(button3);
f.add(button4);
f.add(button5);
f.setSize(550,150);
f.setVisible(true);
}
}

class ConnectButton extends Button{

ConnectButton(String lable){
super(lable);
}
public boolean action(Event e,Object arg){
//String s=tf1.getText;
System.out.println("Pressed ConnectButton: "+arg);
System.out.println("Event: "+e);
return true;
}
}

class MyButton extends Button{

MyButton(String lable){
super(lable);
}
public boolean action(Event e,Object arg){
System.out.println("Pressed Button: "+arg);
System.out.println("Event: "+e);;
return true;
}
}

2.Re:求助!!Java GUI问题:在按下Button后,如何调用TextField中的值? [Re: hyzou] Copy to clipboard
Posted by: hyzou
Posted on: 2007-05-15 12:02

自己顶一个!

3.Re:求助!!Java GUI问题:在按下Button后,如何调用TextField中的值? [Re: hyzou] Copy to clipboard
Posted by: hyzou
Posted on: 2007-05-15 12:33


4.Re:求助!!Java GUI问题:在按下Button后,如何调用TextField中的值? [Re: hyzou] Copy to clipboard
Posted by: kkkrnm
Posted on: 2007-05-15 20:51

你得利用动作事件才能产生按下Button后的动作
比如写个这样的方法
public void actionPerfomed(ActionEvent e)
{
.......//此处写你的方法
}

5.Re:求助!!Java GUI问题:在按下Button后,如何调用TextField中的值? [Re: hyzou] Copy to clipboard
Posted by: glqky
Posted on: 2007-05-28 00:28

改了一下您的程序,您看这个行不行

import java.awt.*;
import java.awt.event.*;

public class myButtons{

Frame f = null;
Button button0, button1, button2, button3, button4, button5;
TextField tf1, tf2, tf3, tf4, tf5;

public myButtons()
{
f=new Frame("SoftPhone");
f.setLayout(new FlowLayout());
tf1=new TextField("Tserver IP",30);
tf2=new TextField("Tserver Port",20);
tf3=new TextField("PlaceID",20);
tf4=new TextField("AgentID",20);
tf5=new TextField("PhoneNumber",20);

button0=new Button("Connect");
button1=new Button("Login");
button2=new Button("Ready");
button3=new Button("Make call");
button4=new Button("Release call");
button5=new Button("sub test");

button0.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    System.out.println("Tserver IP: "+ gettf1());
    button0.setLabel("conneted");
    }
});

f.add(tf1);
f.add(tf2);
f.add(button0);
f.add(tf3);
f.add(tf4);
f.add(button1);
f.add(button2);
f.add(tf5);
f.add(button3);
f.add(button4);
f.add(button5);

f.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e)
  { System.exit(0);}
});

f.setSize(550,150);
f.setVisible(true);
}

String gettf1()
{return tf1.getText();}

public static void main(String[] args)
{
myButtons obj = new myButtons();
}
}

6.Re:求助!!Java GUI问题:在按下Button后,如何调用TextField中的值? [Re: hyzou] Copy to clipboard
Posted by: 胡嘉惠
Posted on: 2007-06-26 10:32

import java.awt.*;
import java.awt.event.*;

public class myButtons{

Frame f = null;
Button button0, button1, button2, button3, button4, button5;
TextField tf1, tf2, tf3, tf4, tf5;

public myButtons()
{
f=new Frame("SoftPhone");
f.setLayout(new FlowLayout());
tf1=new TextField("Tserver IP",30);
tf2=new TextField("Tserver Port",20);
tf3=new TextField("PlaceID",20);
tf4=new TextField("AgentID",20);
tf5=new TextField("PhoneNumber",20);

button0=new Button("Connect");
button1=new Button("Login");
button2=new Button("Ready");
button3=new Button("Make call");
button4=new Button("Release call");
button5=new Button("sub test");

button0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.out.println("Tserver IP: "+ gettf1());
button0.setLabel("conneted");
}
});

f.add(tf1);
f.add(tf2);
f.add(button0);
f.add(tf3);
f.add(tf4);
f.add(button1);
f.add(button2);
f.add(tf5);
f.add(button3);
f.add(button4);
f.add(button5);

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});

f.setSize(550,150);
f.setVisible(true);
}

String gettf1()
{return tf1.getText();}

public static void main(String[] args)
{
myButtons obj = new myButtons();
}
}
编译错误.....


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923