java按钮事件添加
我想让按着数字,然后就在文本域显示数字,跟我们拨打电话一样,怎么实现,代码如下:importjava.awt.*;importjava.awt.event.*;impor...
我想让按着数字,然后就在文本域显示数字,跟我们拨打电话一样,怎么实现,代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Phone extends JFrame{
private JFrame jf;
private JPanel p1,p2,p3;
private JTextArea jta;
public Phone(){
jf=new JFrame("吴旭制作");
jf.setSize(180,370);
jf.setLocation(200,200);
setPan1();
jf.add(p1,BorderLayout.NORTH);
setPan2();
jf.add(p2,BorderLayout.SOUTH);
setPan3();
jf.add(p3,BorderLayout.CENTER);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setPan1(){
p1=new JPanel();
final CardLayout c1=new CardLayout();
p1.setLayout(c1);
JButton bt1=new JButton("Welcome to NOKIA");
JButton bt2=new JButton("NOKIA 5200");
ActionListener a1=new ActionListener(){
public void actionPerformed(ActionEvent e){
c1.next(p1);
}
};
bt1.addActionListener(a1);
bt2.addActionListener(a1);
p1.add(bt1,"1");
p1.add(bt2,"2");
}
public void setPan2(){
p2=new JPanel();
p2.setLayout(new GridLayout(4,3));
String[] name={"1","2","3","4","5","6","7","8","9","*","0","#"};
for(int i=0;i<name.length;i++){
p2.add(new JButton(name[i]));
}
}
public void setPan3(){
p3=new JPanel();
jta=new JTextArea(10,13);
p3.add(new JScrollPane(jta));
}
public static void main(String args[]){
Phone ph=new Phone();
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Phone extends JFrame{
private JFrame jf;
private JPanel p1,p2,p3;
private JTextArea jta;
public Phone(){
jf=new JFrame("吴旭制作");
jf.setSize(180,370);
jf.setLocation(200,200);
setPan1();
jf.add(p1,BorderLayout.NORTH);
setPan2();
jf.add(p2,BorderLayout.SOUTH);
setPan3();
jf.add(p3,BorderLayout.CENTER);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setPan1(){
p1=new JPanel();
final CardLayout c1=new CardLayout();
p1.setLayout(c1);
JButton bt1=new JButton("Welcome to NOKIA");
JButton bt2=new JButton("NOKIA 5200");
ActionListener a1=new ActionListener(){
public void actionPerformed(ActionEvent e){
c1.next(p1);
}
};
bt1.addActionListener(a1);
bt2.addActionListener(a1);
p1.add(bt1,"1");
p1.add(bt2,"2");
}
public void setPan2(){
p2=new JPanel();
p2.setLayout(new GridLayout(4,3));
String[] name={"1","2","3","4","5","6","7","8","9","*","0","#"};
for(int i=0;i<name.length;i++){
p2.add(new JButton(name[i]));
}
}
public void setPan3(){
p3=new JPanel();
jta=new JTextArea(10,13);
p3.add(new JScrollPane(jta));
}
public static void main(String args[]){
Phone ph=new Phone();
}
} 展开
2个回答
展开全部
jf.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
char key = e.getKeyChar();
String temp=jta.getText();
if(key.equals('1')){
jta.setText(temp+"1");
}
if(key.equals('2')){
jta.setText(temp+"2");
}
...
}
});
就这样。。。
{
@Override
public void keyPressed(KeyEvent e)
{
char key = e.getKeyChar();
String temp=jta.getText();
if(key.equals('1')){
jta.setText(temp+"1");
}
if(key.equals('2')){
jta.setText(temp+"2");
}
...
}
});
就这样。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询