- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对FEST有疑问:
我在 JDialog
中有四个 JtextField
。如果我有四个 JTextField
,其中属性 name
、text
和 visibility
未定义或为 null,如何获取某个 JTextField
。
public class Form1 {
public static void main(String[] args) {
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JTextField tf4 = new JTextField();
tf1.setPreferredSize(tf1.getPreferredSize());
tf1.setText("");
tf2.setPreferredSize(tf2.getPreferredSize());
tf2.setText("");
tf3.setPreferredSize(tf3.getPreferredSize());
tf3.setText("");
tf4.setPreferredSize(tf4.getPreferredSize());
tf4.setText("");
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 1));
frame.add(tf1);
frame.add(tf2);
frame.add(tf3);
frame.add(tf4);
frame.setSize(300, 85);
frame.setVisible(true);
}
}
最佳答案
这里是一个使用反射为对象内包含的所有 JComponent 实例运行 setName 的方法。如果对象内部存在循环引用,则会出现堆栈溢出错误。
public static void assignComponentNames(Object obj) {
try {
Method getComponentsMethod = obj.getClass().getMethod("getComponents", new Class[]{});
if (null != getComponentsMethod){
try {
for (Component component : (Component[])(getComponentsMethod.invoke(obj))){
assignComponentNames(component);
}
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
else {
System.out.println(obj.toString());
}
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
for (Field field : obj.getClass().getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName(); //this is a different name, the reflection level name
Object fieldValue = null;
try {
fieldValue = field.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (null == fieldValue) {
continue;
}
if (fieldValue instanceof JComponent) {
String currentComponentNameForFieldValue = ((JComponent) fieldValue).getName();
if (null == currentComponentNameForFieldValue){
System.out.println("null component name");
((JComponent) fieldValue).setName(fieldName); //this sets the name specially for JComponent
}
}
else if(fieldValue instanceof Collection){
for (Object subObject : ((Collection)fieldValue).toArray()) {
assignComponentNames(subObject);
}
}
}
}
关于java - 如何使用FEST获取某个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9516750/
我正在尝试使用 FEST 运行一个简单的测试它失败了。这是我的 Swing 应用程序: public final class App extends JFrame { public App() {
我正在尝试使用 Fest 测试 Java Swing。我的问题是,关闭框架fixture(frameFixture.close())作为我的测试的@After TeaDown()方法的一部分以某种方式
我正在尝试对多个帧运行测试。执行时,它会在创建新机器人时挂起。 第一帧使用默认的Robot,第二个Robot的代码如下: Robot robot2 = BasicRobot.robotWithCurr
我开始使用FEST帮助我在 Java Swing GUI 上执行单元测试。 目前,我设法阅读了文档(大部分已弃用)并通过查看 Javadoc 和代码来帮助我。 现在我在使用 NoExitSecurit
这两种方法isSameAs()和isEqualTo()有什么区别? 该文档说: 验证实际值等于给定值。 和 验证实际值与给定值相同。 (http://fest.easytesting.org/asse
我的测试有时在 requireSelected() 调用上失败,但我不明白为什么。 public void testSimple() { JRadioButtonFixture fixture =
有没有办法将 FEST 测试附加到先前启动的应用程序,而不是从测试启动应用程序? 我问这个是因为 Squish例如可以做到这一点。我找不到与 FEST 类似的内容。 这个问题来自需要大量登录的应用程序
我有这个代码: //FrameFixture frame = (...got it from window, main frame...) JTableFixture table = fr
我是基于 FEST 的 GUI 测试的新手。 MyFrame 是我的应用程序的根类。 @Before public void onSetUp() { MyFrame
我正在使用 FEST在 JUnit 中编写断言。 断言列表包含一些元素很容易: assertThat(list).contains(2,4); 但是如何断言列表不包含某些东西呢?喜欢: assertT
在我的 FEST 测试中,我尝试断言 JButton 具有某个 ImageIcon。我在 org.fest.swing.fixture.JButtonFixture 上没有找到对应的方法 最佳答案 您
如何使用 FEST assertThat(...) 方法测试类对象是否相等? 示例: import static org.fest.assertions.api.Assertions.assertTh
@Before public void setUp() { Robot robot = BasicRobot.robotWithCurrentAwtHierarchy();
我正在测试我的 SWING Gui。我通过使用 FEST 框架来做到这一点。 我有以下输入: window.textBox("txtDatabaseConnectionString").enterTe
在使用 UISpec4J 获得不令人满意的结果后,我尝试使用 FEST 进行自动化测试。我对 NetBeans 有点陌生,必须在我正在处理的项目中使用 6.8。我已经使用 FEST 编写了一个测试用例
我已经编写了几个 JUnit 测试方法来测试我的 Java Swing GUI(使用 FEST)。我的类(class)采用以下格式: public class Tests { @BeforeC
我正在尝试使用 Groovy (2.1.6) 为 Fest 创建一个闭包匹配器,如下所示: def matcherLabel = [ isMatching: { JLabel label -> /*
我们有一个遵循 MVC 模式的应用程序,因此有一个很大的 GUI 部分需要测试。 我从阅读各种帖子中筛选出来的是,目前最好的两个选择是 UISpec4J 和 FEST。 每个选项的优缺点是什么?是否有
我一直在阅读和查看代码以了解 FEST 的工作原理。不幸的是,我在网上找不到那么多示例代码,除了: fest.codehaus.org/Getting+Started http://www.javaw
我正在尝试在 JFrame、JButton 的 Fest 教程之后进行简单的测试。 这是我的代码: private FrameFixture window; @Before p
我是一名优秀的程序员,十分优秀!