当前位置: 代码迷 >> J2SE >> private JScrollPane jsp;为什么要定义替私有的
  详细解决方案

private JScrollPane jsp;为什么要定义替私有的

热度:572   发布时间:2016-04-23 20:05:48.0
private JScrollPane jsp;为什么要定义为私有的
01: package com.d10.www;
02: import java.awt.Container;
03: import javax.swing.JFrame;
04: import javax.swing.JPanel;
05: import javax.swing.JScrollPane;
06: import javax.swing.JTextArea;
07: import javax.swing.ScrollPaneConstants;
08: public class Example10_7 extends JFrame{
09: private JScrollPane jsp;
010: public Example10_7(){
011: this.setTitle("JScrollPane实例");
012: this.setBounds(200, 200, 300, 200);
013: this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
014: Container c=this.getContentPane();
015: jsp=new JScrollPane();
016: jsp.setBounds(0, 0, 298, 198);
017: c.add(jsp);
018: JTextArea area=new JTextArea();
019: jsp.setViewportView(area);
020: area.setLineWrap(true);
021: area.append("北京\n中华人民共和国首都\n");
022: area.append("中央直辖市\n中国国家中心城市\n");
023: area.append("中国政治、文化、教育\n和国际交流中心\n");
024: area.append("同时是中国经济金融的\n决策中心和管理中心.");
025: jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
026: jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
027: }
028: public static void main(String[] args) {
029: new Example10_7().setVisible(true);
030: }
031: } 

------解决思路----------------------
多种方法可以完成一件事,其中比较优秀的方法是最佳实践。
成员定义为private就是最佳实践,避免不必要的暴露,增加系统耦合性。
  相关解决方案