『壹』 請教java中的按鈕監聽事件代碼

把下面兩個類去了,直接用匿名內部類來實現...

b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.print(jt.getText());
}

});

b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
jt.setText("");
DoubleColorBall dbcb = new DoubleColorBall();
try {
// 根據文本框里輸入的數字,調用DoubleColorBall中的方法生成幸運
String[] num = dbcb.getNumber(Integer
.parseInt(jf.getText()));
for (int i = 0; i < num.length; i++) {
jt.append(num[i] + "\n");
}
} catch (Exception e) {
e.printStackTrace();
jt.setText("請輸入正整數數字");

}
}
});

『貳』 JAVA添加按鈕監聽器

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class ShowButton implements ActionListener{
private JTextField textField = null;
private JButton button = null;
private JFrame frame = null;

public ShowButton(){
frame = new JFrame("顯示按鈕文字");
button = new JButton("3");
textField = new JTextField("",20);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.add(textField);
button.addActionListener(this);
frame.add(button);
frame.setSize(400, 100);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
textField.setText("");
textField.setText(button.getActionCommand());
System.out.println(button.getActionCommand()+"===");
}

public static void main(String[] args) {
new ShowButton();
}

}

『叄』 java 按鈕監聽

jb1.addActionListener(this);// 你沒有為按鈕添加監聽器吧!,類似這種格式

『肆』 java如何監聽所有按鈕

使用ActionListener:

ActionListener action = new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
tf.setText(tf.getText()+e.getActionCommand());
}
}
numberbutton.addActionListener(action);
如果JButton沒有調用setActionCommand方法。e.getActionCommand()返回的就和JButton的getText()一致。

===========================================
另一種方法是使用Action:

public class NumberAction extends AbstractAction {
private static final long serialVersionUID = -3941319225168433304L;
private transient JTextField input;

public NumberAction(final String label, final JTextField input) {
super(label);
this.input = input;
}

@Override
public void actionPerformed(final ActionEvent e) {
input.setText(input.getText() + e.getActionCommand());
}
}
final JTextField input = ...;
for (int i = 0; i < 10; i++) {
add(new JButton(new NumberAction(Integer.toString(i), input)));
}

『伍』 JAVA中的按鈕監聽事件~

設一個boolean變數 點一下改變他的值

『陸』 java 按鈕添加監聽

xx.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
//
}
});

『柒』 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沒學,試了下,能達到效果。

『捌』 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);
}
});
}
}

在你的基礎上改了一下,界面什麼的沒有改

『玖』 JAVA中如何給按鈕做監聽

你a[i][j].addActionListener(this); 這句就是給按鈕加監聽啊,你想給哪些按鈕加就在哪些按鈕上調用addActionListener(this);方法。
你的類還實現ActionListener介面,並補全actionPerformed方法,添加監聽的方法才不會報錯。
有問題的話再問,把問題描述的具體些。