2012-05-02 9 views
0

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

は、イベントハンドラである:私はjFormDesignerフォームは動作しますが、イベントなしでテストフォームをクリックすると

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

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

は、ここでの考え方コードをのIntelliJによって生成されます。イベントをテストするには? JButtonため

+0

Netbeansを使用しましたが、問題はありません。今私はintellijのアイデアを使用する必要があります。 – Mirgorod

+0

すぐに役立つように、[SSCCE](http://sscce.org/)を投稿してください。 –

答えて

1

は私がMouseListenerをと思いActionListener

) ので、最も一般的な全体Swing JComponents

2)()のための

1)Swing Action非常にスケーラブルな回避策を使用することが良いです間違っているSwing ListenerJButton

+0

回答者のためのThanx、しかし私はintellijのアイデアで何をする必要がありますか? – Mirgorod

+0

同じように、MouseListenerを追加したのと同じように、 – mKorbel

+0

分待ってから、スクリーンショットを表示します。 – Mirgorod

0

なぜあなたはモックでテストしませんか?そこにはたくさんの模擬フレームワークがあります。あなたはMockito、JMock、JMockitなどを使うことができます... Mockitoは良いスタートになるでしょう。あなたが実際にGUIアクションをシミュレートしたいのであれば...それはまったく別の領域です。 私はそれを最初に嘲笑して、そのようにテストすることをお勧めします。

また、フォームはのIntelliJアイデアによってgenereatedされていませんが、JFormDesigner - 素晴らしいアイデアです - あなたは抽象GUIを使用することができ、特定のIDEにそれを抱き合わせていません。 http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller - - それは、テスト、maintenenceを簡素化し、generalyフォームを簡素化すべきであるあなたが、Swingコンポーネントの多くを使用しようとしている場合

また、私はあなたがMVCパターンで行くお勧めします。

+0

Thanx、なぜIntellij IdeaではNetBeansのようなGUIアプリケーションを(イベントなどで)実行できないのですか? – Mirgorod

+0

できます。 JFormDesigner用のプラグインがあります。このプラグインはhttp://www.formdev.com/jformdesigner/doc/ides/intellij-idea/で入手できます。 – pfh

+0

プラグインは開発を単純化するはずですが、実際にフォームを実行することはできます。これは生成されたSwingコードです。あなたの写真に基づいて、 "MouseListener"をクリックすることができます。これを見てください - http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/MouseAdapter.html。私はあなたのボタンを動作させるのが簡単だと思います。ちょうど "mousePressed"をovverideし、あなたは行く準備ができました! – pfh

0

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

関連する問題