2012-02-29 23 views
0

编辑:已解决,添加组件后我没有使用“validate()”。Java - 调用嵌套类中的方法,特别是ActionListener

我有一个GUI类的结构是这样的(这是我的代码一个非常基本的表示):

编辑:这里是我的全部代码

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); 
    } 
}  

我的主要方法是包含在另一个类中,并且工作正常。

我的问题是如何在actionPerformed方法中调用“displayButton”方法?我已经尝试了一些技巧,比如用“Gui.this.displayButton(”Press me!“)调用它。

我测试了我的代码的其他方面,这似乎是唯一的问题。

我没有得到任何错误,当我运行的代码。

如果需要的话,我可以张贴满级的,但我想这个问题就在于试图调用这些方法。

你对此有何看法?

+0

您在displayButton末尾缺少分号(“Press me!”)。我猜这是错字。 – JProgrammer 2012-02-29 16:33:42

+0

“我在运行代码时没有遇到任何错误。” - 但是你的代码不能编译... – DNA 2012-02-29 16:34:17

+0

如果你修正了编译错误,它对我很有用(编辑:如果你按照@ DNA的推荐添加处理程序到按钮 - 1+投票给他的答案)。我认为你的问题存在于代码中的其他地方,尤其是在这里:'//更多代码来显示包含contentPane中的按钮的JPanel' – 2012-02-29 16:35:45

回答

4

调用该方法工作正常,没有什么奇特需要

您需要将HandlerClass的实例添加到GUI控件,例如JButton,该控件在单击时将触发该方法。这是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 – assylias 2012-02-29 16:38:25

+0

@assylias:你是对的 - 这不是问题的一部分。 1 +投票。 – 2012-02-29 16:39:40

+0

更早尝试此错误,但即将尝试。手指交叉。 – HJED 2012-02-29 19:43:59

-1

的快速和肮脏的方法是使displayButton调用静态的。

然后问题是比其他任何设计更多。我建议你问自己为什么你希望你的动作监听器调用拥有对象中的方法。

+1

对于每个问题都有一个快速,肮脏,简单,干净和错误的解决方案(向H. Mencken道歉)。 – 2012-02-29 16:38:47

+0

@Brent从ActionListener调用所有者的方法是一种非常常见的模式 – DNA 2012-02-29 16:51:09

1

在运行时添加/删除容器中的组件是可能的,但不一定是好的做法。如果要在GUI中根据用户选择显示不同的内容,请考虑使用CardLayout布局管理器,该管理器可自动处理所有运行时细节。

Jim S.

+0

好的建议和祝贺 - 您刚刚获得了当天的最后一票。现在我必须等待7个小时。 :( – 2012-02-29 16:43:03

+0

这与我正在寻找的东西很接近,这意味着我必须重写方法。 – HJED 2012-02-29 19:21:08

0

您的代码应该工作得很好。您必须将ActionListener添加到组件。

其他

您可以在Handler类中编写displayButton方法。 然后,您将不得不简单地调用该方法,因为它可以访问前一个类的成员。