你的这种方法不好,这样做吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class button extends JFrame
{
public button()
{
JButton jb1=new JButton(" 1 ");
JButton jb2=new JButton(" 2 ");
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jb1,BorderLayout.NORTH);
getContentPane().add(jb2,BorderLayout.SOUTH);
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb2());
setSize(300,300);
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
new button();
}
class action_jb1 implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
//do something
System.out.println("this is 1");
}
action_jb1(){}
}
class action_jb2 implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
//do something
System.out.println("this is 2");
}
action_jb2(){}
}
}如果你的两个button是事件是一样的,那么这两句
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb2());
就改为:
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb1());
也就是用同一个动作
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class button extends JFrame
{
public button()
{
JButton jb1=new JButton(" 1 ");
JButton jb2=new JButton(" 2 ");
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jb1,BorderLayout.NORTH);
getContentPane().add(jb2,BorderLayout.SOUTH);
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb2());
setSize(300,300);
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
new button();
}
class action_jb1 implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
//do something
System.out.println("this is 1");
}
action_jb1(){}
}
class action_jb2 implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
//do something
System.out.println("this is 2");
}
action_jb2(){}
}
}如果你的两个button是事件是一样的,那么这两句
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb2());
就改为:
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb1());
也就是用同一个动作
解决方案 »
- 建一个TreeSet<Short>数组的问题
- 中文支持问题
- java application 关闭前如何判断某个参数并决定是否要关闭程序?
- 朋友问我的问题,大家看看会不会。
- test
- 梅花雪的method使用问题?
- JComboBox中 如何插入图片和文字进行显示
- InstallAnyway发布java时遇到的问题,大侠帮忙!急!
- xml中NodeList为什么不能使用forEach循环
- HTTP Status 500 - Servlet.init() for servlet appServlet threw exception
- 我是想检查读出的字节和下一字节是不是相等,但从现在的输出来看,它输出的每一个字节都是显示 [B@77d134 ,如何进行字节的比较,用这种
- 关于rational rose使用的一个简单问题,在线等待
来检测按下了哪个按钮Object eveSrc = e.getSource();
if(eveSrc instanceof Button)
{
if(eveSrc == btn1)
{ public void windowClosing (WindowEvent e)
{ System.exit(0);
}
}class TestFrame extends Frame implements ActionListener
{ String Close="Close";
String Ok="Ok"; public TestFrame ()
{ super("Test Frame");
setSize(300,300);
Panel P=new Panel();
Button A=new Button(Ok);
A.addActionListener(this);
P.add(A);
add(P);
setVisible(true);
Button B=new Button(Close);
B.addActionListener(this);
P.add(B);
add(P);
setVisible(true);
addWindowListener(new Closer());
}
public void actionPerformed (ActionEvent e)
{ if (e.getActionCommand().equals(Close))
{ dispose(); System.exit(0); }
if (e.getActionCommand().equals(Ok))
{
//add code here!!
}
}
}你可以用getActionCommand来判断点了哪个button.
e.getSource() instanceof Button 的意思是判断event的发生源是否是一个button.
up
and up
}
///////////////////////////////////////////////////////////
void jButton3_actionPerformed(ActionEvent e) {
}
分开比较好