2017-05-03 5 views
0

Ich möchte JScrollPane mit geänderten Farben des Textes, Hintergrund in der Textbox und eigene Schriftart machen, aber meine Implementierung funktioniert nicht - Ich habe Standardform von JScrollPane (weißer Hintergrund, Standard schwarze Schrift) gesehen. Könnte mir jemand sagen, warum es nicht funktioniert und wie man es repariert?Wie kann ich die Farben des JScrollPane-Inhalts ändern?

public class TextField extends JFrame 
 
{ 
 
\t public TextField() 
 
\t { 
 
\t \t JScrollPane scroll = new JScrollPane(new JTextArea(15, 45)); 
 
\t \t scroll.setPreferredSize(new Dimension(500, 300)); 
 
\t \t scroll.getViewport().setBackground(Color.BLUE); 
 
\t \t scroll.getViewport().setForeground(Color.YELLOW); 
 
\t \t 
 
\t \t Font font = new Font("Dialog", Font.BOLD + Font.ITALIC, 14); 
 
\t \t scroll.getViewport().setFont(font); 
 
\t \t add(scroll); 
 
\t \t pack(); 
 
\t } 
 
}

Antwort

2

Die tatsächliche Ansicht Component Sie anpassen möchten mit scroll.getViewport().getView() erhalten wird, nicht scroll.getViewport().

public class TextField extends JFrame 
{ 
    public TextField() 
    { 
     JScrollPane scroll = new JScrollPane(new JTextArea(15, 45)); 
     scroll.setPreferredSize(new Dimension(500, 300)); 
     scroll.getViewport().getView().setBackground(Color.BLUE); 
     scroll.getViewport().getView().setForeground(Color.YELLOW); 

     Font font = new Font("Dialog", Font.BOLD + Font.ITALIC, 14); 
     scroll.getViewport().getView().setFont(font); 
     add(scroll); 
     pack(); 
    } 
} 
Verwandte Themen