2017-09-28 9 views
0

Les questions disent tout vraiment, je veux que l'arrière-plan d'un JTextField soit semi-transparent et j'utilise une minuterie pour le faire clignoter.Rendre JTextField semi-transparent

J'ai donc trouvé que l'utilisation du textField.setBackground() traditionnel crée un étrange problème graphique où à chaque flash le champ de texte est plus sombre qu'il ne devrait l'être. (Voir ci-dessous)

strange graphical glitch

Ainsi, après la recherche en ligne j'ai essayé de passer outre la méthode de peinture du JTextField avec le code suivant:

name = new JTextField(15) { 
     @Override 
     protected void paintComponent(Graphics g) { 
      g.setColor(this.getBackground()); 
      g.fillRect(getX(), getY(), getWidth(), getHeight()); 
      super.paintComponent(g); 
     } 
    }; 

En outre, certains ont recommandé que je mets le champ de texte opaque booléen à faux. Ce que je fis et en vain, maintenant il n'y a même pas de clignotement rouge et je reçois simplement ceci:

fields with field.setOpaque(false);

Juste au cas où il aide, voici le code que je utilise pour faire les champs clignotent.

public void flashField(JTextField field, Color flashColor, final int flashDelay, final int numberOfFlashes) { 
    timers.add(new Timer(flashDelay, new ActionListener() { 

     int counter = 0; 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      counter++; 
      if (counter % 2 == 0) 
       field.setBackground(
         new Color(flashColor.getRed(), flashColor.getBlue(), flashColor.getGreen(), 50)); 
      else 
       field.setBackground(Color.WHITE); 

      if (counter == (numberOfFlashes * 2) + 1) { 
       ((Timer) e.getSource()).stop(); 
      } 

      field.repaint(); 
     } 
    })); 

    timers.get(timers.size() - 1).start(); 

} 

Répondre

0

Voici un code adapté où Textfield continue de clignoter toutes les 200ms et est transparent.

import java.awt.AlphaComposite; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TransparentTextFieldExample { 
public static void main(String[] args) { 
    new TransparentTextFieldExample(); 
} 

public TransparentTextFieldExample() { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
      } 

      JFrame frame = new JFrame("Testing"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setLayout(new BorderLayout()); 
      JPanel panel = new JPanel(); 
      panel.setLayout(new GridBagLayout()); 
      TransparentTextField ttf = new TransparentTextField("Some text!", 20); 
      panel.add(ttf); 
      frame.add(panel); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
      flashField(ttf, Color.RED, 1, 20); 
     } 
    }); 

} 

public void flashField(JTextField field, java.awt.Color flashColor, final int flashDelay, final int numberOfFlashes) { 

    Timer tm = new Timer(flashDelay, new ActionListener() { 

     int counter = 0; 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (counter % 2 == 0) { 
       field.setBackground(flashColor); 
      } else { 
       field.setBackground(Color.WHITE); 
      } 
      if (counter == (numberOfFlashes * 2) + 1) { 
       System.out.println("Inside"); 
       //((Timer) e.getSource()).stop(); 
       // break; 
      } 

      field.repaint(); 
      try { 
       Thread.sleep(200l); 
      } catch (Exception ex) { 
      } 
      counter++; 
     } 
    }); 
    tm.start(); 

} 

public class TransparentTextField extends JTextField { 

    public TransparentTextField(String text) { 
     super(text); 
     init(); 
    } 

    public TransparentTextField(int columns) { 
     super(columns); 
     init(); 
    } 

    public TransparentTextField(String text, int columns) { 
     super(text, columns); 
     init(); 
    } 

    protected void init() { 
     setOpaque(false); 
    } 

    @Override 
    public void paint(Graphics g) { 
     Graphics2D g2d = (Graphics2D) g.create(); 
     g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f)); 
     super.paint(g2d); 
     g2d.dispose(); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     Graphics2D g2d = (Graphics2D) g.create(); 
     g2d.setColor(getBackground()); 
     g2d.fillRect(0, 0, getWidth(), getHeight()); 
     super.paintComponent(g2d); 
     g2d.dispose(); 
    } 

} 
} 
0

Ainsi, après la recherche en ligne j'ai essayé de passer outre la méthode de peinture

En outre, certains ont recommandé que je mets le champ de texte booléen opaque à false.

Eh bien, vous devez faire les deux en même temps. Quelque chose comme:

JPanel panel = new JPanel() 
{ 
    protected void paintComponent(Graphics g) 
    { 
     g.setColor(getBackground()); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     super.paintComponent(g); 
    } 
}; 
panel.setOpaque(false); // background of parent will be painted first 
panel.setBackground(new Color(255, 0, 0, 20)); 
frame.add(panel); 

Découvrez Background With Transparency pourquoi cela est nécessaire et pour une solution simple réutilisable que vous pouvez utiliser sur tout composant sans faire de la peinture personnalisée à chaque fois.