2017-05-03 133 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(); 
    } 
}