2012-02-29 11 views
0

編集:コンポーネントを追加した後で 'validate()'を使用していませんでした。Java - ネストされたクラスのメソッド、特にActionListenerを呼び出す

編集:ここ

package source; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 

public class Gui extends JFrame 
{ 
    public String[] list1 = {"equation1","equation2","equation3","equation4", "equation5"}; 
    public JOptionPane opt1; 
    private JButton custom; 
    private JTextField[] tf; 
    public HandlerClass2 itemhandler = new HandlerClass2(); 
    private JList list; 
    private static int index = 0; 
    private static int lastlistindex = 0; 
    private JPanel buttonpanel; 
    private JPanel buttonpanel2[] = new JPanel[3]; 
    private JPanel listpanel[] = new JPanel[4]; 
    private JPanel checkpanel; 
    private JCheckBox checkboxes[]; 
    private SpringLayout layout; 
    public Container contentPane; 
    private JButton but; 

public Gui() 
{ 
    super("Physics Helper v0.1"); 
    setBackground(Color.DARK_GRAY); 

    layout = new SpringLayout(); 

    contentPane = getContentPane(); 
    contentPane.setLayout(layout); 

    displayPortal(); 
} 

public void displayPortal() 
{ 
    Icon a = new ImageIcon(getClass().getResource("button.png"));  
    Icon b = new ImageIcon(getClass().getResource("button2.png")); 
    custom = new JButton("", a); 
    custom.setRolloverIcon(b); 

    buttonpanel = new JPanel(); 
    buttonpanel.setBackground(Color.GRAY); 
    buttonpanel.add(custom); 
    contentPane.add(buttonpanel); 

    layout.putConstraint(SpringLayout.WEST, buttonpanel, 5, SpringLayout.WEST, contentPane); 
    layout.putConstraint(SpringLayout.EAST, buttonpanel, -5, SpringLayout.EAST, contentPane); 
    layout.putConstraint(SpringLayout.NORTH, buttonpanel, 5, SpringLayout.NORTH, contentPane); 

    custom.addActionListener(new HandlerClass()); 

} 

public void displayButton(String s) 
{ 
    but = new JButton(s); 

    buttonpanel2[index] = new JPanel(); 
    buttonpanel2[index].setBackground(Color.GRAY); 
    buttonpanel2[index].add(but); 

    contentPane.add(buttonpanel2[index]); 

    layout.putConstraint(SpringLayout.SOUTH, buttonpanel2[index], -5, SpringLayout.SOUTH, contentPane); 

    if (index < 1) 
    { 
     layout.putConstraint(SpringLayout.WEST, buttonpanel2[index], 5, SpringLayout.WEST, contentPane); 
    } 
    else 
    { 
     layout.putConstraint(SpringLayout.WEST, buttonpanel2[index], 5, SpringLayout.EAST, buttonpanel2[index - 1]); 
    } 

    index++; 
} 

public void displayList(String[] t) 
{ 
    list = new JList(t); 
    list.setVisibleRowCount(8); 
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    add(new JScrollPane(list)); 

    listpanel[lastlistindex] = new JPanel(); 
    listpanel[lastlistindex].setBackground(Color.GRAY); 
    listpanel[lastlistindex].add(list); 

    contentPane.add(listpanel[lastlistindex]); 

    layout.putConstraint(SpringLayout.NORTH, listpanel[lastlistindex], 5, SpringLayout.SOUTH, buttonpanel); 

    if (lastlistindex < 1) 
    { 
     layout.putConstraint(SpringLayout.WEST, listpanel[lastlistindex], 5, SpringLayout.WEST, contentPane); 
    } 
    else 
    { 
     layout.putConstraint(SpringLayout.WEST, listpanel[lastlistindex], 5, SpringLayout.EAST, listpanel[lastlistindex - 1]); 
    } 

    lastlistindex++; 
} 

public void displayInputValues(String[] p) 
{ 
    checkboxes = new JCheckBox[p.length]; 

    GridLayout gridlayout = new GridLayout(p.length, 2); 
    tf = new JTextField[p.length]; 

    checkpanel = new JPanel(); 
    checkpanel.setBackground(Color.GRAY); 
    checkpanel.setLayout(gridlayout); 

    for (int b = 0; b < p.length; b++) 
    { 
     checkboxes[b] = new JCheckBox(p[b]); 
     checkpanel.add(checkboxes[b]); 

     tf[b] = new JTextField("", 9); 
     checkpanel.add(tf[b]); 
     tf[b].setFont(new Font("Serif", Font.PLAIN, 14)); 
    } 

    contentPane.add(checkpanel); 

    layout.putConstraint(SpringLayout.EAST, checkpanel, -5, SpringLayout.EAST, contentPane); 
    layout.putConstraint(SpringLayout.SOUTH, checkpanel, -5, SpringLayout.SOUTH, contentPane); 
} 

private class HandlerClass implements ActionListener 
{ 

    public void actionPerformed(ActionEvent event) 
    {    
     displayButton("Back"); 
     displayButton("Next"); 

     displayList(list1); 
    } 
}  

私の主な方法は、別のクラスの中に含まれている私の完全なコードだ

は、私は(これは私のコードの非常に基本的な表現である)このようなGUIクラス構造の何かを持っています、うまく動作します。

私の質問は、actionPerformedメソッドの "displayButton"メソッドをどのように呼び出すことができますか? "Gui.this.displayButton(" Press me! ")と呼ぶなど、いくつかのヒントを既に試してみました。

私のコードの他のすべての面をテストしましたが、これは唯一の問題です。

私は、コードを実行したとき、私はエラーを取得していない。

ニーズがあることならば、私は完全なクラスを投稿することができますが、私はこの問題はこれらのメソッドを呼び出そうとしてあると思います。あなたの意見だ

何?

+0

displayButton( "Press me!")の最後にセミコロンがありません。私はそれがタイプミスだと思う。 – JProgrammer

+0

"コードを実行するとエラーは発生しません。" - しかし、あなたのコードはコンパイルされません... – DNA

+0

コンパイルエラーを修正すると、それは私のために働きます(編集:@ DNAの推薦に従ってボタンにハンドラを追加すると、1+私はあなたの問題がコード以外の場所、特にここにあると考えています: '// contentPaneにボタンを含むJPanelを表示するコードmore –

答えて

4

このメソッドを呼び出すと問題なく動作します。

JButtonのようなGUIコントロールにHandlerClassのインスタンスを追加する必要があります。クリックすると、このメソッドがトリガーされます。それはActionListeners(および一般にリスナー)の全体的なポイントです。あなたのコードに基づいて

myJButton.addActionListener(new HandlerClass()); 

作業例:たとえば

public class Gui extends JFrame 
{ 
    public Gui() 
    { 
     super("Physics Helper v0.1"); 
     JButton b = new JButton("Press me!"); 
     b.addActionListener(new HandlerClass()); 
     add(b); 
     pack(); 
     setVisible(true); 
    } 

    public void displayButton(String s) 
    { 
     System.out.println(s); 
    } 

    private class HandlerClass implements ActionListener 
    { 
     public void actionPerformed(ActionEvent event) 
     { 
      displayButton("Press me!"); 
     } 
    } 

    public static void main(String[] args) 
    { 
     new Gui(); 
    } 
} 
+0

これが質問の一部であるかどうかはわかりませんが、myJButton.doClick()を呼び出すことで、プログラムでクリックをシミュレートできます。http://docs.oracle.com/javase/6/docs/api/javax/swing/AbstractButton.html #doClick%28%29 – assylias

+0

@assylias:そうです、それは質問の一部ではありません。 1+の投票。 –

+0

これを以前は誤解していましたが、試してみようとしています。指が交差した。 – HJED

-1

迅速かつ汚いオプションがdisplayButtonコールが静的にすることです。

次に、問題は他のものよりもデザイン的なものです。自分のアクションリスナーが所有オブジェクト内のメソッドを呼び出すようにする理由を自分自身に尋ねることをお勧めします。

+1

すべての問題に対して、迅速、汚れ、シンプル、きれい、間違っている解決策があります(H.L. Menckenにお詫び申し上げます)。 –

+0

@Brent ActionListenerから所有者のメソッドを呼び出すことは、非常に一般的なパターンです。 – DNA

1

実行時にコンテナ内のコンポーネントを追加/削除することは可能ですが、必ずしも良い方法ではありません。ユーザの選択に基づいて異なる内容をGUIに表示する場合は、すべてのランタイムの詳細を自動的に処理するCardLayoutレイアウトマネージャを使用することを検討してください。

ジムS.

+0

良いお勧めとおめでとうございます。今私は7時間待たなければならない。 :( –

+0

これは私が探しているものに近いです。恥ずかしくて、私はメソッドを書き直さなければならないことを意味します。 – HJED

0

コードは細かく動作するはずです。コンポーネントにaddActionListenerを追加する必要があります。

Else

Handlerクラス内にdisplayButtonメソッドを記述することができます。 次に、以前のクラスのメンバーにアクセスして、そのままメソッドを呼び出さなければなりません。

関連する問題