簡體   English   中英

Java Swing ActionListener顯示JButton數組

[英]Java Swing ActionListener Display JButton Array

我正在嘗試使用Java Swing編寫Battleship程序,目前我有一個可以創建兩個網格的類。 我試圖找出單擊了哪個按鈕的位置,以便以后可以使用它來放置鏡頭等。不幸的是,我對此頗有麻煩。

我已經有了使用actionPerformed方法來打印出其中所有內容的對象,但是我只想要grid [x] [y]。 我該怎么辦?

在此先感謝您的幫助。

package testapp;

/**
 *
 * @author Craig
 */
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;

public class menu extends JPanel implements ActionListener{
        JButton[][] grid;
        TextField text = new TextField(20);

    public menu(int width, int length) {


        Border playerBorder = BorderFactory.createTitledBorder("Player");
        Border comBorder = BorderFactory.createTitledBorder("Com");


        JPanel player = new JPanel();
        player.setBorder(playerBorder);// set border round player grid 
        player.setLayout(new GridLayout(4,4));

        grid=new JButton[width][length]; //allocate the size of grid
        for(int y=0; y<length; y++){
            for(int x=0; x<width; x++){
                grid[x][y]=new JButton(); //creates new button    
                player.add(grid[x][y]); //adds button to grid
                grid[x][y].setBackground(Color.BLUE);//sets grid background colour
                grid[x][y].setPreferredSize(new Dimension(40, 40));//sets each grid buttons dimensions
                add(text);
                grid[x][y].addActionListener(this);
            }
        }

        JPanel com = new JPanel();
        com.setBorder(comBorder);// set border round com grid        
        com.setLayout(new GridLayout(4,4));
        grid=new JButton[width][length]; //allocate the size of grid
        for(int y=0; y<length; y++){
            for(int x=0; x<width; x++){
                grid[x][y]=new JButton(); //creates new button
                com.add(grid[x][y]); //adds button to grid
                grid[x][y].setBackground(Color.BLUE);//sets grid background colour
                grid[x][y].setPreferredSize(new Dimension(40, 40));//sets each grid buttons dimensions
            }
        }        

        //this.setLayout(new FlowLayout());
        this.add(player);
        this.add(com);

}
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source instanceof JButton) {
            JButton btn = (JButton)source;
            text.setText("IN THE BOX ");
        }

    }

}

有不同的選擇。 擴展JButton應該是恕我直言,並且幾乎沒有必要。 遍歷grid [] []數組並檢查它們是否是相應事件的source可能是可以的。 但是另一個(恕我直言,簡單而優雅的)解決方案是使用匿名偵聽器:

// Where the grid buttons are created:
....
grid[x][y].addActionListener(createActionListener(x, y));


private ActionListener createActionListener(final int x, final int y)
{
    return new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            clickedButton(x, y);
        }
    };
}

private void clickedButton(int x, int y)
{
    System.out.println("Clicked "+x+" "+y);
}

編輯:同樣,以http://sscce.org/的形式(您可以創建一個,然后我可以將您的示例中的答案集成...)

/**
 *
 * @author Craig and me :D
 */
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;

public class menu extends JPanel {

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new menu(4,4));
        f.setLocationRelativeTo(null);
        f.pack();
        f.setVisible(true);
    }

    JButton[][] grid;
    TextField text = new TextField(20);

    public menu(int width, int length) {


        Border playerBorder = BorderFactory.createTitledBorder("Player");
        Border comBorder = BorderFactory.createTitledBorder("Com");


        JPanel player = new JPanel();
        player.setBorder(playerBorder);// set border round player grid 
        player.setLayout(new GridLayout(4,4));

        grid=new JButton[width][length]; //allocate the size of grid
        for(int y=0; y<length; y++){
            for(int x=0; x<width; x++){
                grid[x][y]=new JButton(); //creates new button    
                player.add(grid[x][y]); //adds button to grid
                grid[x][y].setBackground(Color.BLUE);//sets grid background colour
                grid[x][y].setPreferredSize(new Dimension(40, 40));//sets each grid buttons dimensions
                add(text);
                grid[x][y].addActionListener(
                    createActionListener(x, y, "Player"));
            }
        }

        JPanel com = new JPanel();
        com.setBorder(comBorder);// set border round com grid        
        com.setLayout(new GridLayout(4,4));
        grid=new JButton[width][length]; //allocate the size of grid
        for(int y=0; y<length; y++){
            for(int x=0; x<width; x++){
                grid[x][y]=new JButton(); //creates new button
                com.add(grid[x][y]); //adds button to grid
                grid[x][y].setBackground(Color.BLUE);//sets grid background colour
                grid[x][y].setPreferredSize(new Dimension(40, 40));//sets each grid buttons dimensions
                grid[x][y].addActionListener(
                    createActionListener(x, y, "Computer"));
            }
        }        

        //this.setLayout(new FlowLayout());
        this.add(player);
        this.add(com);

    }
    private ActionListener createActionListener(
        final int x, final int y, final String name)
    {
        return new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                clickedButton(x, y, name);
            }
        };
    }

    private void clickedButton(int x, int y, String name)
    {
        System.out.println("Clicked "+x+" "+y+" for "+name);
    }
}

暫無
暫無

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

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