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方法,添加監聽的方法才不會報錯。
有問題的話再問,把問題描述的具體些。