我制作了一个GUI程序,它计算在主文本字段中输入的内容的某些元素。如果文本字段为空,则应弹出一条消息,提示用户应在文本字段中输入文本。如果tfMain == null,我做了一个if语句,应该弹出一个JOptionPane消息,但是由于某种原因它不会。为什么没有弹出提示?
这是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class LabExcer9 extends WindowAdapter implements ActionListener
{
//Container
private Frame f;
private Panel p1,p2,p3;
//Component
private Button bReadAndComp, bClear;
private TextField tfMain,tf1,tf2,tf3,tf4,tf5,tf6;
private Label l1,l2,l3,l4,l5,l6;
public LabExcer9()
{
f = new Frame("Character Counter of Miguel Martin");
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
bReadAndComp = new Button("Read and Compute");
bClear = new Button("Clear ALL Values");
tfMain = new TextField(null);
tf1 = new TextField("0");
tf2 = new TextField("0");
tf3 = new TextField("0");
tf4 = new TextField("0");
tf5 = new TextField("0");
tf6 = new TextField("0");
l1 = new Label("Number of Words ");
l2 = new Label("Number of Characters ");
l3 = new Label("Number of Vowels ");
l4 = new Label("Number of Consonants ");
l5 = new Label("Number of Digits ");
l6 = new Label("Number of Symbols and Spaces ");
}
public void assembleGUI()
{
p1.setLayout(new GridLayout(1,1));
p1.setPreferredSize(new Dimension(200, 200));
p1.add(tfMain);
p2.setLayout(new GridLayout(6,2));
p2.add(l1);
p2.add(tf1);
p2.add(l2);
p2.add(tf2);
p2.add(l3);
p2.add(tf3);
p2.add(l4);
p2.add(tf4);
p2.add(l5);
p2.add(tf5);
p2.add(l6);
p2.add(tf6);
p3.setLayout(new GridLayout(1,2));
p3.add(bReadAndComp);
p3.add(bClear);
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.add(p3,BorderLayout.SOUTH);
f.pack();
f.addWindowListener(this);
f.setVisible(true);
//registers
bReadAndComp.addActionListener(this);
bClear.addActionListener(this);
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
//gets main text
String textString = tfMain.getText();
//puts words into array
int vowels = 0, consonants = 0, digits = 0, symbolsAndSpaces = 0;
int characters = textString.length();
if(tfMain.getText() == "" || tfMain.getText() == null )
{
if(source == bReadAndComp)
JOptionPane.showMessageDialog(null, "Please Enter Text!");
}
else if(tfMain.getText() != null && tfMain.getText() != "")
{
if(source == bReadAndComp)
{
for(int i = 0;i<textString.length();i++)
{
if(Character.isDigit(textString.charAt(i)))
digits++;
if(Character.isLetterOrDigit(textString.charAt(i)) == false)
symbolsAndSpaces++;
if(isVowel(textString.charAt(i)) == true)
vowels++;
else if(Character.isDigit(textString.charAt(i)) == false && isVowel(textString.charAt(i)) == false && textString.charAt(i) != ' ' && Character.isLetter(textString.charAt(i)) == true)
consonants++;
}
tf1.setText(""+textString.split(" ").length);
tf2.setText(""+characters);
tf3.setText(""+vowels);
tf4.setText(""+consonants);
tf5.setText(""+digits);
tf6.setText(""+symbolsAndSpaces);
//System.out.println(textStringArray[0]+"wat");
//JOptionPane.showMessageDialog(null, "Please Enter Text!");
}
else if (source == bClear)
{
tf1.setText("0");
tf2.setText("0");
tf3.setText("0");
tf4.setText("0");
tf5.setText("0");
tf6.setText("0");
tfMain.setText(null);
}
}
}
public boolean isVowel(char c)
{
if (c == 'a' || c == 'A' ||c== 'e' ||c == 'E' ||c == 'i' ||c == 'I' ||c == 'o' ||c == 'O' ||c == 'u' ||c == 'U')
return true;
else
return false;
}
public static void main(String args [])
{
LabExcer9 GUI = new LabExcer9();
GUI.assembleGUI();
}
}
要比较字符串,请使用equals或equalsIgnoreCase。
替换您的
if(tfMain.getText() == "" || tfMain.getText() == null )
带有以下内容的陈述:
if("".equals(tfMain.getText())){
}