java编程Swing编辑多个界面给多个界面中的按钮设置监听如何把按钮监听写在一个方法里
展开全部
import java.awt.event.*;
import javax.swing.*;
public class Test implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSourse().equals(button1)){
//do your work,如果按钮1按下
}else if(e.getSourse().equals(button2)){}//button2........
}
JButton button1=new JButton(“b1”),button2=new JButton(“b2”);
//.......buttons
public Test(){
JFrame fra1=new JFrame(),fra2=new JFrame();//...frames
fra1.add(button1);
fra2.add(button2);
//add frame
button1.addActionListener(this);
button2.addActionLi
stener(this);
//add listener
fram1.setVisible(true);
fram2.setVisible(true);
}
public static void main(String[] args){
new Test();
}
}
import javax.swing.*;
public class Test implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSourse().equals(button1)){
//do your work,如果按钮1按下
}else if(e.getSourse().equals(button2)){}//button2........
}
JButton button1=new JButton(“b1”),button2=new JButton(“b2”);
//.......buttons
public Test(){
JFrame fra1=new JFrame(),fra2=new JFrame();//...frames
fra1.add(button1);
fra2.add(button2);
//add frame
button1.addActionListener(this);
button2.addActionLi
stener(this);
//add listener
fram1.setVisible(true);
fram2.setVisible(true);
}
public static void main(String[] args){
new Test();
}
}
追答
行吗?就在if里面写
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询