import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame implements ActionListener{
JButton jb;
FlowLayoutDemo(){
super("FlowLayoutDemo布局");
// Container c=f.getContentPane();
FlowLayout flow=new FlowLayout(FlowLayout.CENTER,10,10);
getContentPane().setLayout(flow);
for(int i=0;i<=9;i++){
jb=new JButton(""+i);
jb.addActionListener(this);
add(jb);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb){System.exit(0);}
}
public static void main(String args[]){
JFrame f=new FlowLayoutDemo();
f.setSize(300,200);
f.setVisible(true);
}
}
请问各位大侠:为什么只有最后一个按钮响应事件?怎么能让所有按钮都响应此事件呢?请帮忙指正,谢谢!
import java.awt.event.*;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame implements ActionListener{
JButton jb;
FlowLayoutDemo(){
super("FlowLayoutDemo布局");
// Container c=f.getContentPane();
FlowLayout flow=new FlowLayout(FlowLayout.CENTER,10,10);
getContentPane().setLayout(flow);
for(int i=0;i<=9;i++){
jb=new JButton(""+i);
jb.addActionListener(this);
add(jb);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb){System.exit(0);}
}
public static void main(String args[]){
JFrame f=new FlowLayoutDemo();
f.setSize(300,200);
f.setVisible(true);
}
}
请问各位大侠:为什么只有最后一个按钮响应事件?怎么能让所有按钮都响应此事件呢?请帮忙指正,谢谢!
解决方案 »
- java停止线程
- 应该是小问题,但是不知道为什么。。。。。
- 请问在一个页面表单里 如何用javascript调用exe文件 望高手指点 写出代码
- 帮我看看这个问题怎么解决,单步执行和普通执行程序结果不同~郁闷
- 我用JBUILDER打包的APPLET程序,在有的电脑上运行不了,说找不到类
- 新手,请大侠指路!
- 在看<thinking in java>的时候遇到一个inner class的问题,请各位帮忙解答一下
- 特别初级的问题,我想开始学习java了,下载那些软件以及如何配置开发环境啊?
- 拜托各位结贴时,选择是否提交FAQ时请注意一下!!!!
- listFiles的用法
- 学习一下。
- 枚举值中定义了方法如何使用
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class FlowLayoutDemo extends JFrame implements ActionListener{
JButton[] jb = new JButton[10]; FlowLayoutDemo(){
super("FlowLayoutDemo布局");
// Container c=f.getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.CENTER,10,10);
getContentPane().setLayout(flow);
for(int i = 0;i <= 9;i++){
jb[i] = new JButton("" + i);
jb[i].addActionListener(this);
add(jb[i]);
}
} public void actionPerformed(ActionEvent e){
if(e.getSource() == jb[0]){
System.out.println("jb0");
}
if(e.getSource() == jb[9]){
System.exit(0);
}
} public static void main(String args[]){
JFrame f = new FlowLayoutDemo();
f.setSize(300,200);
f.setVisible(true);
}
}
if(e.getSource()==jb){System.exit(0);}
}
一个一个的判断 ,呵呵
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutDemo extends JFrame implements ActionListener{
JButton jb;
FlowLayoutDemo(){
super("FlowLayoutDemo布局");
// Container c=f.getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.CENTER,10,10);
getContentPane().setLayout(flow);
for(int i = 0;i <= 9;i++){
jb = new JButton("" + i);
jb.addActionListener(this);
add(jb);
}
}
public void actionPerformed(ActionEvent e){
JButton jb = (JButton)e.getSource();
if(jb.getActionCommand().equals("9")){
System.exit(0);
}
if(jb.getActionCommand().equals("1")){
System.out.println("1");
}
}
public static void main(String args[]){
JFrame f = new FlowLayoutDemo();
f.setSize(300,200);
f.setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
class FlowLayoutDemo extends JFrame implements ActionListener{
JButton jb[]=new JButton[10];
FlowLayoutDemo(){
super("FlowLayoutDemo布局");
// Container c=f.getContentPane();
FlowLayout flow=new FlowLayout(FlowLayout.CENTER,10,10);
getContentPane().setLayout(flow);
for(int i=0;i <=9;i++){
jb[i]=new JButton(""+i);
jb[i].addActionListener(this);
add(jb[i]);
}
}
public void actionPerformed(ActionEvent e){
for (int i = 0; i < jb.length; i++) {
if(e.getSource()==jb[i]){System.exit(0);}
}
}
public static void main(String args[]){
JFrame f=new FlowLayoutDemo();
f.setSize(300,200);
f.setVisible(true);
}
}