简体   繁体   English

如何删除 jbutton 的颜色保持

[英]How to remove the color hold of a jbutton

When the validation is correct the si button is placed green, then I perform a incorrect validation and the si button is still in that color.当验证正确时,si 按钮被放置为绿色,然后我执行了不正确的验证并且 si 按钮仍为该颜色。

I tried to select the original color of both buttons, but it does not work for the si button.我试图选择两个按钮的原始颜色,但它不适用于 si 按钮。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

private void btn_validarActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String w_correo = caja_correo.getText();
    Pattern p_correo1 = Pattern.compile("^(?=.{1,129}$)[a-zA-Z][a-zA-Z][a-zA-Z]*\\.[a-zA-Z][a-zA-Z][a-zA-Z]*\\.(?:2019|20[2-9][0-9]|2[1-9][0-9]{2}|[3-9][0-9]{3})@([0-9]|[a-z]|[A-Z])+\\.edu.?[a-z]*$");
        Matcher m_correo1 = p_correo1.matcher(w_correo);
     Pattern p_correo2 = Pattern.compile("^(?=.{1,129}$)(?:2018|201[0-7]|200[0-9]|1[0-9]{1,3}|[0-9]{1,3})\\.[a-zA-Z][a-zA-Z][a-zA-Z]*\\.[a-zA-Z][a-zA-Z][a-zA-Z]*@\\w+(?:\\.\\w+)*\\.com\\.co$");
    Matcher m_correo2 = p_correo2.matcher(w_correo);    

    correcto.setBackground(null);  
    incorrecto.setBackground(null);

    if (m_correo1.matches()|| m_correo2.matches()) {
        correcto.setBackground(null);  
        incorrecto.setBackground(null);
        String validacion = "";
        validacion = validacion +  "Direccion de correo electrónico correcta";
        JOptionPane.showMessageDialog(null, validacion);
        correcto.setForeground(Color.GREEN);

    }

    else { 

        String validacion = "";
        if (!m_correo1.matches() || m_correo2.matches()) {
            correcto.setBackground(null);  
            incorrecto.setBackground(null);    
            validacion = validacion + "Direccion de correo electrónico incorrecta";
            JOptionPane.showMessageDialog(null, validacion);
                incorrecto.setBackground(Color.RED);
        }
    }
}

that I must change so that the si button stops holding the green color我必须改变,以便 si 按钮停止保持绿色

Within your correct-block (the one setting the text green) you want to call incorrect.setBackground(null) in order to remove the background color from it.在您的正确块(将文本设置为绿色的块)中,您要调用incorrect.setBackground(null)以从中删除背景颜色。 In the other block, you should also remove the green color from the correcto -Button ( setForeground(null) ) to signify a Change to a false answer.在其他块,你也应该删除从绿色correcto -按钮( setForeground(null)来表示一个改变以一个虚假的答案。

Notice that Swing requires you to call repaint() on a changed UI element, to ensure it is redrawn, otherwise it will only be redrawn after a specific event causes the UI to redraw (for example a change in the JFrame size).请注意,Swing 要求您在更改的 UI 元素上调用repaint() ,以确保重绘它,否则只会在特定事件导致 UI 重绘(例如JFrame大小的更改)后重绘。

As a hint on the side, you should find more qualifying names for your variables.作为侧面的提示,您应该为您的变量找到更多符合条件的名称。 The ones you picked are very hard to read, since they are all effectively the same, but for some single characters or digits.您选择的那些很难阅读,因为它们实际上都是相同的,但是对于某些单个字符或数字。 For example your patterns could be named p_correctEdu and the other p_correctCom .例如,您的模式可以命名为p_correctEdu和另一个p_correctCom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM