2014-02-14 21 views
0

我使用WindowBuilderPro編寫了一個程序,它工作正常。但是,我現在想要在新項目中重用這兩個類,但是當我將第一個項複製到項目中時,Eclipse中會出現兩個錯誤。這裏有一個SSCCE證明了問題:重複使用WindowBuilderPro創建的視覺類

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JTextField; 
import javax.swing.JLabel; 
import java.awt.Font; 
import org.eclipse.wb.swing.FocusTraversalOnArray; 
import java.awt.Component; 

public class ExampleFrame extends JFrame { 

    private JPanel contentPane; 
    private JTextField textField; 
    private JTextField textField_1; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ExampleFrame frame = new ExampleFrame(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public ExampleFrame() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     textField = new JTextField(); 
     textField.setBounds(106, 32, 74, 28); 
     contentPane.add(textField); 
     textField.setColumns(10); 

     JLabel lblNewLabel = new JLabel("Box 1"); 
     lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18)); 
     lblNewLabel.setBounds(31, 39, 46, 14); 
     contentPane.add(lblNewLabel); 

     JLabel lblBox = new JLabel("Box 2"); 
     lblBox.setFont(new Font("Tahoma", Font.PLAIN, 18)); 
     lblBox.setBounds(31, 101, 46, 14); 
     contentPane.add(lblBox); 

     textField_1 = new JTextField(); 
     textField_1.setColumns(10); 
     textField_1.setBounds(106, 94, 74, 28); 
     contentPane.add(textField_1); 
     contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblNewLabel, textField, lblBox, textField_1})); 
    } 
} 

的2個錯誤,我得到如下:

The import org.eclipse cannot be resolved 

上線

import org.eclipse.wb.swing.FocusTraversalOnArray; 

FocusTraversalOnArray cannot be resolved to a type 

在行

contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblNewLabel, textField, lblBox, textField_1})); 

當您設置標籤順序時,會出現這些行。有誰知道如何解決這個問題嗎?考慮到Java的全部重點是能夠重用組件,這似乎是一個很大的錯誤。

回答

0

我需要將舊項目中使用的所有庫添加到新項目的構建路徑中。

0

複製由Window Builder創建的類後,我遇到了同樣的問題。我發現我錯過了以下文件:

\src\org\eclipse\wb\swinger\FocusTraversalOnArray.java 

這似乎是Window Builder在外部添加的唯一文件。如果有人需要,我會在這裏發佈代碼。

/******************************************************************************* 
* Copyright (c) 2011 Google, Inc. 
* All rights reserved. This program and the accompanying materials 
* are made available under the terms of the Eclipse Public License v1.0 
* which accompanies this distribution, and is available at 
* http://www.eclipse.org/legal/epl-v10.html 
* 
* Contributors: 
* Google, Inc. - initial API and implementation 
*******************************************************************************/ 
package org.eclipse.wb.swing; 

import java.awt.Component; 
import java.awt.Container; 
import java.awt.FocusTraversalPolicy; 

/** 
* Cyclic focus traversal policy based on array of components. 
* <p> 
* This class may be freely distributed as part of any application or plugin. 
* 
* @author scheglov_ke 
*/ 
public class FocusTraversalOnArray extends FocusTraversalPolicy { 
    private final Component m_Components[]; 
    //////////////////////////////////////////////////////////////////////////// 
    // 
    // Constructor 
    // 
    //////////////////////////////////////////////////////////////////////////// 
    public FocusTraversalOnArray(Component components[]) { 
     m_Components = components; 
    } 
    //////////////////////////////////////////////////////////////////////////// 
    // 
    // Utilities 
    // 
    //////////////////////////////////////////////////////////////////////////// 
    private int indexCycle(int index, int delta) { 
     int size = m_Components.length; 
     int next = (index + delta + size) % size; 
     return next; 
    } 
    private Component cycle(Component currentComponent, int delta) { 
     int index = -1; 
     loop : for (int i = 0; i < m_Components.length; i++) { 
      Component component = m_Components[i]; 
      for (Component c = currentComponent; c != null; c = c.getParent()) { 
       if (component == c) { 
        index = i; 
        break loop; 
       } 
      } 
     } 
     // try to find enabled component in "delta" direction 
     int initialIndex = index; 
     while (true) { 
      int newIndex = indexCycle(index, delta); 
      if (newIndex == initialIndex) { 
       break; 
      } 
      index = newIndex; 
      // 
      Component component = m_Components[newIndex]; 
      if (component.isEnabled() && component.isVisible() && component.isFocusable()) { 
       return component; 
      } 
     } 
     // not found 
     return currentComponent; 
    } 
    //////////////////////////////////////////////////////////////////////////// 
    // 
    // FocusTraversalPolicy 
    // 
    //////////////////////////////////////////////////////////////////////////// 
    public Component getComponentAfter(Container container, Component component) { 
     return cycle(component, 1); 
    } 
    public Component getComponentBefore(Container container, Component component) { 
     return cycle(component, -1); 
    } 
    public Component getFirstComponent(Container container) { 
     return m_Components[0]; 
    } 
    public Component getLastComponent(Container container) { 
     return m_Components[m_Components.length - 1]; 
    } 
    public Component getDefaultComponent(Container container) { 
     return getFirstComponent(container); 
    } 
} 
+0

感謝您的回答。在我的情況下,沒有線路丟失。這個問題,我把我的答案(我忘了接受它,直到你的答案出現在我的郵箱中)是我需要將所有庫添加到構建路徑。 –