Explorer.java 主要含两个Panel,布局的South:LogPanel,在weka.gui下定义;布局的Center:TabbedPanel,含六个子Panel。我们只看第一个:PreprecessPanel。由文件.java实现。
PreprocessPanel布局负责,好像有7层容器,由于其中的5个在weka.gui下定义,为了保持本代码的独立性,将这5个Panel用普通的JPanel代替,所以界面可能看上去不是很爽。
再者,本界面的按钮等功能都是通过添加 侦听器 实现的,联系复杂,为了保证代码独立性,本着学习设计界面的原则,在代码中被省略了。
为了使大家能够更好的了解Panel的层次结构,运行后的界面上,每个Panel都给出了ToolTipText,方便大家观察。
运行效果:
500)this.width=500'>
PreprocessPanel.java代码如下,可直接编译运行:
/* * PreprocessPanel.java * Copyright (C) 2003 Richard Kirkby, Len Trigg * */
//package weka.gui.explorer;
import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseAdapter;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.io.*;import java.net.URL;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JComboBox;import javax.swing.ListSelectionModel;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.filechooser.FileFilter;
//import weka.core.Instances;//import weka.core.Attribute;//import weka.core.Instance;//import weka.core.SerializedObject;//import weka.core.converters.Loader;//import weka.core.converters.CSVLoader;//import weka.core.converters.C45Loader;////import weka.experiment.InstanceQuery;//import weka.filters.Filter;//import weka.filters.SupervisedFilter;//import weka.filters.unsupervised.attribute.Remove;////import weka.gui.AttributeSelectionPanel;//import weka.gui.AttributeSummaryPanel;//import weka.gui.PropertyPanel;//import weka.gui.AttributeVisualizationPanel;//import weka.gui.InstancesSummaryPanel;////import weka.gui.ExtensionFileFilter;//import weka.gui.FileEditor;//import weka.gui.GenericArrayEditor;//import weka.gui.GenericObjectEditor;//import weka.gui.Logger;//import weka.gui.PropertyDialog;//import weka.gui.SysErrLog;//import weka.gui.TaskLogger;//import weka.gui.ViewerDialog;////import weka.core.UnassignedClassException;
/** * This panel controls simple preprocessing of instances. Summary * information on instances and attributes is shown. Filters may be * configured to alter the set of instances. Altered instances may * also be saved. * * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz) * @author Len Trigg (trigg@cs.waikato.ac.nz) * @version $Revision: 1.50.2.6 $ */public class PreprocessPanel extends JPanel { /** Displays simple stats on the working instances */ // protected InstancesSummaryPanel m_InstSummaryPanel = new InstancesSummaryPanel();protected JPanel m_InstSummaryPanel = new JPanel(); /** Click to load base instances from a file */ protected JButton m_OpenFileBut = new JButton("Open file...");
/** Click to load base instances from a URL */ protected JButton m_OpenURLBut = new JButton("Open URL...");
/** Click to load base instances from a Database */ protected JButton m_OpenDBBut = new JButton("Open DB...");
/** Lets the user enter a DB query */ // protected GenericObjectEditor m_DatabaseQueryEditor = new GenericObjectEditor();
/** Click to revert back to the last saved point */ protected JButton m_UndoBut = new JButton("Undo");
/** Click to open the current instances in a viewer */ protected JButton m_EditBut = new JButton("Edit...");
/** Click to apply filters and save the results */ protected JButton m_SaveBut = new JButton("Save..."); /** Panel to let the user toggle attributes */ // protected AttributeSelectionPanel m_AttPanel = new AttributeSelectionPanel(); protected JPanel m_AttPanel = new JPanel();
/** Button for removing attributes */ JButton m_RemoveButton = new JButton("Remove");
/** Displays summary stats on the selected attribute */ // protected AttributeSummaryPanel m_AttSummaryPanel = new AttributeSummaryPanel();protected JPanel m_AttSummaryPanel = new JPanel(); /** Lets the user configure the filter */// protected GenericObjectEditor m_FilterEditor = new GenericObjectEditor();
/** Filter configuration */// protected PropertyPanel m_FilterPanel = new PropertyPanel(m_FilterEditor);protected JPanel m_FilterPanel = new JPanel(); /** Click to apply filters and save the results */ protected JButton m_ApplyFilterBut = new JButton("Apply");
/** The file chooser for selecting arff files */ protected JFileChooser m_FileChooser = new JFileChooser(new File(System.getProperty("user.dir")));
// /** File filters for various file types */// protected ExtensionFileFilter m_bsiFileFilter = // new ExtensionFileFilter(Instances.SERIALIZED_OBJ_FILE_EXTENSION,// "Binary serialized instances");//// protected ExtensionFileFilter m_c45FileFilter = // new ExtensionFileFilter(C45Loader.FILE_EXTENSION,// "C45 names files");//// protected ExtensionFileFilter m_csvFileFilter = // new ExtensionFileFilter(CSVLoader.FILE_EXTENSION,// "CSV data files");//// protected ExtensionFileFilter m_arffFileFilter = // new ExtensionFileFilter(Instances.FILE_EXTENSION,// "Arff data files");
/** Stores the last URL that instances were loaded from */ protected String m_LastURL = "http://"; /** Stores the last sql query executed */ protected String m_SQLQ = new String("SELECT * FROM ?"); /** The working instances */ // protected Instances m_Instances;
/** The visualization of the attribute values */// protected AttributeVisualizationPanel m_AttVisualizePanel = new AttributeVisualizationPanel();protected JPanel m_AttVisualizePanel = new JPanel();
/** Keeps track of undo points */ protected File[] m_tempUndoFiles = new File[20]; // set number of undo ops here
/** The next available slot for an undo point */ protected int m_tempUndoIndex = 0; /** * Manages sending notifications to people when we change the set of * working instances. */ // protected PropertyChangeSupport m_Support = new PropertyChangeSupport(this);
/** A thread for loading/saving instances from a file or URL */ protected Thread m_IOThread;
/** The message logger */// protected Logger m_Log = new SysErrLog(); // static {// GenericObjectEditor.registerEditors();// } /** * Creates the instances panel with no initial instances. */ public PreprocessPanel() {
// Create/Configure/Connect components//// try {//// m_DatabaseQueryEditor.setClassType(weka.experiment.InstanceQuery.class);//// m_DatabaseQueryEditor.setValue(new weka.experiment.InstanceQuery());//// ((GenericObjectEditor.GOEPanel)m_DatabaseQueryEditor.getCustomEditor())//// .addOkListener(new ActionListener() {//// public void actionPerformed(ActionEvent e) {//// setInstancesFromDBQ();//// }//// });//// } catch (Exception ex) {//// }//// m_InstSummaryPanel.setToolTipText("m_InstSummaryPanel,defined in weka.gui--1/5"); m_AttPanel.setToolTipText("m_AttPanel,defined in weka.gui--2/5"); m_AttSummaryPanel.setToolTipText("m_AttSummaryPanel,defined in weka.gui--2/5"); m_AttVisualizePanel.setToolTipText("m_AttVisualizePanel,defined in weka.gui--2/5"); m_FilterPanel.setToolTipText("m_FilterPanel,defined in weka.gui--2/5");
//// m_FilterEditor.setClassType(weka.filters.Filter.class); m_OpenFileBut.setToolTipText("Open a set of instances from a file"); m_OpenURLBut.setToolTipText("Open a set of instances from a URL"); m_OpenDBBut.setToolTipText("Open a set of instances from a database"); m_UndoBut.setToolTipText("Undo the last change to the dataset"); m_EditBut.setToolTipText("Open the current dataset in a Viewer for editing"); m_SaveBut.setToolTipText("Save the working relation to a file"); m_ApplyFilterBut.setToolTipText("Apply the current filter to the data");
// m_FileChooser.// addChoosableFileFilter(m_bsiFileFilter);// m_FileChooser.// addChoosableFileFilter(m_c45FileFilter);// m_FileChooser.// addChoosableFileFilter(m_csvFileFilter);// m_FileChooser.// addChoosableFileFilter(m_arffFileFilter);
m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); m_OpenURLBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// setInstancesFromURLQ(); } }); m_OpenDBBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// PropertyDialog pd = new PropertyDialog(m_DatabaseQueryEditor,100,100); } }); m_OpenFileBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// setInstancesFromFileQ(); } }); m_UndoBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// undo(); } }); m_EditBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // edit(); } }); m_SaveBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// saveWorkingInstancesToFileQ(); } }); m_ApplyFilterBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// applyFilter((Filter) m_FilterEditor.getValue()); } }); //// m_AttPanel.getSelectionModel()//// .addListSelectionListener(new ListSelectionListener() {//// public void valueChanged(ListSelectionEvent e) {//// if (!e.getValueIsAdjusting()) { //// ListSelectionModel lm = (ListSelectionModel) e.getSource();//// for (int i = e.getFirstIndex(); i <= e.getLastIndex(); i++) {//// if (lm.isSelectedIndex(i)) {//// m_AttSummaryPanel.setAttribute(i);//// m_AttVisualizePanel.setAttribute(i);//// break;//// }//// }//// }//// }//// });
m_InstSummaryPanel.setBorder(BorderFactory .createTitledBorder("Current relation")); JPanel attStuffHolderPanel = new JPanel(); attStuffHolderPanel.setBorder(BorderFactory .createTitledBorder("Attributes")); attStuffHolderPanel.setLayout(new BorderLayout()); attStuffHolderPanel.add(m_AttPanel, BorderLayout.CENTER); m_RemoveButton.setEnabled(false); m_RemoveButton.setToolTipText("Remove selected attributes."); m_RemoveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Pop up an error optionpane// JOptionPane.showMessageDialog(PreprocessPanel.this,// "Problem filtering instances:Remove Attributes",// JOptionPane.ERROR_MESSAGE); } });
JPanel p1 = new JPanel(); p1.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); p1.setLayout(new BorderLayout()); p1.add(m_RemoveButton, BorderLayout.CENTER); attStuffHolderPanel.add(p1, BorderLayout.SOUTH); m_AttSummaryPanel.setBorder(BorderFactory .createTitledBorder("Selected attribute")); m_UndoBut.setEnabled(false); m_EditBut.setEnabled(false); m_SaveBut.setEnabled(false); m_ApplyFilterBut.setEnabled(false); // Set up the GUI layout JPanel buttons = new JPanel(); buttons.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); buttons.setLayout(new GridLayout(1, 6, 5, 5)); buttons.add(m_OpenFileBut); buttons.add(m_OpenURLBut); buttons.add(m_OpenDBBut); buttons.add(m_UndoBut); buttons.add(m_EditBut); buttons.add(m_SaveBut);
JPanel attInfo = new JPanel();
attInfo.setLayout(new BorderLayout()); attInfo.add(attStuffHolderPanel, BorderLayout.CENTER);
JPanel filter = new JPanel(); filter.setBorder(BorderFactory .createTitledBorder("Filter")); filter.setLayout(new BorderLayout()); filter.add(m_FilterPanel, BorderLayout.CENTER); filter.add(m_ApplyFilterBut, BorderLayout.EAST);
JPanel attVis = new JPanel(); attVis.setLayout( new GridLayout(2,1) ); attVis.add(m_AttSummaryPanel);
// JComboBox colorBox = m_AttVisualizePanel.getColorBox(); JComboBox colorBox = new JComboBox(); colorBox.setToolTipText("The chosen attribute will also be used as the " + "class attribute when a filter is applied."); final JButton visAllBut = new JButton("Visualize All"); visAllBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) {// if (m_Instances != null) {// try {// final weka.gui.beans.AttributeSummarizer as = // new weka.gui.beans.AttributeSummarizer();// as.setColoringIndex(m_AttVisualizePanel.getColoringIndex());// as.setInstances(m_Instances);// // final javax.swing.JFrame jf = new javax.swing.JFrame();// jf.getContentPane().setLayout(new java.awt.BorderLayout());// // jf.getContentPane().add(as, java.awt.BorderLayout.CENTER);// jf.addWindowListener(new java.awt.event.WindowAdapter() {// public void windowClosing(java.awt.event.WindowEvent e) {// visAllBut.setEnabled(true);// jf.dispose();// }// });// jf.setSize(830,600);// jf.setVisible(true);// } catch (Exception ex) {// ex.printStackTrace();// }// } } }); JPanel histoHolder = new JPanel(); histoHolder.setLayout(new BorderLayout()); histoHolder.add(m_AttVisualizePanel, BorderLayout.CENTER); JPanel histoControls = new JPanel(); histoControls.setLayout(new BorderLayout()); histoControls.add(colorBox, BorderLayout.CENTER); histoControls.add(visAllBut, BorderLayout.EAST); histoHolder.add(histoControls, BorderLayout.NORTH); attVis.add(histoHolder); histoHolder.setToolTipText("name:histoHolder,level:6,father:attVis");
JPanel lhs = new JPanel(); lhs.setLayout(new BorderLayout()); lhs.add(m_InstSummaryPanel, BorderLayout.NORTH); lhs.add(attInfo, BorderLayout.CENTER); m_InstSummaryPanel.setToolTipText("name:m_InstSummaryPanel,level:5,father:lhs,describe:weka.gui.InstancesSummaryPanel"); attInfo.setToolTipText("name:attInfo,level:5,father:lhs,children:2,describe:the Center of rhs-Panel");
JPanel rhs = new JPanel(); rhs.setLayout(new BorderLayout()); rhs.add(attVis, BorderLayout.CENTER); attVis.setToolTipText("name:attVis,level:5,father:rhs,children:2,describe:the Center of rhs-Panel");
JPanel relation = new JPanel(); relation.setLayout(new GridLayout(1, 2)); relation.add(lhs); relation.add(rhs); lhs.setToolTipText("name:lhs,level:4,father:relation,children:2,describe:the Grid1 of relation-Panel"); rhs.setToolTipText("name:rhs,level:4,father:relation,children:1,describe:the Grid2 of relation-Panel");
JPanel middle = new JPanel(); middle.setLayout(new BorderLayout()); middle.add(filter, BorderLayout.NORTH); middle.add(relation, BorderLayout.CENTER); filter.setToolTipText("name:filter,level:3,father:middle,children:2,describe:the North of middle-Panel"); relation.setToolTipText("name:relation,level:3,father:middle,children:2,describe:the Center of middle-Panel");
setLayout(new BorderLayout()); add(buttons, BorderLayout.NORTH); add(middle, BorderLayout.CENTER); buttons.setToolTipText("name:buttons,level:2,father:sp,children:6,describe:the NORTH of sp-Panel"); middle.setToolTipText("name:middle,level:2,father:sp,children:2,describe:the center of sp-Panel"); }
/** * Sets the Logger to receive informational messages * * @param newLog the Logger that will now get info messages */// public void setLog(Logger newLog) {//// m_Log = newLog;// }// /** * Tells the panel to use a new base set of instances. * * @param inst a set of Instances */
/** * Tests out the instance-preprocessing panel from the command line. * * @param args ignored */ public static void main(String [] args) {
try { final JFrame jf = new JFrame("Weka Explorer: Preprocess"); jf.getContentPane().setLayout(new BorderLayout()); final PreprocessPanel sp = new PreprocessPanel(); sp.setToolTipText("name:sp,type:PreprocessPanel,level:1,father:if,children:2,describe:the center of if-Panel"); jf.getContentPane().add(sp, BorderLayout.CENTER); // weka.gui.LogPanel lp = new weka.gui.LogPanel(); JPanel lp = new JPanel(); lp.setToolTipText("name:lp,type:weka.gui.LogPanel(),level:1,father:if,children:2,describe:the center of if-Panel"); // sp.setLog(lp); jf.getContentPane().add(lp, BorderLayout.SOUTH); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.dispose(); System.exit(0); } }); jf.pack(); jf.setSize(800, 600); jf.setVisible(true); } catch (Exception ex) { ex.printStackTrace(); System.err.println(ex.getMessage()); } }}