java简单拨号器界面

java简单拨号器界面_第1张图片
拨号器界面灰常简单,一个文本框和一个Jpanel面板,面板里是5*3的网格布局,就不详细叙述了,代码如下

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
 * 
 * @author lc
 * 
 *
 */
public class CallLayout extends JFrame{
    public static void main(String[] args) {
        CallLayout  callLayout = new CallLayout();
        callLayout.setTitle("拨号器");

    }
    //定义字符串数组,为按钮显示文本赋值
    String str[] = {"1","2","3","4","5","6","7","8","9","*","0","#","拨号","挂断","嘻嘻"};
    JTextField textField;
    String flag="";
    public CallLayout() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);//设置可以点击x号退出
        JPanel panel = new JPanel();//设置面板
        panel.setLayout(new GridLayout(5, 3));//面板里里面设置5*3的网格布局
        setLayout(new BorderLayout());//设置边界布局  是最下面的一层,上一层是面板和文本框
        for(int i=0;i<15;i++){
            JButton btn = new JButton(str[i]);
            btn.addActionListener(new BtnClick());
            panel.add(btn);

        }
        textField = new JTextField(); //定义文本框
        getContentPane().add(textField, BorderLayout.NORTH);//将文本框放置在窗体的上方
        getContentPane().add(panel, BorderLayout.CENTER);//将面板放在center位置
        setSize(320,320);//设置窗体大小
        setLocationRelativeTo(null);//窗体居中显示    

        setResizable(false);
        setVisible(true);
    }
    class BtnClick implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            JButton btn=(JButton)e.getSource();

            if (btn.getText().equals("嘻嘻")) {
                if (textField.getText().length()>0) {
                    if ( flag.equals("拨号") || flag.equals("挂断")) {
                        textField.setText("");
                    }else {
                        textField.setText(textField.getText().substring(0,textField.getText().length()-1));
                    }

                }

            }
            else if (btn.getText().equals("拨号")) {
                textField.setText("拨号成功");
            }
            else if (btn.getText().equals("挂断")) {
                textField.setText("已挂断");
            }
            else {
                if ( flag.equals("拨号") || flag.equals("挂断")) {
                    textField.setText(btn.getText());
                }else {
                    textField.setText(textField.getText()+btn.getText());
                }

            }
            flag=btn.getText();

        }

    }
}

你可能感兴趣的:(java学习笔记)