2017-05-03 14 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

カスタマイズする実際のビューComponentscroll.getViewport().getView()、ない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(); 
    } 
} 
関連する問題