簡體   English   中英

Java-調用嵌套類中的方法,特別是ActionListener

[英]Java - Calling methods in a nested class, specifically an ActionListener

編輯:解決了,添加組件后我沒有使用'validate()'。

我有一個結構像這樣的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!”)來調用它。

我已經測試了代碼的所有其他方面,這似乎是唯一的問題。

運行代碼時沒有錯誤。

如果需要,我可以發布完整的類,但是我認為這個問題在於嘗試調用這些方法。

你怎么看?

調用該方法效果很好,不需要花哨的東西

您需要將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();
    }
}

在運行時在容器中添加/刪除組件是可能的,但不一定是好的做法。 如果要基於GUI中的用戶選擇顯示不同的內容,請考慮使用CardLayout布局管理器,該管理器會自動處理所有運行時詳細信息。

吉姆·S。

您的代碼應該可以正常工作。 您將必須將ActionListener添加到組件。

其他

您可以在Handler類中編寫displayButton方法。 然后,您只需按原樣調用方法即可訪問上一個類的成員。

快速而骯臟的選項是使displayButton調用變為靜態。

那么,問題更多的是設計問題。 我建議您自問,為什么要讓動作監聽器在擁有對象中調用方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM