现在用NetBeans做一个Java的GUI开发,组件很多,把组件拉好后,只有一个主类很大,有什么方法把它分开

解决方案 »

  1.   

    把在同一个Panel上的东西独立出来成一个类.这样可以分开.
    我想在你的介面上肯定有很多Panel.
      

  2.   

    但是我是用NetBeans开发的,它支持把组件拖到上面,但是生成的代码是死的,没办法修改
      

  3.   

    你在设计的时候,自己主动把它分到多个类里面处理:比如,有个表格显示数据的区域,还有几个按钮控制表格的增删改查,那么你就可以新创建个JPanel A,把这些都功能都绘制到A里面。当你要在主界面用到这个功能时,你就new 一个A 就可以了。
      

  4.   

    生成的代码那就算了,实在不行把active都拿出来,不用匿名内布类。
    不过反正是拖出来的,应该改的几率也不大吧,类大点就大点吧!
      

  5.   

    借用JavaBean的设计模式,按照功能模块区别设计不同的小显示模块
    在最后的总显示类中把这些模块合并给个例子:
    一个Bean/**
     * A Bean used to show annotation file comboBox
     * @author Marquis
     * @version 1.0
     */
    public class AnnotationBean extends JComponent {
        private JComboBox annotationCmb;
        private JLabel annotationLb;    /**
         * Constructor
         */
        public AnnotationBean() {
            super();
            init();
        }    //=====public methods=========    /**
         * Set the selected annotation file with the name
         *
         * @param fileName file name
         */
        public void setSelectedAnnotationFile(String fileName) {
            annotationCmb.setSelectedItem(fileName);
        }    /**
         * Set the selected annotation file with the index
         *
         * @param index the index of annotation files added in annotation comboBox
         */
        public void setSelectedAnnotationFile(int index) {
            annotationCmb.setSelectedIndex(index);
        }    /**
         * @return the name of annotation file selected
         */
        public String getSelectedAnnotationFileName() {
            return StringUtils.deNull((String) annotationCmb.getSelectedItem());
        }
        
        /**
         * This method gets the full path of the annotation file selected.
         * @return
         */
        public String getSelectedAnnotationFilePath()
        {
         return getSelectedAnnotationFile().getPath();
        }    /**
         * @return the annotation file selected
         */
        public File getSelectedAnnotationFile() {
            String fileName = (String) annotationCmb.getSelectedItem();
            if( StringUtils.isEmpty( fileName ) )
            {
             return null;
            }
            String filePath = User.getCurrentUser().getUserProfilerPath();
            
            return new File(filePath + File.separator + fileName);
        }    /**
         * get the annotation comboBox
         *
         * @return The Annotation ComboBox
         */
        public JComboBox getAnnotationComboBox() {
            return annotationCmb;
        }    /**
         * Set ItemListener to annotation comboBox
         *
         * @param itemListener
         */
        public void setAnnotationComboBoxListener(ItemListener itemListener) {
            annotationCmb.addItemListener(itemListener);
        }    //====intenal methods====    /**
         * initialize AnnotationComp
         */
        protected void init() {
            this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            this.add(createAnnotationLabel());
            this.add(createAnnotationComboBox());
            for (Component c : this.getComponents()) {
                ((JComponent) c).setAlignmentX(Component.LEFT_ALIGNMENT);
            }
        }    /**
         * @return create a annotation label
         */
        protected JLabel createAnnotationLabel() {
            annotationLb = new JLabel();
            annotationLb.setText(CytoMessage.getResourse("label.annotationComp.annotation"));
            return annotationLb;
        }    /**
         * @return JComboBox  create a ComboBox with annotation files in user profile path
         */
        protected JComboBox createAnnotationComboBox() {
            annotationCmb = new JComboBox();
            annotationCmb.setFocusable( true );
            File userProfileDirectory = new File(User.getCurrentUser().getLibPath());
            File[] annotationFiles = FileUtils.getFilesByType(userProfileDirectory, CytoConstant.ANNOTATION_FILE_EXTENSION);        if (annotationFiles.length == 0) {//there are no .db files found in user profilerPath
                //TODO Throw a fileNotFindException
                return annotationCmb;
            }        for (File f : annotationFiles) {
                annotationCmb.addItem(f.getName());
            }
            annotationCmb.setSelectedIndex(0);
            return annotationCmb;
        }}
    最后在主显示界面中把bean进行组装 /**
     * Create Main Panel
     */
    @Override
    protected void createMainPanel( JPanel mainPanel )
    {
    //other beans....
    annotationComp = new AnnotationBean();
    mainPanel.add( annotationComp );
            }