电话拨号盘的实现
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Myframe50 extends JFrame implements ActionListener
{
JTextField txt1,txt2;
Mypanel1 p1;
public Myframe50()
{
setTitle("电话拨号盘");
txt1=new JTextField(20);
txt2=new JTextField(20);
p1=new Mypanel1();
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(txt1,BorderLayout.NORTH);
this.getContentPane().add(p1,BorderLayout.CENTER);
this.getContentPane().add(txt2,BorderLayout.SOUTH);
p1.b1.addActionListener(this);
p1.b2.addActionListener(this);
p1.b3.addActionListener(this);
p1.b4.addActionListener(this);
p1.b5.addActionListener(this);
p1.b6.addActionListener(this);
p1.b7.addActionListener(this);
p1.b8.addActionListener(this);
p1.b9.addActionListener(this);
p1.b10.addActionListener(this);
p1.b11.addActionListener(this);
p1.b12.addActionListener(this);
setSize(200,250);
setLocation(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==p1.b10)
{
txt1.setText("");
txt2.setText("");
}
else if(e.getSource()==p1.b12)
{
if(Long.parseLong(txt1.getText())==110||Long.parseLong(txt1.getText())==119)
{
txt2.setText("正在拨号:"+txt1.getText());
}
else if(txt1.getText().charAt(0)=='0')
{
txt2.setText("无效号码:"+txt1.getText());
}
else
{
if(txt1.getText().length()==8)
{
txt2.setText("正在拨号"+txt1.getText());
}
else
{
txt2.setText("不是市话");
}
}
}
}
public static void main(String args[])
{
new Myframe50();
}
}
class Mypanel1 extends JPanel
{
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12;
Font f;
public Mypanel1()
{
f=new Font("Arial",Font.BOLD,14);
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b10=new JButton("*");
b11=new JButton("0");
b12=new JButton("#");
b1.setFont(f);
b2.setFont(f);
b3.setFont(f);
b4.setFont(f);
b5.setFont(f);
b6.setFont(f);
b7.setFont(f);
b8.setFont(f);
b9.setFont(f);
b10.setFont(f);
b11.setFont(f);
b12.setFont(f);
this.setLayout(new GridLayout(4,3));
this.add(b1);
this.add(b2);
this.add(b3);
this.add(b4);
this.add(b5);
this.add(b6);
this.add(b7);
this.add(b8);
this.add(b9);
this.add(b10);
this.add(b11);
this.add(b12);
}
}
各位高人,我初学JAVA,我做了一个电话拨号盘的程序,在文本域中得到单击按钮的名称,比如:单击
b1,再次单击b1,有单击b11,在文本域中应该得到110,用JAVA咋实现?