我使用WindowBuilderPro编写了一个程序,它运行正常。但是,我现在想在一个新项目中重用这两个类,但是当我将第一个类复制到项目中时,我从Eclipse中得到了2个错误。这是SSCCE证明问题:
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}));
}
}
我得到的两个错误如下:
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}));
当设置Tab键顺序时会出现这些行。有谁知道如何克服这个问题?考虑到Java的重点是能够重用组件,这似乎是一个很大的错误。
答案 0 :(得分:0)
我需要将旧项目中使用的所有库添加到新项目的构建路径中。
答案 1 :(得分: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);
}
}