對不起,又有問題

本帖最後由 luckiejacky 於 2013-9-8 17:19 編輯

1) Why the mainPanel2 within the TreeListener only refreshes when I click on the
root node, when I inject a custPanel directly into that mainPanel, it works ok

2) How do I get the source like e.getSource() from a mouse listener/tree listener.
e.getSource() == tcr doesn't work
  1. package jewelleryMain;


  2. import GenericForm.GradientPanel;
  3. import GenericForm.TopPanel;
  4. import javax.swing.JFrame;

  5. /**
  6. *
  7. * @author Jacky
  8. */


  9.   
  10. public class MainFrame2 extends javax.swing.JFrame implements Runnable {

  11.     /**
  12.      * Creates new form mainFrame
  13.      */

  14.     @SuppressWarnings("empty-statement")
  15.     public MainFrame2() throws SQLException {
  16.         db = new Database("jdbc:mariadb://localhost:3307/pos_crm");
  17.         db.OpenConnection();
  18.         
  19.         initComponents();
  20.          
  21.     }
  22.     Thread t;

  23.   
  24.   

  25.   public void start()
  26.    {
  27.       t = new Thread(this);
  28.       t.start();
  29.    }

  30.   public void stop()
  31.     {
  32.       t = null;
  33.     }

  34.   public void doSomething()
  35.   {
  36.     setTitle((new java.util.Date()).toString());
  37.   }


  38.   public void run() {
  39.       while (t != null){
  40.           doSomething();
  41.          try {
  42.              Thread.sleep(1000);
  43.          }  catch(InterruptedException ex) {

  44.          }
  45.       }
  46.   }
  47.     /**
  48.      * @param args the command line arguments
  49.      */
  50.     public static void main(String args[]) {
  51.         /* Set the Nimbus look and feel */
  52.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  53.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  54.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  55.          */
  56.         try {
  57.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  58.                 if ("Nimbus".equals(info.getName())) {
  59.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  60.                     break;
  61.                 }
  62.             }
  63.         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
  64.             java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  65.         }
  66.         //</editor-fold>

  67.         /* Create and display the form */
  68.         java.awt.EventQueue.invokeLater(new Runnable() {
  69.             @Override
  70.             public void run() {
  71.                 try {
  72.                     new MainFrame2().setVisible(true);
  73.                 } catch (SQLException ex) {
  74.                     Logger.getLogger(MainFrame2.class.getName()).log(Level.SEVERE, null, ex);
  75.                 }
  76.             }
  77.         });
  78.     }
  79.    
  80.     private Database db;
  81.     private int custID;
  82.     TopPanel topPanel;
  83.     // End of variables declaration                  

  84.     private void initComponents() throws SQLException {
  85.          Container mainPanel = this.getContentPane();
  86.          topPanel = new TopPanel("/images/topbar_Start.png");
  87.          mainPanel.add(topPanel, BorderLayout.NORTH);

  88.          centerPanel = new GradientPanel(color1, color2);
  89.          GradientPanel TreePanel = new GradientPanel(color1, color2);
  90.          
  91.          
  92.          JTree tree = null;
  93.          crm = new DefaultMutableTreeNode("CRM");
  94.          tcr = new DefaultMutableTreeNode("TCR");
  95.          totalCustomer = new DefaultMutableTreeNode("Total Number  of Customers");
  96.          totalValueCustomer = new DefaultMutableTreeNode("Total Value  of Customers");
  97.          top25Customers = new DefaultMutableTreeNode("Total 25 Customers");
  98.          top25StatusChange = new DefaultMutableTreeNode("Top 25 status change");
  99.          addCustomer = new  DefaultMutableTreeNode("加入客户");
  100.          top25StatusChange = new DefaultMutableTreeNode("Top 25 status change");
  101.          viewCustomer = new DefaultMutableTreeNode("显示客户");
  102.          manageCustomer = new DefaultMutableTreeNode("管理客户");
  103.          report = new  DefaultMutableTreeNode("展示报告");
  104.          administor = new  DefaultMutableTreeNode("系统维护");
  105.          
  106.          
  107.          
  108.          
  109.          
  110.          
  111.          tcr.add(totalCustomer);
  112.          tcr.add(totalValueCustomer);
  113.          tcr.add(top25Customers);
  114.          tcr.add(top25StatusChange);

  115.          crm.add(tcr);
  116.          crm.add(addCustomer);
  117.          crm.add(viewCustomer);
  118.          crm.add(manageCustomer);
  119.          crm.add(report);
  120.          crm.add(administor);
  121.          
  122.          tree = new JTree(crm);


  123.         
  124.          
  125.          TreePanel.add(tree);
  126.          
  127.            final DefaultTreeCellRenderer renderer =
  128.             (DefaultTreeCellRenderer)(tree.getCellRenderer());
  129.             
  130.             renderer.setTextNonSelectionColor(Color.WHITE);
  131.             renderer.setTextSelectionColor(Color.YELLOW);
  132.             renderer.setOpaque(false);
  133.          
  134.             
  135.          tree.setOpaque(false);
  136.          tree.addTreeSelectionListener(new TreeSelectionListener() {
  137.             @Override
  138.             public void valueChanged(TreeSelectionEvent e) {
  139.                   ActionButtonsPanel btnPanel;
  140.                 try {
  141.                     btnPanel = new ActionButtonsPanel(db, 1);
  142.                     GenericCustomerPanel custPanel = new GenericCustomerPanel(db,-1);
  143.                     JSplitPane mainPanel2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, custPanel, btnPanel);
  144.                     mainPanel2.setDividerLocation(700);
  145.                     mainPanel2.setEnabled(false);
  146.                      
  147.                    centerPanel.add(mainPanel2);
  148.                                  
  149.                      //centerPanel.repaint();
  150.                     
  151.                   
  152.                 } catch (SQLException ex) {
  153.                     Logger.getLogger(MainFrame2.class.getName()).log(Level.SEVERE, null, ex);
  154.                 }
  155.                            
  156.                                 
  157.             }
  158.                      
  159.          } );
  160.          
  161.      
  162.          
  163.         
  164.       
  165.          
  166.          JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, TreePanel, centerPanel);
  167.          
  168.          splitPane.setDividerLocation(200);
  169.          splitPane.setDividerSize(2);
  170.          splitPane.setEnabled(false);
  171.          
  172.          
  173.          mainPanel.add(splitPane, BorderLayout.CENTER);
  174.          
  175.          
  176.          
  177.          
  178.          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  179.         setSize(1280, 1000);
  180.         setVisible(true);        
  181.          
  182.          
  183.          
  184.     }
  185.     Color color1 = new Color(0,0,0);
  186.     Color color2 = new Color(128,128,128);
  187.     DefaultMutableTreeNode crm;
  188.     DefaultMutableTreeNode tcr;
  189.     DefaultMutableTreeNode totalCustomer;
  190.     DefaultMutableTreeNode totalValueCustomer;
  191.     DefaultMutableTreeNode addCustomer;
  192.     DefaultMutableTreeNode top25Customers;
  193.     DefaultMutableTreeNode top25StatusChange;
  194.     DefaultMutableTreeNode viewCustomer;
  195.     DefaultMutableTreeNode manageCustomer;
  196.     DefaultMutableTreeNode report;
  197.     DefaultMutableTreeNode administor;
  198.    
  199.    
  200.    
  201.     //Container mainPanel;
  202.     //CreateCustomer createCust;
  203.     protected GradientPanel centerPanel;
  204.    // ActionButtonsPanel btnPanel;

  205. }
複製代碼