2012-05-02 43 views
0

我是新的intellij想法和jFormDesigner。 我想測試我的應用程序。 我添加jFormDesigner文件到我的項目中,創建窗體並添加了簡單的按鈕和textarea。我添加了鼠標點擊事件按鈕,但我不知道如何測試它。intellij想法的運行GUI應用程序

這裏是事件處理程序:

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

這裏是由IntelliJ IDEA的代碼生成:

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形式的作品,但事件沒有測試形式。如何測試事件?

+0

我以前用過Netbeans並沒有問題。現在我需要使用intellij的想法。 – Mirgorod

+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

0

你爲什麼不用mock測試它?這裏有很多嘲諷框架。你可以使用Mockito,JMock,JMockit等...... Mockito會是一個好的開始。如果你想實際模擬圖形用戶界面的操作...這是一個完整的其他領域。 我建議你先嘲弄一下,測試一下。

此外,表單不是由Intellij Idea生成的,而是JFormDesigner - 您可以使用它來抽象GUI,而不是將它捆綁到特定的IDE--這是一個好主意。

另外,如果你要使用大量的Swing組件,我建議你去與MVC模式 - http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller - 它應該簡化測試,maintenence和generaly簡化形式。

+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」包起來,你就準備好了! – pfh

0

你有沒有標記de複選框「自定義創建」?當爲組件添加自定義代碼時,在本例中爲JButton,您必須在其屬性檢查器中標記此選項。

相關問題