0

我目前在我的 Maven 项目中使用BalloonTip 1.2.4.1 。

我想根据用户输入的条件动态显示/隐藏 BalloonTip,但没有这样做。错误是NullPointerException

这就是我想要实现的:在应用程序启动时,气球将是不可见的。如果用户输入错误的输入,气球会弹出。如果用户输入正确,气球会消失。

package view;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import net.java.balloontip.BalloonTip;
import net.java.balloontip.styles.BalloonTipStyle;
import net.java.balloontip.styles.EdgedBalloonStyle;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class Frame extends JFrame {

    private JPanel contentPane;
    public JLabel lblUserID;
    public JTextField txtUserID;
    private BalloonTip bl;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame frame = new Frame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        lblUserID = new JLabel("UserID");
        lblUserID.setHorizontalAlignment(SwingConstants.CENTER);
        lblUserID.setBounds(46, 92, 69, 23);
        contentPane.add(lblUserID);
        
        txtUserID = new JTextField();
        txtUserID.setBounds(146, 92, 116, 23);
        contentPane.add(txtUserID);
        txtUserID.setColumns(10);
        txtUserID.getDocument().addDocumentListener(new DocumentListener() {    
            public void removeUpdate(DocumentEvent e) {
                check();                
            }
            
            public void insertUpdate(DocumentEvent e) {
                check();                
            }
            

            public void changedUpdate(DocumentEvent e) {
                check();                
            }
        });
        
        BalloonTipStyle edgedLook = new EdgedBalloonStyle(Color.WHITE, Color.RED);
        BalloonTip bl = new BalloonTip(txtUserID, "Please enter number as Personal ID", edgedLook, false);
        bl.setVisible(false);
        
    }
    
    private void check() {
        if (!txtUserID.getText().equals("") && txtUserID.getText().matches("\\d+")) {
            txtUserID.setBorder(new LineBorder(Color.GREEN, 1));
            if (bl.isVisible() == true) {
                bl.setVisible(false);
            }            
        } else {
            txtUserID.setBorder(new LineBorder(Color.RED, 1));
            bl.setVisible(true);
        }
    }
}
4

0 回答 0