с Windows L & F, чей фон выбора по умолчанию белый, выбранные текстовые фрагменты, которые выделены желтыми. I can pass any color to that utility method, not necessarily something stored in the background property.
The important consideration is the highlight color is not the same as the background (which stays the same — blue in my case).
Basically, it seems I need to dynamically determine the foreground color based on its actual background (and not the фон свойство). Даже если я переопределяю getforeground () , мне все еще нужно как -то получить доступ к этой информации. Что сложно, так как я не знаю, какая точная часть текстового качания собирается рисовать (независимо от того, выделено или нет).
Как я могу решить эту проблему? />
Код: Выделить всё
package demos.text;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
public class HighlighterDemo {
private static final DefaultHighlighter.DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
public static void main(String[] args) throws BadLocationException, UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException {
UIManager.setLookAndFeel(WindowsLookAndFeel.class.getName());
Container mainPanel = createMainPanel();
JFrame frame = new JFrame("Highlighter Demo");
frame.setContentPane(mainPanel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JPanel createMainPanel() throws BadLocationException {
JPanel panel = new JPanel(new BorderLayout());
panel.add(createTextArea());
return panel;
}
private static JTextArea createTextArea() throws BadLocationException {
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Arial", Font.PLAIN, 20)); // the default font in nonsensically small
String text = "Some sample text";
textArea.setText(text);
textArea.getHighlighter().addHighlight(text.indexOf("sample"), text.indexOf("text"), painter);
return textArea;
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... background