Java 循环按钮监听事件:要判断当前点击的按钮是哪一个,并有响应。贴出代码:
publicclassOrderOpextendsJFrameimplementsActionListener{/****/privatestaticfinallongs...
public class OrderOp extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private ResultSet rs=null;
private JButton b1=new JButton("添加");
private String sql=null;
private int len,i;
private JButton []b;
private String ordername=null;
String num="";
OrderOp(final String num) throws SQLException{
this.num=num;
sql="select * from orderlist where num='"+num+"'";
this.setTitle("命令操作");
getContentPane().setLayout(new FlowLayout());
//显示所有命令的命令名
display(sql);
//点击增加按钮,增加命令内容
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
new AddOrder(num);
try {
display(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
public void display(String sql) throws SQLException{
rs=SqlOp.executeQuery(sql, null);
rs.last();
len=rs.getRow();
b=new JButton[len];
rs.first();
try {
do{
i=0;
ordername=rs.getString("ordername");
b[i]=new JButton(""+ordername);
System.out.println("b[i]:"+b[i]);
this.add(b[i]);
System.out.println("+"+b[i]);
b[i].addActionListener(this);
i++;
}while(rs.next());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
for(int i=0;i<b.length;i++){
if(command==b[i].getActionCommand()){//判断点击的按钮,出现显示命令的界面
new OrderDisplay(command,num);
}
}
}}
判断点击按钮时没有反应,这是为什么?求教大神!!!! 展开
/**
*
*/
private static final long serialVersionUID = 1L;
private ResultSet rs=null;
private JButton b1=new JButton("添加");
private String sql=null;
private int len,i;
private JButton []b;
private String ordername=null;
String num="";
OrderOp(final String num) throws SQLException{
this.num=num;
sql="select * from orderlist where num='"+num+"'";
this.setTitle("命令操作");
getContentPane().setLayout(new FlowLayout());
//显示所有命令的命令名
display(sql);
//点击增加按钮,增加命令内容
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
new AddOrder(num);
try {
display(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
public void display(String sql) throws SQLException{
rs=SqlOp.executeQuery(sql, null);
rs.last();
len=rs.getRow();
b=new JButton[len];
rs.first();
try {
do{
i=0;
ordername=rs.getString("ordername");
b[i]=new JButton(""+ordername);
System.out.println("b[i]:"+b[i]);
this.add(b[i]);
System.out.println("+"+b[i]);
b[i].addActionListener(this);
i++;
}while(rs.next());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
for(int i=0;i<b.length;i++){
if(command==b[i].getActionCommand()){//判断点击的按钮,出现显示命令的界面
new OrderDisplay(command,num);
}
}
}}
判断点击按钮时没有反应,这是为什么?求教大神!!!! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询