2017-10-23 15 views
-3

これは私のコードですどのようにアクションリスナーを追加できますか?私は既に知られているメソッドを試しましたが、静的メソッドのために追加できません.Netbeans IDEはプログラムのアクションリスナーを提案しましたそれを使用することができない人は、アクションリスナーを宣言するより簡単な方法を提案することができます。gridbaglayoutにアクションリスナーを追加できません

パブリッククラスのCalc {

public static void addComponentsToPane(Container pane) { 


     pane.setLayout(new GridBagLayout()); 
     GridBagConstraints gBC = new GridBagConstraints(); 

     gBC.ipady = 40; 
     gBC.ipadx = 40; 

     JTextField JTextField = new JTextField("Hello"); 
     gBC.fill = GridBagConstraints.HORIZONTAL; 
     gBC.gridx = 0; 
     gBC.gridy = 0; 
     gBC.gridwidth = 4; 
     JTextField.setEditable(false); 
     pane.add(JTextField, gBC); 

     //JButton jbnButton; 
     gBC.gridwidth = 1; 

     JButton b7 = new JButton("7"); 
     gBC.gridx = 0; 
     gBC.gridy = 1; 
     pane.add(b7, gBC); 
     // more calculator buttons declared here 

//ここではactionlistner

 b0.addActionListener(new java.awt.event.ActionListener() { 
     @Override 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      b0ActionPerformed(evt); 
     } 

     private void b0ActionPerformed(ActionEvent evt) } 
     ` 
    }); 
    b0.addActionListener(new java.awt.event.ActionListener() { 
    public void actionPerformed(java.awt.event.ActionEvent evt) { 
     b0ActionPerformed(evt); 
    } 
    private void b0ActionPerformed(Actionenter Event evt) {} 
    });} 

    private static void createAndShowGUI() {} 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 

示唆どのように}私はあなたのためにこれを試してみてください

+0

SOにようこそ。 https://stackoverflow.com/help/how-to-askを読むことを検討してください。どのコンポーネントにactionListenerを追加したいのですか?これまでに何を試みましたか? –

+0

私の電卓ボタンが目的の操作を実行するコードのどこにでもリスナーを追加したい。クリックすると – Prapti

答えて

0

を。宣言したコンポーネントごとにaddComponentsToPaneメソッドにアクションリスナーを追加できます。また、フレームを渡すのを忘れてしまったので、メインメソッドで渡すことができます。

public class Calc { 

public static void addComponentsToPane(Container pane) { 

    pane.setLayout(new GridBagLayout()); 
    GridBagConstraints gBC = new GridBagConstraints(); 

    gBC.ipady = 40; 
    gBC.ipadx = 40; 

    JTextField JTextField = new JTextField("Hello"); 
    gBC.fill = GridBagConstraints.HORIZONTAL; 
    gBC.gridx = 0; 
    gBC.gridy = 0; 
    gBC.gridwidth = 4; 
    JTextField.setEditable(false); 
    pane.add(JTextField, gBC); 

    //JButton jbnButton; 
    gBC.gridwidth = 1; 

    JButton b7 = new JButton("7"); 
    gBC.gridx = 0; 
    gBC.gridy = 1; 
    pane.add(b7, gBC); 
    // more calculator buttons declared here 

    b7.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      // Write your action here 

     } 
    }); 

} 

private static void createAndShowGUI(JFrame pane) { 
    addComponentsToPane(pane); 
    pane.setVisible(true); 
} 

public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(new JFrame()); 
     } 
    }); 
} 

}

+0

ありがとうございました。 NetBeansのデザインタブでGUIプログラミングを常に行っているため、私は問題を抱えていました。 – Prapti

+0

私の答えを正解とすることはできますか? – Tadesse

関連する問題