【问题标题】:Run GUI app in intellij idea在 Intellij IDEA 中运行 GUI 应用程序
【发布时间】:2012-05-02 21:23:10
【问题描述】:

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

这是事件处理程序:

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

这里是intellijidea生成的代码:

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
}

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

【问题讨论】:

  • 我以前用过Netbeans,没问题。现在我需要使用intellij idea。
  • 如需尽快获得更好的帮助,请发帖SSCCE

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


【解决方案1】:

对于JButton有没有更好用的

1) Swing Action 整个Swing JComponents 的可扩展解决方法

2)(最常见)ActionListener

因为

3) 我认为 MouseListener 是错误的 Swing Listener 对于 JButton

【讨论】:

  • 感谢回答,但我需要在 intellij idea 中做什么?
  • 同样的方式,就像你添加了 MouseListener
  • 现在看,我需要选择什么?
【解决方案2】:

为什么不用模拟来测试它?有很多模拟框架。你可以使用 Mockito、JMock、JMockit 等等...... Mockito 将是一个好的开始。如果您想实际模拟 GUI 操作……那是另一个领域。 我建议你先模拟它,这样测试它。

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

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

【讨论】:

  • 谢谢,但是为什么在 Intellij Idea 中我不能像在 NetBeans 中那样运行我的 GUI 应用程序(带有事件和其他东西)?
  • 你可以。你有一个 JFormDesigner 插件。该插件在formdev.com/jformdesigner/doc/ides/intellij-idea 上可用。
  • 该插件应该可以简化您的开发,但您可以实际运行表单 - 它是生成的 Swing 代码。根据您的图片,您可以单击“MouseListener”。看看这个 - docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/…。我认为让你的按钮工作更简单。只需覆盖“mousePressed”,您就可以开始了!
【解决方案3】:

您是否曾经将复选框标记为“自定义创建”?当您为组件(在本例中为 JButton)添加自定义代码时,您必须在其 Properties Inspector 中标记此选项。

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2023-03-24
    • 2016-07-12
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多