java按钮监听
A. java用监听事件监听两个按钮
你问的是JAVASE么?
使用Button的addActionListener就好了
JFrameframe=newJFrame();
frame.setTitle("myframe");
frame.setBounds(0,0,200,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButtonbtn1=newJButton("OpenBrowser");
JButtonbtn2=newJButton("OpenNotepad");
JPanelpanel=newJPanel(newFlowLayout(4));
btn1.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getActionCommand().equals("OpenBrowser")){
try{
Processp=Runtime.getRuntime().exec("explorerhttp://www.qq.com");
p.destroy();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
});
btn2.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
try{
Processp=Runtime.getRuntime().exec("notepad");
p.destroy();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
});
panel.add(btn1);
panel.add(btn2);
frame.add(panel);
frame.setVisible(true);
B. java 怎么监听 另一个窗口按钮
在另一个类中,实现Get方法,或者将按钮设为public。
然后,在本类中实现监听接口,将另一个类中的按钮的监听接口设为本类
然后设置发送的ActionCommand
C. java如何监听所有按钮
先定义一个监听器al:
ActionListener al = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String str = btn1.getText();
field.setText(str);
}
};
然后所有button都调用addActionListener(al);这样al就可以监听所有button的点击事件了.通过e.getSource()获取哪个按钮,然后获取按钮上的数字即可.
D. java中怎样为多个按钮设置监听
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
publicclassOne
{
publicstaticvoidmain(String[]args)
{
JFramef=newJFrame("JAVA小游戏");
//设置f布局管理器为3行3列,组件间水平和垂直间距都为2
f.setLayout(newGridLayout(3,3,2,2));
for(inti=0;i<9;i++)
{
if(i%2==0)
{
JButtonbtn=newJButton("+");
btn.addMouseListener(newMouseMonitor(btn));
f.add(btn);
}
else
{
JButtonbtn=newJButton("-");
btn.addMouseListener(newMouseMonitor2(btn));
f.add(btn);
}
}
f.setSize(300,200);
f.setVisible(true);
f.setResizable(false);
}
}
{
JButtonjbt;
publicMouseMonitor(JButtonx)
{
this.jbt=x;
}
publicvoidmouseClicked(MouseEvente)
{
jbt.setText("-");
}
}
{
JButtonjbt;
publicMouseMonitor2(JButtonx)
{
this.jbt=x;
}
publicvoidmouseClicked(MouseEvente)
{
jbt.setText("+");
}
}
刚学的AWT,Swing没学,试了下,能达到效果。
E. java 给按钮加监听的快捷键是什么(快捷键加监听减少写代码的时间)
ALT+/可以实现快速的代码补全
JButtonjb=newJButton("测试");
jb.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//你的代码
}
});
当然也可以自定义 快捷键 实现快速补全
第一步 Window -->Preferences 打开配置
F. javaswing 按钮监听问题
因为你这个类实现了ActionListener接口,所以这个类本身就是一个监听器,this指的就是这个监听器类的对象,就是说用这个类的对象做为监听器监听着sub这个按钮发出的行为事件。
G. java 按钮监听
jb1.addActionListener(this);// 你没有为按钮添加监听器吧!,类似这种格式
H. Java按钮监听
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.math.BigDecimal;
importjava.util.Scanner;
importjavax.swing.JFrame;
importjava.awt.Button;
importjava.awt.Label;
importjava.awt.TextField;
importjava.awt.Frame;
importjava.awt.Panel;
importjava.awt.Color;
importjava.awt.*;
publicclassPanelTest{
publicstaticvoidmain(Stringargs[]){
/*Scannersc=newScanner(System.in);doublepi=3.14,s;doubler;r=sc.nextDouble();s=pi*r*r;System.out.println("s等于"+s);*/
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
CricleFrameframe=newCricleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
classCricleFrameextendsJFrame{
Panelp=newPanel();
TextFieldt=newTextField();
Buttonb=newButton("确定");
Labela=newLabel("请在此输入半径");
TextFieldresult=newTextField();
publicCricleFrame(){
add(a);
add(t);
add(b);
add(result);
add(p);
setVisible(true);
p.setBackground(Color.black);
a.setBackground(Color.yellow);
t.setBackground(Color.white);
result.setBackground(Color.white);
b.setBackground(Color.cyan);
setSize(300,300);
setTitle("圆的面积");
a.setBounds(105,45,90,25);
t.setBounds(100,80,100,25);
result.setBounds(100,180,100,25);
b.setBounds(111,120,80,40);
b.addActionListener(newActionListener(){//按钮点击事件监听
publicvoidactionPerformed(ActionEventevent){
Doubler=0.0;
try{
r=Double.parseDouble(t.getText());
}catch(Exceptione){
System.out.println(e.getMessage());
}
BigDecimaltmp=newBigDecimal(r*r*Math.PI);
Doublearea=tmp.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//保留2位小数
result.setText(""+area);
}
});
}
}
在你的基础上改了一下,界面什么的没有改
I. JAVA 对按钮注册事件监听时,需要实现的接口是
代码一
implements
ActionListener
说明:实现接口,没有为什么
代码二
b1.addActionListener(this)
说明:为b1注册,参数是指交给谁处理,这里是this
代码三
这里是做什么用的?
代码四
b1.setText(“已登录”)
好像是这个函数。
J. JAVA中如何给按钮做监听
你a[i][j].addActionListener(this); 这句就是给按钮加监听啊,你想给哪些按钮加就在哪些按钮上调用addActionListener(this);方法。
你的类还实现ActionListener接口,并补全actionPerformed方法,添加监听的方法才不会报错。
有问题的话再问,把问题描述的具体些。