在intellij想法中运行GUI应用程序

时间:2012-05-02 21:23:10

标签: java swing intellij-idea mouse-listeners jform-designer

我是intellij idea和jFormDesigner的新手。 我想测试我的应用程序。 我将jFormDesigner文件添加到我的项目中,创建了表单并添加了简单的按钮和textarea。我为按钮添加了鼠标点击事件,但我不知道如何测试它。

这是事件处理程序:

private void startButtonMouseClicked(MouseEvent e) {
    resultTextField.setText("hello!");
}

这是由intellij想法代码生成的:

public class SysJournalForm extends JFrame {
    public SysJournalForm() {
        initComponents();
    }

    public static void main(String [] args)
    {
    }

    private void startButtonMouseClicked(MouseEvent e) {
        resultTextField.setText("hello!");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Vadim Mirgorod
        scrollPane1 = new JScrollPane();
        resultTextField = new JTextPane();
        startButton = new JButton();
        stopButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {

            //---- resultTextField ----
            resultTextField.setText("test");
            scrollPane1.setViewportView(resultTextField);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(5, 5, 530, 295);

        //---- startButton ----
        startButton.setText("\u0441\u0442\u0430\u0440\u0442");
        startButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                startButtonMouseClicked(e);
            }
        });
        contentPane.add(startButton);
        startButton.setBounds(5, 305, 130, startButton.getPreferredSize().height);

        //---- stopButton ----
        stopButton.setText("\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c");
        contentPane.add(stopButton);
        stopButton.setBounds(140, 305, 130, stopButton.getPreferredSize().height);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - Vadim Mirgorod
    private JScrollPane scrollPane1;
    private JTextPane resultTextField;
    private JButton startButton;
    private JButton stopButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

intellij idea and jFromDesigner project

当我在jFormDesigner表单中单击测试表单时,但事件没有。如何测试事件?

3 个答案:

答案 0 :(得分:1)

JButton

最好使用

1)Swing Action整个Swing JComponents

的非常可扩展的解决方法

2)(最常见)ActionListener

,因为

3)我认为MouseListenerSwing Listener错误JButton

答案 1 :(得分:0)

为什么不用模拟测试呢?那里有很多模拟框架。你可以使用Mockito,JMock,JMockit等...... Mockito将是一个好的开始。如果你想实际模拟GUI动作......这是另一个领域。 我建议你先嘲笑它,然后按照这种方式进行测试。

此外,表格不是由Intellij Idea生成的,而是JFormDesigner - 您可以使用它来抽象GUI,而不是将其绑定到特定的IDE - 这是一个好主意。

另外,如果你打算使用很多Swing组件,我建议你选择MVC模式 - http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller - 它应该简化测试,维护和简化你的表单

答案 2 :(得分:0)

你有没有标记de复选框“自定义创建”?为组件添加自定义代码(在本例中为JButton)时,必须在其属性检查器中标记此选项。