且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

逻辑错误或重绘/重新验证Java JFrame

更新时间:2023-02-05 15:47:11

我建议您使用InputVerifier,因为这将在允许之前验证JTextField的内容是否正确(以任何方式定义此内容)你甚至离开了JTextField。现在它不会阻止你按下其他JButton等等,所以如果你有一个提交按钮,你需要采取其他预防措施。检查JTextField是否为空的简单InputVerifier示例如下所示:

I recommend that you use an InputVerifier as this will verify that the contents of the JTextField are correct (any way that you wish to define this) before allowing you to even leave the JTextField. Now it won't stop you from pressing other JButtons and whatnot, so you'll need to take other precautions if you have a submit button. An example of a simple InputVerifier that checks to see if the JTextField is empty is shown below:

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

@SuppressWarnings("serial")
public class InputVerifierExample extends JPanel {
   public static final Color WARNING_COLOR = Color.red;
   private JTextField firstNameField = new JTextField(10);
   private JTextField middleNameField = new JTextField(10);
   private JTextField lastNameField = new JTextField(10);
   private JTextField[] nameFields = { 
         firstNameField, 
         middleNameField,
         lastNameField };
   private JLabel warningLabel = new JLabel("  ");

   public InputVerifierExample() {
      warningLabel.setOpaque(true);

      JPanel namePanel = new JPanel();
      namePanel.add(new JLabel("Name:"));
      MyInputVerifier verifier = new MyInputVerifier();
      for (JTextField field : nameFields) {
         field.setInputVerifier(verifier);
         namePanel.add(field);
      }
      namePanel.add(new JButton(new SubmitBtnAction()));

      setLayout(new BorderLayout());
      add(namePanel, BorderLayout.CENTER);
      add(warningLabel, BorderLayout.SOUTH);
   }

   private class SubmitBtnAction extends AbstractAction {
      public SubmitBtnAction() {
         super("Submit");
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         // first check all fields aren't empty
         for (JTextField field : nameFields) {
            if (field.getText().trim().isEmpty()) {
               return;  // return if empty
            }
         }
         String name = "";
         for (JTextField field : nameFields) {
            name += field.getText() + " ";
            field.setText("");
         }
         name = name.trim();
         JOptionPane.showMessageDialog(InputVerifierExample.this, name, "Name Entered",
               JOptionPane.INFORMATION_MESSAGE);
      }
   }

   private class MyInputVerifier extends InputVerifier {

      @Override
      public boolean verify(JComponent input) {
         JTextField field = (JTextField) input;
         if (field.getText().trim().isEmpty()) {
            warningLabel.setText("Please do not leave this field empty");
            warningLabel.setBackground(WARNING_COLOR);
            return false;
         }
         warningLabel.setText("");
         warningLabel.setBackground(null);
         return true;
      }

   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("InputVerifier Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new InputVerifierExample());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}