简体   繁体   English

如何将JButton操作侦听器链接到其他类中的接口

[英]How to link a JButton action listener to an interface from anther class

so I have an interface that reads a 2D Char array and is supposed to make a graphical drawing in a newly created window. 所以我有一个读取2D Char数组的界面,应该在新创建的窗口中绘制图形。

Here's the code: 这是代码:

class View extends JFrame {

        private final List<Integer> path = new ArrayList<Integer>();
        private int pathIndex;

        private View() {
            setTitle("Simple Maze Solver");
            setSize(640, 480);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            //DepthFirst.searchPath(storelab, 1, 1, path);
            pathIndex = path.size() - 2;
     }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.translate(50, 50);

        // draws the maze
        for (int row = 0; row < storelab.length; row++) {
            for (int col = 0; col < storelab[0].length; col++) {
                Color color;
                switch (storelab[row][col]) {
                    case 1 : color = Color.BLACK; break;
                    case 9 : color = Color.RED; break;
                    default : color = Color.WHITE;
                }
                g.setColor(color);
                g.fillRect(30 * col, 30 * row, 30, 30);
                g.setColor(Color.BLACK);
                g.drawRect(30 * col, 30 * row, 30, 30);
            }
        }

        // draw the path list
        for (int p = 0; p < path.size(); p += 2) {
            int pathX = path.get(p);
            int pathY = path.get(p + 1);
            g.setColor(Color.GREEN);
            g.fillRect(pathX * 30, pathY * 30, 30, 30);
        }

        // draw the ball on path
        int pathX = path.get(pathIndex);
        int pathY = path.get(pathIndex + 1);
        g.setColor(Color.RED);
        g.fillOval(pathX * 30, pathY * 30, 30, 30);
    }

    @Override
    protected void processKeyEvent(KeyEvent ke) {
        if (ke.getID() != KeyEvent.KEY_PRESSED) {
            return;
        }
        if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
            pathIndex -= 2;
            if (pathIndex < 0) {
                pathIndex = 0;
            }
        }
        else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
            pathIndex += 2;
            if (pathIndex > path.size() - 2) {
                pathIndex = path.size() - 2;
            }
        }
        repaint();
    }

    public void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                View view = new View();
                view.setVisible(true);


            }


        });

    }
 }
}

It's supposed to make a graphical drawing. 应该制作一个图形绘图。 Now, in another class, my Interface class, as you can probably guess :D, I have an interface with some buttons, now my problem is, how do I link the graphical drawing of my array to the button in the Interface class? 现在,在另一个类(我的Interface类)中,您可能会猜到:D,我有一个带有一些按钮的接口,现在的问题是,如何将数组的图形链接到Interface类中的按钮? When I click that button, it's supposed to trigger the JFrame class. 当我单击该按钮时,它应该触发JFrame类。

Here's a relevant snippet of it: 这是相关的代码段:

public static void main(String[] args) {
    Ch14JButtonFrame frame = new Ch14JButtonFrame();
    frame.setVisible(true);
}

public Ch14JButtonFrame() {

    /*Vai ser definido os parametros (dimensoes, titulo, etc.) da iterface*/
    Container contentPane = getContentPane();
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setResizable(true);
    setTitle("Jogo do Labirinto 37732 37861");
    setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    contentPane.setLayout(new FlowLayout());


    **showButton = new JButton("Show Labyrinth");
    contentPane.add(showButton);
    showButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

        }**
    });

The button that's supposed to trigget it, is named showButton . 应该触发它的按钮名为showButton Any help appreciated! 任何帮助表示赞赏!

If you want to open the frame in a new window you could do this: 如果要在新窗口中打开框架,可以执行以下操作:

showButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        new View().setVisible(true);
    }
});

Also you must change your View class to public and also the constructor. 另外,您还必须将View类更改为public,并将其更改为构造函数。

public class View extends JFrame {
    ...
    public View() {
        ...
    }
}

But if you want to embed the content of the View Frame in another frame that's another process. 但是,如果您要将View Frame的内容嵌入另一个框架中,那是另一个过程。

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

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