简体   繁体   English

如何将动作侦听器添加到 static Jbutton?

[英]How do I add an action listener to a static Jbutton?

I need to call the homeIcons method in another class statically but I don't know how to add the action listener to the static button.我需要在另一个 class 中静态调用 homeIcons 方法,但我不知道如何将动作侦听器添加到 static 按钮。 Is there a way to do this simply?有没有办法简单地做到这一点? I need to use this panel in a CardLayout to refresh a frame.我需要在 CardLayout 中使用此面板来刷新框架。 The Buttons change the panel in the CardLayout, so I need the action listeners to work.按钮更改 CardLayout 中的面板,因此我需要动作侦听器才能工作。

Need to add this panel to add to a CardLayout需要添加此面板才能添加到 CardLayout

public class Home extends JFrame implements ActionListener {
    
    //fields
    private static JButton sandwich;
    private static JButton burger;
    private static JButton pancake;
    private static JButton ramen;
    private static JButton beefWellington;
    private static JButton help;
    private static JPanel homeBody;

public static JPanel homeIcons() {
        JPanel topButtons = new JPanel();
        GridLayout topLayout = new GridLayout(1,2);
        topButtons.setBorder(BorderFactory.createEmptyBorder(0,120,0,120));
        topLayout.setVgap(5);
        topLayout.setHgap(150);
        topButtons.setLayout(topLayout);
        topButtons.setBackground(new Color(200,200,200));
        
        //Level 1, make a sandwich
        ImageIcon sandwichImage = new ImageIcon("Resources/sandwichthumbnail.png");
        sandwich = new JButton();
        sandwich.setIcon(sandwichImage);
        sandwich.setHorizontalAlignment(JButton.CENTER);
        sandwich.setBackground(Color.WHITE);
        sandwich.addActionListener(this);
        this.add(sandwich);
        topButtons.add(sandwich);
        
        //Level 2, make a burger
        ImageIcon burgerImage = new ImageIcon("Resources/burgerthumbnail.png");
        burger = new JButton();
        burger.setIcon(burgerImage);
        burger.setHorizontalAlignment(JButton.CENTER);
        burger.setBackground(Color.WHITE);
        burger.addActionListener(this);;
        this.add(burger);
        topButtons.add(burger);
        
        JPanel lowerButtons = new JPanel();
        GridLayout lowerLayout = new GridLayout(1,2);
        lowerButtons.setBorder(BorderFactory.createEmptyBorder(0,15,0,15));
        lowerLayout.setVgap(5);
        lowerLayout.setHgap(5);
        lowerButtons.setLayout(lowerLayout);
        lowerButtons.setBackground(new Color(200,200,200));
        
        //Level 3, make a souffle pancake
        ImageIcon pancakeImage = new ImageIcon("Resources/pancakethumbnail.png");
        pancake = new JButton();
        pancake.setIcon(pancakeImage);
        pancake.setHorizontalAlignment(JButton.CENTER);
        pancake.setBackground(Color.WHITE);
        pancake.addActionListener(this);
        this.add(pancake);
        lowerButtons.add(pancake);
        
        //Level 4, 
        ImageIcon ramenImage = new ImageIcon("Resources/ramenthumbnail.png");
        ramen = new JButton();
        ramen.setIcon(ramenImage);
        ramen.setHorizontalAlignment(JButton.CENTER);
        ramen.setBackground(Color.WHITE);
        ramen.addActionListener(this);;
        this.add(ramen);
        lowerButtons.add(ramen);
        
        ImageIcon wellingtonImage = new ImageIcon("Resources/wellingtonthumbnail.png");
        beefWellington = new JButton();
        beefWellington.setIcon(wellingtonImage);
        beefWellington.setHorizontalAlignment(JButton.CENTER);
        beefWellington.setBackground(Color.WHITE);
        beefWellington.addActionListener(this);;
        this.add(beefWellington);
        lowerButtons.add(beefWellington);
        
        homeBody.add(lowerButtons);
        homeBody.add(topButtons);
        return homeBody;
    }
}

While I question the code layout choice, you could pass though parameters for each button action listener, assuming you need the context provided from the calling method虽然我质疑代码布局选择,但您可以为每个按钮操作侦听器传递参数,假设您需要调用方法提供的上下文

Note: within a static method, there's no this注意:在 static 方法中,没有this

static JPanel homeIcons(ActionListener sandwichAction) {
    JPanel topButtons = new JPanel();

    JButton sandwich = new JButton();
    sandwich.addActionListener(sandwichAction);

    topButtons.add(sandwich);
    
    return topButtons;
} 

Otherwise, inline new ActionListener() into the button method itself否则,将new ActionListener()内联到按钮方法本身

Other than there can't be this in static context.除了在 static 上下文中不能有这个 You can implement each as:您可以将每个实现为:

sandwich.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //action listener here
        }
    });

You have duplicate code, each segment of which has a few common requirements.你有重复的代码,每个部分都有一些共同的要求。 You should make a general method for each button.您应该为每个按钮制定一个通用方法。 Something like this.像这样的东西。 You may need to adjust a few things since this is out of context of your actual code.您可能需要调整一些事情,因为这超出了您的实际代码的上下文。

 
public  JButton createButton(String imagePath, ActionListener buttonListener) { 
        ImageIcon foodImage = new ImageIcon(imagePath);
        foodButton = new JButton();
        foodButton.setIcon(foodImage);
        foodButton.setHorizontalAlignment(JButton.CENTER);
        foodButton.setBackground(Color.WHITE);
        foodButton.addActionListener(buttonListener);
        this.add(foodButton);  // <--- not certain what "this" is??
        lowerButtons.add(foodButton); // <-- might be best to add upon return
        return foodButton;
}

You may need to add the button upon return from this method.您可能需要在从此方法返回时添加按钮。 You may even be able to make this method static if you don't need to reference any instance fields.如果您不需要引用任何实例字段,您甚至可以使用此方法 static。

And regarding declaring you buttons static (which I would not do).关于声明你的按钮 static (我不会这样做)。 Just because something is static does not mean it cannot hold an instance of something else (like an actionListener).仅仅因为某事物是static并不意味着它不能保存其他事物的instance (例如 actionListener)。 It can just make it more difficult.它只会让它变得更加困难。 Especially if the enclosing class implements a common actionListener.特别是如果封闭的 class 实现了一个通用的 actionListener。 That precludes being able to reference the listener via this .这排除了通过this引用监听器的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM