当前位置: 技术问答>java相关
如何判断点击了哪个按钮?
来源: 互联网 发布时间:2015-05-24
本文导语: 在一个form中有两个button,如何判断点击了哪个按钮? | 为两个按钮添加ActionListener 在actionPerformed(ActionEvent e)中: Button btn=(Button)e.getSource(); if(btn==button1) //some code... else if(btn==...
在一个form中有两个button,如何判断点击了哪个按钮?
|
为两个按钮添加ActionListener
在actionPerformed(ActionEvent e)中:
Button btn=(Button)e.getSource();
if(btn==button1)
//some code...
else if(btn==button2)
//other code...
在actionPerformed(ActionEvent e)中:
Button btn=(Button)e.getSource();
if(btn==button1)
//some code...
else if(btn==button2)
//other code...
|
JButton confirmButton = new JButton("修 改");
confirmButton.setActionCommand("confirm");
confirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
button_actionPerformed(ae);
}
});
JButton cancleButton = new JButton("取 消");
cancleButton.setActionCommand("cancle");
cancleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
button_actionPerformed(ae);
}
});
private void button_actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("confirm"))
{
System.out.println("confirm");
super.dispose();
ll.setVisible(true);
}
else
{
System.out.println("cancle");
super.dispose();
ll.setVisible(true);
}
}
confirmButton.setActionCommand("confirm");
confirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
button_actionPerformed(ae);
}
});
JButton cancleButton = new JButton("取 消");
cancleButton.setActionCommand("cancle");
cancleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
button_actionPerformed(ae);
}
});
private void button_actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("confirm"))
{
System.out.println("confirm");
super.dispose();
ll.setVisible(true);
}
else
{
System.out.println("cancle");
super.dispose();
ll.setVisible(true);
}
}
|
其实这种例子论坛上挺多的,可以搜索一下,下面是我以前回答的
通过ActionEvent来获得事件源
下面是一个例子
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Frame1 extends JFrame implements ActionListener {
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
public Frame1() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(800,600);
frame1.setVisible(true);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jButton2.addActionListener(this);
jButton2.setText("jButton2");
jButton2.setBounds(new Rectangle(177, 135, 56, 21));
jButton3.setText("jButton3");
jButton3.addActionListener(this);
jButton3.setBounds(new Rectangle(257, 140, 54, 23));
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton3, null);
}
public void actionPerformed(ActionEvent e){
System.out.println(e.getSource());
System.out.println(e.getActionCommand());
}
}
通过ActionEvent来获得事件源
下面是一个例子
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Frame1 extends JFrame implements ActionListener {
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
public Frame1() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(800,600);
frame1.setVisible(true);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jButton2.addActionListener(this);
jButton2.setText("jButton2");
jButton2.setBounds(new Rectangle(177, 135, 56, 21));
jButton3.setText("jButton3");
jButton3.addActionListener(this);
jButton3.setBounds(new Rectangle(257, 140, 54, 23));
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton3, null);
}
public void actionPerformed(ActionEvent e){
System.out.println(e.getSource());
System.out.println(e.getActionCommand());
}
}
|
if(request.getParameter("button").equals("button1"))
out.println("Button1 pressed!");
else if(request.getParameter("button").equals("button2"))
out.println("Button1 pressed!");