2017-05-03 132 views
0

我想使JScrollPane的文本,文本框中的背景和自己的字体改变颜色,但我的实现不工作 - 我已经看到JScrollPane的默认形式(白色背景,标准的黑色字体)。谁能告诉我为什么它不起作用以及如何解决它?如何更改JScrollPane内容的颜色?

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 } 
 
}

回答

2

你想定制与scroll.getViewport().getView(),不scroll.getViewport()所获得的实际视图Component

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(); 
    } 
}