private JTextField input = new JTextField( 20 ); private JLabel label = new JLabel();
/** * other members */ public A() { //constructor input.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { String text = input.getText().trim(); returnObject obj = (returnObject)b.process( text ); label.method( obj ); } } ); } /** * other methods. */
在b中定义一个有返回值的方法,然后在a中调用b的这个方法~a() { b b1=new b(); JLable.setText(b.getA()); }b() {} public String getA() { .... return ..; }
我试了半天也不知道怎么弄,以下是我写的如果有不对能否帮我指出或改正,在这里先谢谢大哥大姐大爷大妈...import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent;public class a extends JFrame{ JPanel jp=new JPanel(); JTextField jt=new JTextField(10); JLabel lb=new JLabel("dddd"); JButton jb=new JButton("确定"); public a(){ super("aaa"); setSize(200,150); jp.add(jt); jp.add(jb); jp.add(lb); jb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ b bb=new b(); bb.getA(jt.getText());}}); this.setContentPane(jp);}
public void getB(String lbt){ b b1=new b(); lb.setText(b1.getA());}
public static void main(String args[]){ a aa=new a(); aa.show();} } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public class b{ public b(){} public void getA(String obj){ return ;} }
private B b = new B();
private JTextField input = new JTextField( 20 );
private JLabel label = new JLabel();
/**
* other members
*/
public A() {
//constructor
input.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
String text = input.getText().trim();
returnObject obj = (returnObject)b.process( text );
label.method( obj );
}
} );
} /**
* other methods.
*/
{
b b1=new b();
JLable.setText(b.getA());
}b()
{}
public String getA()
{
....
return ..;
}
改成getA(Object obj)在调用的时候就
b.getA(object obj)这个obj就是你从a传递到b的参数,这个getA(Object obj)返回的就是b传回给a的
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;public class a extends JFrame{
JPanel jp=new JPanel();
JTextField jt=new JTextField(10);
JLabel lb=new JLabel("dddd");
JButton jb=new JButton("确定");
public a(){
super("aaa");
setSize(200,150);
jp.add(jt);
jp.add(jb);
jp.add(lb);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b bb=new b();
bb.getA(jt.getText());}});
this.setContentPane(jp);}
public void getB(String lbt){
b b1=new b();
lb.setText(b1.getA());}
public static void main(String args[]){
a aa=new a();
aa.show();}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class b{
public b(){}
public void getA(String obj){
return ;}
}
在A类中用B.a = jt.getText();
^_^
------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;public class a extends JFrame
{
JPanel jp=new JPanel();
JTextField jt=new JTextField(10);
JLabel lb=new JLabel("dddd");
JButton jb=new JButton("确定");
public a()
{
super("aaa");
setSize(200,150);
jp.add(jt);
jp.add(jb);
jp.add(lb);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
b bb=new b();
lb.setText(bb.getA(jt.getText()));
}
});
this.setContentPane(jp);
}
///////////////
//这样不行,A,你没有调用这个方法; B,即使你调用了这个方法,那它也是创建一个新
//实例,达不到你的要求的
/* public void getB(String lbt)
{
b b1=new b();
lb.setText(b1.getA());//处理完后,在JLabel上显示
}
*/
///////////////
public static void main(String args[])
{
a aa=new a();
aa.show();
}
}class b
{
public b(){}
public String getA(String obj)
{
return obj+" have proccessed";//处理过程,就是在后面加上一点尾巴
}
}