简体   繁体   English

动作侦听器Java问题

[英]Action listener java problems

Ok so im working on a game and right now i have an image and im trying to place buttons under the image in specific places so when the user clicks that part of the screen the image changes. 好的,我正在玩游戏,现在我有一个图像,我试图将按钮放置在图像下方的特定位置,因此当用户单击屏幕的该部分时,图像会更改。 I am having trouble trying to figure out how to format my program for the action listener. 我在尝试弄清楚如何为动作侦听器格式化程序时遇到麻烦。

public class TestJFrame{
    private static JFrame frame = new JFrame();
    private static JLabel label = new JLabel();
    private static JButton buttons[] = new JButton[4];

    private static int[][] location = new int[3][4];


    public static void main(String args[]){
        frame.getInsets().set(20, 5, 5, 5);

        frame.setLayout(null);
        frame.setPreferredSize(new Dimension(507, 528));
        frame.pack();
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Test");

        buttons[0] = new JButton("jbZero");
        buttons[1] = new JButton("jbOne"); 
        buttons[2] = new JButton("jbTwo");
        buttons[3] = new JButton("jbThree");

        frame.add(buttons[0]);
        frame.add(buttons[1]);
        frame.add(buttons[2]);
        frame.add(buttons[3]);

        setButtons();
        frame.setVisible(true);

        buttons[0].setLocation(100, 100);
    }


    private static void setButtons(){
        for (int i=0;i<=3;i++){
            buttons[i].setSize(10, 10);
            buttons[i].setLocation(0, 0);
            buttons[i].setVisible(true);
        }
    }

    public void intializeListener(){
        buttons[0].addActionListener((ActionListener) this);        
    }

    public void buttonsZeroActionPreformed(java.awt.event.ActionEvent e){
        System.out.println("button zero works");
    }
}

So any help would be greatly appreciated. 因此,任何帮助将不胜感激。

Depends what do you want to implement in the listener: 取决于您要在侦听器中实现什么:

  1. If they do the same action you implement the listener inside setButtons function: 如果它们执行相同的操作,则可以在setButtons函数内实现侦听器:

     private static void setButtons(){ for (int i=0;i<=3;i++){ buttons[i].setSize(10, 10); buttons[i].setLocation(0, 0); buttons[i].setVisible(true); buttons[i].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // listener implement btn 0 } }); } } 
  2. If each button need unique implementation I think that better implementation will be in a new function setBtnListeners and call it from your main after using setButtons(); 如果每个按钮需要唯一的实现,我认为更好的实现将在新函数setBtnListeners并在使用setButtons();之后从主函数调用它setButtons(); :

     private static void setBtnListeners() { buttons[0].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // listener implement btn 0 } }); } 

Update: Sorry i'd just noticed you have four buttons... you can just add another one :) 更新:对不起,我刚刚注意到您有四个按钮...您可以添加另一个按钮:)

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

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