import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class calculator extends JFrame implements ActionListener
{
JTextField jtext;
JButton[] num=new JButton[10];
JButton a,b,c,d,e,f,down; public calculator()
{
Container con=getContentPane();
con.setLayout(new BorderLayout());
jtext=new JTextField(10);
jtext.setVisible(true);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel(); p1.setLayout(new GridLayout(3,3));
p2.setLayout(new GridLayout(3,2));
p3.setLayout(new GridLayout(1,1));
//用循环添加数字1~9 为各自注册监听器 ,并将其添加到面板p1
for (int i=0;i<9;i++)
{
num[i]=new JButton(" "+(i+1));
p1.add(num[i]);
num[i].addActionListener(this);
} JButton a=new JButton(""+0);
JButton b=new JButton("AC");
JButton c=new JButton("+");
JButton d=new JButton("-");
JButton e=new JButton("*");
JButton f=new JButton("/");
JButton down=new JButton("=");
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
e.addActionListener(this);
f.addActionListener(this);
down.addActionListener(this);
p2.add(a);
p2.add(b);
p2.add(c);
p2.add(d);
p2.add(e);
p2.add(f);
p3.add(down);
con.add(p1,BorderLayout.CENTER);
con.add(jtext,BorderLayout.NORTH);
con.add(p2,BorderLayout.EAST);
con.add(p3,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<9;i++)
{
if(e.getSource().equals(num[i]))
jtext.setText(jtext.getText() +e.getActionCommand());
}
if(e.getSource()==a)
jtext.setText(jtext.getText()+e.getActionCommand());
System.out.println(e.getSource());
if(e.getSource()==b)
jtext.setText("");
if(e.getSource()==c)
jtext.setText("+");
if(e.getSource()==d)
jtext.setText("-");
if(e.getSource()==e)
jtext.setText("*");
if(e.getSource()==f)
jtext.setText("/");
}
public static void main(String[] args)
{
calculator cal=new calculator();
cal.setSize(270,270);
cal.setVisible(true);
}
}我点0 按钮怎么没有反应啊
打印出来e.getSource()发现注册监听器成功的了
大家能告诉我是怎么回事吗
难道p1面板上的按钮能反应,p2上的就不能了吗?
import javax.swing.*;
import java.awt.event.*;public class calculator extends JFrame implements ActionListener
{
JTextField jtext;
JButton[] num=new JButton[10];
JButton a,b,c,d,e,f,down; public calculator()
{
Container con=getContentPane();
con.setLayout(new BorderLayout());
jtext=new JTextField(10);
jtext.setVisible(true);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel(); p1.setLayout(new GridLayout(3,3));
p2.setLayout(new GridLayout(3,2));
p3.setLayout(new GridLayout(1,1));
//用循环添加数字1~9 为各自注册监听器 ,并将其添加到面板p1
for (int i=0;i<9;i++)
{
num[i]=new JButton(" "+(i+1));
p1.add(num[i]);
num[i].addActionListener(this);
} JButton a=new JButton(""+0);
JButton b=new JButton("AC");
JButton c=new JButton("+");
JButton d=new JButton("-");
JButton e=new JButton("*");
JButton f=new JButton("/");
JButton down=new JButton("=");
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
e.addActionListener(this);
f.addActionListener(this);
down.addActionListener(this);
p2.add(a);
p2.add(b);
p2.add(c);
p2.add(d);
p2.add(e);
p2.add(f);
p3.add(down);
con.add(p1,BorderLayout.CENTER);
con.add(jtext,BorderLayout.NORTH);
con.add(p2,BorderLayout.EAST);
con.add(p3,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<9;i++)
{
if(e.getSource().equals(num[i]))
jtext.setText(jtext.getText() +e.getActionCommand());
}
if(e.getSource()==a)
jtext.setText(jtext.getText()+e.getActionCommand());
System.out.println(e.getSource());
if(e.getSource()==b)
jtext.setText("");
if(e.getSource()==c)
jtext.setText("+");
if(e.getSource()==d)
jtext.setText("-");
if(e.getSource()==e)
jtext.setText("*");
if(e.getSource()==f)
jtext.setText("/");
}
public static void main(String[] args)
{
calculator cal=new calculator();
cal.setSize(270,270);
cal.setVisible(true);
}
}我点0 按钮怎么没有反应啊
打印出来e.getSource()发现注册监听器成功的了
大家能告诉我是怎么回事吗
难道p1面板上的按钮能反应,p2上的就不能了吗?
解决方案 »
- java怎么把最后一个位移
- jtable
- 简单的Socket,为什么不行
- 一个sourceStr.replaceFirst(regStr,repStr)的问题,返回“Illegal Group Reference”异常,请大虾帮忙!
- 用java程序,如何关机,重启机器,有没有这样的api?
- 不能识别中文的问题
- java在linux下面调试的怪问题。
- 菜鸟提问:ListStudents.java:24: unreported exception java.sql.SQLException; must be caught or declared to be thrown
- 请问:如何提高查询的效率和优化数据库
- How to run Java applications in command line?
- 按值排序问题
- 有谁能推荐一些把java程序编译成exe的软件?
JButton b=new JButton("AC");
JButton c=new JButton("+");
JButton d=new JButton("-");
JButton e=new JButton("*");
JButton f=new JButton("/");
JButton down=new JButton("=");
改成
a=new JButton(""+0);
b=new JButton("AC");
c=new JButton("+");
d=new JButton("-");
e=new JButton("*");
f=new JButton("/");
down=new JButton("=");
否则这里面定义的改变不了外面的值