Всплывающая подсказка

Независимо от вашей текущей проблемы, вы не должны использовать KeyListener в JTextField. Вместо этого используйте DocumentListener или DocumentFilter. Учитывая ваш код, я предполагаю, что DocumentFilter - это то, что вам нужно, поскольку вы хотите изменить текст JTextField по мере его ввода и перед его отображением.

например,

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

public class SwapAForAleph {
   // No idea of the correct unicode for this!!!
   public static final char SINDHI_ALIF = '\u0623'; 

   public static void main(String[] args) {
      final JTextField textField = new JTextField(10);
      textField.setFont(textField.getFont().deriveFont(32f));
      PlainDocument doc = (PlainDocument) textField.getDocument();
      doc.setDocumentFilter(new DocumentFilter() {
         @Override
         public void insertString(FilterBypass fb, int offset, String text,
               AttributeSet attr) throws BadLocationException {
            text = filterText(text);
            super.insertString(fb, offset, text, attr);
         }

         @Override
         public void replace(FilterBypass fb, int offset, int length,
               String text, AttributeSet attrs) throws BadLocationException {
            text = filterText(text);
            super.replace(fb, offset, length, text, attrs);
         }


         private String filterText(String text) {
            return text.replace('a', SINDHI_ALIF);
         }
      });

      JPanel panel = new JPanel();
      panel.add(textField);
      JOptionPane.showMessageDialog(null, panel);
   }
}

Или посмотрел по-другому ...

import java.awt.ComponentOrientation;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

public class NonEnglishTextField {
   public static final char ALEPH = '\u05D0';

   public static void main(String[] args) {
      final JTextField textField = new JTextField(20);
      textField.setFont(textField.getFont().deriveFont(32f));
      textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      textField.setHorizontalAlignment(SwingConstants.RIGHT);
      PlainDocument doc = (PlainDocument) textField.getDocument();
      doc.setDocumentFilter(new DocumentFilter() {
         @Override
         public void insertString(FilterBypass fb, int offset, String text,
               AttributeSet attr) throws BadLocationException {
            text = filterText(text);
            super.insertString(fb, offset, text, attr);
         }

         @Override
         public void replace(FilterBypass fb, int offset, int length,
               String text, AttributeSet attrs) throws BadLocationException {
            text = filterText(text);
            super.replace(fb, offset, length, text, attrs);
         }


         private String filterText(String text) {
            StringBuilder sb = new StringBuilder();
            for (char c : text.toLowerCase().toCharArray()) {
               if (c >= 'a' && c <= 'z') {
                  char newChar = (char) (c - 'a' + ALEPH);
                  sb.append(newChar);
               } else {
                  sb.append(c);
               }
            }
            return sb.toString();
         }
      });

      JPanel panel = new JPanel();
      panel.add(textField);
      JOptionPane.showMessageDialog(null, panel);
   }
}
1
задан Amine KOUIS 5 March 2019 в 18:45
поделиться

3 ответа

Что касается «span», вы сказали, что он абсолютный, но не дал никакой информации о позиционировании. В этом случае добавьте «bottom»:

   position: absolute;
   bottom: 0em;

Там также нет ничего, с чем это может быть связано, поэтому добавьте:

   .tooltipWrapper { position: relative; }
0
ответ дан Ray Butterworth 5 March 2019 в 18:45
поделиться

Я предлагаю вам проверить Tippy.js , это классная библиотека всплывающих подсказок и всплывающих окон, она очень настраиваема.

0
ответ дан Meir Roth 5 March 2019 в 18:45
поделиться

Парень из группы Facebook под названием HTML CSS JAVASCRIPT написал кодовую ручку, которая исправила это. Видимо я испортил абсолютное и относительное расположение. https://codepen.io/jkarkosza/pen/WmopbP

    .toolTipDiv {
    float: none;
    width: 275px;
    margin-left: 20px;
}
.toolTipLink a {
display: block;
color: #202020;
background-color: transparent;
width: auto;
padding: 0px 10px;
text-decoration: none;
font-size: 12px;
margin-left: 0px;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: #878787;
line-height: 17px;
}

.toolTipLink a:hover {
background-color: #ddd;
color: #9B0E11;
padding-left: 10px;
}

a.tooltip {
  position: relative;
}

a.tooltip span {
z-index: 10;
display: none;
padding: 7px 10px;
position:absolute; 
bottom: 100%;
color:#EEE;
background:#000;
/* margin-left: 200px; */
width: 140px;
line-height: 16px;
opacity: 0.85;
}

a.tooltip:hover span{
display:block;
}

<body>

<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>

<div class="toolTipDiv">

<span class="toolTipLink"> 

<a href="_lessons/_beginner/054-Basic-Boogie-Rock.html" target="_blank" class="tooltip">
<div class="tooltipWrapper">
<div class="toolTipEdge">Medium amount of text in this line.</div>
<span>I want the bottom of every tooltip to be at the top of every line it hovers over.</span>
</div>
</a> 

<a href="_lessons/_beginner/054-Basic-Boogie-Rock.html" target="_blank" class="tooltip">
<div class="tooltipWrapper">
<div class="toolTipEdge">This is the text that will be hovered over. Sometimes it may be this long</div>
<span>The bottom of this tooltip is too low. It should be just above the line.</span>
</div>
</a> 

<a href="_lessons/_beginner/054-Basic-Boogie-Rock.html" target="_blank" class="tooltip">
<div class="tooltipWrapper">
<div class="toolTipEdge">Here is a shorter line of text.</div>
<span>Sometimes the text in the "tooltip" will be a couple sentences long. If the tooltip text is ong or if it is short, the bottom of the tooltip should be right above the hovered line.</span>
</div>
</a> 

<a href="_lessons/_beginner/054-Basic-Boogie-Rock.html" target="_blank" class="tooltip">
<div class="tooltipWrapper">
<div class="toolTipEdge">Medium amount of text in this line.</div>
<span>This tooltip is way too high.</span>
</div>
</a> 

</span> 
</div>

</body>
0
ответ дан Daverino 5 March 2019 в 18:45
поделиться