简体   繁体   English

在IntelliJ IDEA中运行GUI应用程序

[英]Run GUI app in intellij idea

I'm new with intellij idea and jFormDesigner. 我是intellij创意和jFormDesigner的新手。 I want to test my application. 我想测试我的应用程序。 I added jFormDesigner file to my project, created form and added simple button and textarea. 我将jFormDesigner文件添加到我的项目中,创建了表单,并添加了简单的按钮和文本区域。 I added mouse click event for button, but I don't know how to test it. 我为按钮添加了鼠标单击事件,但是我不知道如何对其进行测试。

Here is event handler: 这是事件处理程序:

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

Here is generated by intellij idea code: 这是由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和jFromDesigner项目

When I click test form in jFormDesigner form works but events no. 当我单击jFormDesigner表单中的测试表单时,可以使用,但事件不存在。 How to test events? 如何测试事件?

for JButton is there better to use 对于JButton有更好的使用

1) Swing Action very scalable workaround for whole Swing JComponents 1)对于整个Swing JComponent, Swing Action的可扩展性解决方法

2) (most common) ActionListener 2)(最常见的) ActionListener

because 因为

3) I think that MouseListener is wrong Swing Listener for JButton 3)我认为MouseListenerJButton的 Swing侦听器错误

Why don't you test it with mocks? 为什么不使用模拟测试呢? There are a lot of mocking frameworks out there. 有很多模拟框架。 You could use Mockito, JMock, JMockit and so on... Mockito would be a good start. 您可以使用Mockito,JMock,JMockit等... Mockito将是一个不错的开始。 If you want to actually simulate the GUI actions... That's a whole other area. 如果您想实际模拟GUI动作,那是整个其他领域。 I suggest you mock it first, test it that way. 我建议您先对其进行模拟,然后再进行测试。

Also, the form is not genereated by Intellij Idea, but JFormDesigner - which you could use to abstract the GUI, not tying it to a specific IDE - which is a great idea. 同样,该表单不是由Intellij Idea生成的,而是JFormDesigner(可用于抽象GUI,而不是将其绑定到特定的IDE),这是一个好主意。

Also, if you are going to use a lot of Swing components, I suggest you go with the MVC pattern - http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller - it should simplify testing, maintenence and generaly simplify your form. 另外,如果您要使用很多Swing组件, 建议您使用MVC模式 -http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller-它应该简化测试,维护和一般简化您的表格。

Have you ever marked de checkbox "Custom Create"? 您是否曾勾选“自定义创建”复选框? When you add custom code for the component, in this case the JButton, you have to mark this option in its Properties Inspector. 在为组件(在本例中为JButton)添加自定义代码时,必须在其属性检查器中标记此选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM