我不知道如何从单独的 ActionListener 类中实现动作监听器。我将应用程序分为几个类。Main 类调用 Frame 类来创建带有所有按钮的 GUI。现在,当按下按钮时,我需要向 ActionListener 方法发送一些数据以使其完成工作。如果我在 Frame 类中添加 ActionListener 而无法发送所需的数据,如果我在 main() 方法中添加 ActionListener,在调用 Frame 类之后会感觉很笨拙。下面是代码: public class Main {
public static Client klijent;
public static void main(String[] args) {
FrameBuilder frame= new FrameBuilder();
frame.frameBuild();
frame.send.addActionListener(new SendBttnListener(ClientBuilder(klijent,frame.txt.getText())));
try {
frame.statLabel.setText(InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
Udp udp;
// klijent = null;
while(true){
klijent=new Client();
udp=new Udp();
klijent=udp.packageIN();
if(klijent!=null){
frame.statLabel.setText("");
String str=new String(klijent.getBajt(),0,klijent.bajt.length);
str=str.trim();
frame.statLabel.setText(str+"@...@"+klijent.clientAddress.toString()+":"+klijent.clientPort+"-->Duljina:"+klijent.bajt.length);
}
}
}
public static Client ClientBuilder(Client klijent2, String str){
klijent.setBajt(str.getBytes());
return klijent2;
}
如您所见,客户端对象可以更改很多(UDP 服务器),并且在等待下一个包时将被清除。后来我想实现一些客户端列表并在 Frame 类中添加 Action 监听器似乎很愚蠢。当我创建用于构建 GUI 的 separet 类时我出错了吗?有什么建议吗?
提前致谢....