这是我到目前为止所做的:
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();
}
}
答案 0 :(得分:2)
phones
是一个String数组(String []
),而不是String
。您正在尝试将其与String进行比较,这就是您收到incompatible types
错误
其他一些注释/问题:
phone
课程应命名为Phone
。 "Class names should be nouns, in mixed case with the first letter of each internal word capitalized" phones=="A"
。字符串比较不起作用。请参阅this StackOverflow问题,详细了解为什么这不起作用以及如何比较字符串txt2.setText(x);
会给您另一个错误,因为x
是int
,并且该方法需要String
作为输入this.setDefaultCloseOperation(3);
- 您应该使用WindowConstants
中的一个常量,而不是硬编码的3
。这将转化为this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);