借用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 ); }
我想在你的介面上肯定有很多Panel.
不过反正是拖出来的,应该改的几率也不大吧,类大点就大点吧!
在最后的总显示类中把这些模块合并给个例子:
一个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 );
}