java中取得点击那个按钮的文字
publicclasssdsadextendsJFrame{privateJButtonitem,item2;publicsdsad(){item=newJButton(...
public class sdsad extends JFrame {
private JButton item,item2;
public sdsad (){
item= new JButton("一");
this.add(item);
item.addMouseListener(new clickAction());
item2= new JButton("二");
this.add(item2);
item2.addMouseListener(new clickAction());
}
}
class clickAction implements MouseListener{
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
请问如何在clickAction的mouse(mousePressed,mouseReleased....)事件中取得点击时的那个button的文字(一或者二)
不要答我getText()=.=
想补充问一下:
以上的~如果我多加几个button(item3,item4,item5),当我点击后我想把item4和item5 颜色转变,请问怎样实现 展开
private JButton item,item2;
public sdsad (){
item= new JButton("一");
this.add(item);
item.addMouseListener(new clickAction());
item2= new JButton("二");
this.add(item2);
item2.addMouseListener(new clickAction());
}
}
class clickAction implements MouseListener{
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
请问如何在clickAction的mouse(mousePressed,mouseReleased....)事件中取得点击时的那个button的文字(一或者二)
不要答我getText()=.=
想补充问一下:
以上的~如果我多加几个button(item3,item4,item5),当我点击后我想把item4和item5 颜色转变,请问怎样实现 展开
展开全部
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class sdsad extends JFrame {
static JButton item,item2;
public sdsad (){
this.setLayout(new FlowLayout());
item= new JButton("一");
this.add(item);
item.addMouseListener( new clickAction());
item2= new JButton("二");
this.add(item2);
item2.addMouseListener(new clickAction());
this.setVisible(true);
this.setBounds(30, 30, 300, 300);
}
public static void main(String a[]){
new sdsad();
}
}
class clickAction implements MouseListener{
public void mouseClicked(MouseEvent e){
System.out.println("你点击的按钮上的文字是"+((JButton) (e.getSource())).getText() );
((JButton)e.getSource()).setBackground(Color.blue);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class sdsad extends JFrame {
static JButton item,item2;
public sdsad (){
this.setLayout(new FlowLayout());
item= new JButton("一");
this.add(item);
item.addMouseListener( new clickAction());
item2= new JButton("二");
this.add(item2);
item2.addMouseListener(new clickAction());
this.setVisible(true);
this.setBounds(30, 30, 300, 300);
}
public static void main(String a[]){
new sdsad();
}
}
class clickAction implements MouseListener{
public void mouseClicked(MouseEvent e){
System.out.println("你点击的按钮上的文字是"+((JButton) (e.getSource())).getText() );
((JButton)e.getSource()).setBackground(Color.blue);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
展开全部
private TextArea ta=new TextArea(6,50);
Button1.addActionListener(new FirstListener());
class FirstListener implements ActionListener{
public void actionPerformed(ActionEvent e){
ta.append("nin dan ji le:"+e.getActionCommand()+"\n");
}
}
也可用匿名内部类
Button1.addActionListener(new FirstListener());
class FirstListener implements ActionListener{
public void actionPerformed(ActionEvent e){
ta.append("nin dan ji le:"+e.getActionCommand()+"\n");
}
}
也可用匿名内部类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
按钮一般都是直接addactionlistener的。很少有你响应mouselistener的吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询