0

私は intellij のアイデアと jFormDesigner が初めてです。アプリケーションをテストしたい。jFormDesigner ファイルをプロジェクトに追加し、フォームを作成し、シンプルなボタンとテキストエリアを追加しました。ボタンのマウスクリックイベントを追加しましたが、テストする方法がわかりません。

イベントハンドラは次のとおりです。

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 のアイデアと jFromDesigner プロジェクト

jFormDesignerフォームでテストフォームをクリックすると機能しますが、イベントはありません。イベントをテストするには?

4

3 に答える 3

1

JButtonの場合、使用する方が良いです

1) Swing JComponents全体に対するSwing Actionの非常にスケーラブルな回避策

2) (最も一般的な) ActionListener

なぜなら

3) MouseListenerは間違っていると思いますJButtonSwing Listener

于 2012-05-02T21:32:08.053 に答える
0

モックでテストしてみませんか?世の中にはたくさんのモッキング フレームワークがあります。Mockito、JMock、JMockit などを使用できます... Mockito から始めるのがよいでしょう。実際に GUI アクションをシミュレートしたい場合... それはまったく別の領域です。最初にそれをモックし、そのようにテストすることをお勧めします。

また、フォームは Intellij Idea によって生成されるのではなく、JFormDesigner (特定の IDE に結び付けるのではなく、GUI を抽象化するために使用できます) によって生成されます。これは素晴らしいアイデアです。

また、多くの Swing コンポーネントを使用する場合は、MVC パターンを使用することをお勧めします- http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller - itテスト、メンテナンスを簡素化し、一般的にフォームを簡素化する必要があります。

于 2012-05-02T21:48:07.543 に答える
0

「カスタム作成」チェックボックスをマークしたことがありますか? コンポーネント (この場合は JButton) のカスタム コードを追加する場合、プロパティ インスペクターでこのオプションをマークする必要があります。

于 2016-02-25T19:17:20.673 に答える