检查用户何时未在JTextArea中输入任何内容

时间:2018-11-23 07:15:59

标签: java equals jtextarea

我已经用Java编写了以下代码,以检查用户是否在JTextArea中没有条目。 仅JTextArea.getText()。equals(“”)有效,而其他则无效。 如果您可以解释为什么其他检查无效的话,这将有很大帮助。

谢谢

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class TestJTextArea{
  private JFrame frame;
  private JPanel panel;
  private JLabel textLabel;
  private JTextArea textArea;
  private JButton submitButton;

  public TestJTextArea(){
    frame = new JFrame("Test JTextArea");
    panel = new JPanel();
    textLabel = new JLabel("Enter your Text below");
    textArea = new JTextArea(20,20);
    submitButton = new JButton("Submit");

    setupGUI();
  }

  private void setupGUI(){
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    textArea.setFont(new Font("sansserif", Font.BOLD, 24));

    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    panel.add(textLabel);
    panel.add(scrollPane);
    panel.add(submitButton);
    submitButton.addActionListener(new SubmitButtonListener());

    frame.setSize(200,200);
    frame.getContentPane().add(BorderLayout.CENTER,panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  private class SubmitButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

检查equals(“”)是否有效。因此,以下代码将抛出True:

if(textArea.getText().equals("")){
        JOptionPane.showMessageDialog(null,"No Text test True for equals()","Check equality through equals()",JOptionPane.WARNING_MESSAGE);
      }
      else
        JOptionPane.showMessageDialog(null,"No Text test False for equals()","Check equality through equals()",JOptionPane.WARNING_MESSAGE);

下面的代码抛出False:

  if(textArea.getText() == ""){
    JOptionPane.showMessageDialog(null,"No Text test True for ==","Check equality through ==",JOptionPane.WARNING_MESSAGE);
  }      
  else
    JOptionPane.showMessageDialog(null,"No Text test False for ==","Check equality through ==",JOptionPane.WARNING_MESSAGE);

下面的代码抛出False:

 if(textArea.getText() == null){
    JOptionPane.showMessageDialog(null,"No Text test True for null","Check equality through null",JOptionPane.WARNING_MESSAGE);
  }      
  else
    JOptionPane.showMessageDialog(null,"No Text test False for null","Check equality through null",JOptionPane.WARNING_MESSAGE);

以下所有代码均抛出False:

  if(textArea.getText().toString() == ""){
    JOptionPane.showMessageDialog(null,"No Text test True for toString()","Check equality through toString()",JOptionPane.WARNING_MESSAGE);
  }      
  else
    JOptionPane.showMessageDialog(null,"No Text test False for toString()","Check equality through toString()",JOptionPane.WARNING_MESSAGE);

  if(textArea.getText().toString() == null){
    JOptionPane.showMessageDialog(null,"No Text test True for toString() = null","Check equality through toString() = null",JOptionPane.WARNING_MESSAGE);
  }      
  else
    JOptionPane.showMessageDialog(null,"No Text test False for toString() = null","Check equality through toString() = null",JOptionPane.WARNING_MESSAGE);

1 个答案:

答案 0 :(得分:0)

将String与==运算符进行比较将比较引用,这意味着它仅在相同的String实例时才有效。要检查字符串的值是否相同,应始终使用equals方法。

好的方法是使用StringUtils方法IsEmpty / IsBlank-检查字符串是否包含文本https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html