用手机键盘格式写入数字转换器

时间:2015-03-08 07:17:00

标签: java user-interface

我在我的程序中遇到了麻烦。 该指令是转换用户输入的字母,并在电话键盘中输出相应的相应数字。 我一直有同样的错误:不兼容的类型

这是我到目前为止所做的:

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.text.DecimalFormat;

public class phone extends JFrame implements ActionListener
{
    JLabel num = new JLabel("Telephone Number(phones Format): ");   
    JButton btn1 = new JButton("   Convert   ");
    JLabel tel = new JLabel("Number Format: ");


    JTextField txt1 = new JTextField(10);
    JTextField txt2 = new JTextField(10);

    Container c = getContentPane();

    public phone()
    {
        super("Telephone");
        c.setLayout(new FlowLayout());

        c.add(num);
        c.add(txt1);
        c.add(btn1);
        c.add(tel);
        c.add(txt2);

        btn1.addActionListener(this);

        this.setSize(160,260);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setResizable(false);                   
    }

    public void actionPerformed(ActionEvent a)
    {
        String txt=txt1.getText();
    int x=0;
        String [] phones = new String[8];
        for( int i = 0; i<=8;i++)
        {

    if (phones=="A" || phones=="B" || phones== "C")
         {
            x=2;
         }

         else if(phones=="D" || phones=="E" || phones== "F")
         {
           x=3;
         }

        else if (phones=="G" || phones=="H" || phones== "I")
         {
           x=4;   
         }
         else if (phones=="J" || phones=="K" || phones== "L")
         {
            x=5;    
         }
         else if (phones=="M" || phones=="N" || phones== "O")
         {
            x=6;    
         }    

        else if (phones=="P" || phones=="Q" || phones== "R" || phones== "S")
         {
           x=7;    
         }    

        else if (phones=="T" || phones=="U" || phones== "V")
         {
            x=8;    
         }    

         else if (phones=="W" || phones=="X" || phones== "Y" || phones== "Z")
         {
            x=9;    
         }    


        }
        txt2.setText(x);
    }
    public static void main(String args[])
    {
        phone M = new phone();
    }
}

更新

我设法做了一些改动,但我现在的问题是格式化部分。

格式应为:### - ####

这是更新:

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.text.DecimalFormat;

public class phone extends JFrame implements ActionListener
{
    JLabel num = new JLabel("Telephone Number(txt Format): ");  
    JButton btn1 = new JButton("   Convert   ");
    JLabel tel = new JLabel("Number Format: ");


    JTextField txt1 = new JTextField(10);
    JTextField txt2 = new JTextField(10);

    Container c = getContentPane();

    public phone()
    {
        super("Telephone");
        c.setLayout(new FlowLayout());

        c.add(num);
        c.add(txt1);
        c.add(btn1);
        c.add(tel);
        c.add(txt2);

        btn1.addActionListener(this);

        this.setSize(160,260);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setResizable(false);                   
    }

    public void actionPerformed(ActionEvent a)
    {
        String pattern="###-####";
        DecimalFormat df=new DecimalFormat(pattern);
        String txt= txt1.getText();
           char[] data = txt.toCharArray();
        for (int i=0; i<data.length; ++i) {
            char c = data[i];
            c = Character.toLowerCase(c);
            switch (c) {
                case 'a':
                case 'b':
                case 'c':   data[i] = '2';
                            break;
                case 'd':
                case 'e':
                case 'f':   data[i] = '3';
                            break;
                case 'g':
                case 'h':
                case 'i':   data[i] = '4';
                            break;
                case 'j':
                case 'k':
                case 'l':   data[i] = '5';
                            break;
                case 'm':
                case 'n':
                case 'o':   data[i] = '6';
                            break;
                case 'p':
                case 'q':
                case 'r':
                case 's':   data[i] = '7';
                            break;
                case 't':
                case 'u':
                case 'v':   data[i] = '8';
                            break;
                case 'w':
                case 'x':
                case 'y':
                case 'z':   data[i] = '9';
            }
        }
        String l=df.format(String.valueOf(data));

        txt2.setText(l);
    }
    public static void main(String args[])
    {
        phone M = new phone();
    }
}

1 个答案:

答案 0 :(得分:2)

phones是一个String数组(String []),而不是String。您正在尝试将其与String进行比较,这就是您收到incompatible types错误

的原因

其他一些注释/问题: