Swing按钮重画问题

发布于 2024-08-29 06:15:36 字数 4364 浏览 10 评论 0原文

我是java新手,我必须在周日之前完成一个学校项目,但遇到了问题。

代码如下:

private abstract class GamePanel {
    JPanel panel = null;
}

private class PutPanel extends GamePanel {

    JButton putShip1 = new JButton("");
    JButton putShip2 = new JButton("");
    JButton putShip3 = new JButton("");
    JButton putShip4 = new JButton("");

    ShipDirection ship1Direction = ShipDirection.NORTH;
    ShipDirection ship2Direction = ShipDirection.NORTH;
    ShipDirection ship3Direction = ShipDirection.NORTH;
    ShipDirection ship4Direction = ShipDirection.NORTH;



    JButton startButton = new JButton("Start game");

    public PutPanel(){
        this.panel = new JPanel();
        panel.setSize(200, Torpedo.session.map.size*Field.buttonSize+300);

        panel.setBackground(Color.blue);

        putShip1.setSize(90, 90);
        putShip1.setLocation(55, 5);
        putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_north.png", null));

        putShip2.setSize(90, 90);
        putShip2.setLocation(55, 105);
        putShip2.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship2/full_north.png", null));

        putShip3.setSize(90, 90);
        putShip3.setLocation(55, 205);
        putShip3.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship3/full_north.png", null));

        putShip4.setSize(90, 90);
        putShip4.setLocation(55, 305);
        putShip4.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship4/full_north.png", null));

        startButton.setSize(150, 30);
        startButton.setLocation(20, Torpedo.session.map.size*Field.buttonSize+205);

        panel.add(putShip1);
        panel.add(putShip2);
        panel.add(putShip3);
        panel.add(putShip4);
        panel.add(startButton);

        startButton.addActionListener(startButton());
        startButton.addActionListener(putShip1());
        startButton.addActionListener(putShip2());
        startButton.addActionListener(putShip3());
        startButton.addActionListener(putShip4());

        panel.setLayout(null);
        panel.setVisible(true);
    }

    private ActionListener startButton(){
        return new ActionListener(){
             public void actionPerformed(ActionEvent e) {
                 putPanel.panel.setVisible(false);
                 actionPanel.panel.setVisible(true);
             }
        };

    }

    private ActionListener putShip1(){
        return new ActionListener(){
             public void actionPerformed(ActionEvent e) {
                 selectedShip = 1;
                 putShip1.setBackground(Color.red);
                 putShip2.setBackground(null);
                 putShip3.setBackground(null);
                 putShip4.setBackground(null);
                 switch(ship1Direction){
                    case NORTH: ship1Direction = ShipDirection.EAST;
                                putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_east.png", null));
                                break;
                    case EAST:  ship1Direction = ShipDirection.SOUTH;
                                putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_south.png", null));
                                break;
                    case SOUTH: ship1Direction = ShipDirection.WEST;
                                putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_west.png", null));
                                break;
                    case WEST:  ship1Direction = ShipDirection.NORTH;
                                putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_north.png", null));
                                break;
                 }
                 putShip1.repaint();
                 putShip2.repaint();
                 putShip3.repaint();
                 putShip4.repaint();
                 panel.repaint();
                 JOptionPane.showMessageDialog(new JFrame(), "Repaint finished", "Repaint status info", JOptionPane.INFORMATION_MESSAGE); //this here hangs the program when the method is finally called
             }
        };

当单击其中一个 putShip* 按钮时,它应该将自己的图标向右旋转 90°(意味着将其更改为下一个图像),但在单击 startButton 之前它不会执行任何操作,这会将面板更改为另一个一。 (这里只有第一个按钮的actionListener,其他的几乎相同)。该面板与其他两个面板位于 JFrame 中,但它们什么也不做。

我怎样才能让它正常工作?

谢谢。

I'm new to java and I have to get a school project done by Sunday and got a problem.

Here's the code:

private abstract class GamePanel {
    JPanel panel = null;
}

private class PutPanel extends GamePanel {

    JButton putShip1 = new JButton("");
    JButton putShip2 = new JButton("");
    JButton putShip3 = new JButton("");
    JButton putShip4 = new JButton("");

    ShipDirection ship1Direction = ShipDirection.NORTH;
    ShipDirection ship2Direction = ShipDirection.NORTH;
    ShipDirection ship3Direction = ShipDirection.NORTH;
    ShipDirection ship4Direction = ShipDirection.NORTH;



    JButton startButton = new JButton("Start game");

    public PutPanel(){
        this.panel = new JPanel();
        panel.setSize(200, Torpedo.session.map.size*Field.buttonSize+300);

        panel.setBackground(Color.blue);

        putShip1.setSize(90, 90);
        putShip1.setLocation(55, 5);
        putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_north.png", null));

        putShip2.setSize(90, 90);
        putShip2.setLocation(55, 105);
        putShip2.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship2/full_north.png", null));

        putShip3.setSize(90, 90);
        putShip3.setLocation(55, 205);
        putShip3.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship3/full_north.png", null));

        putShip4.setSize(90, 90);
        putShip4.setLocation(55, 305);
        putShip4.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship4/full_north.png", null));

        startButton.setSize(150, 30);
        startButton.setLocation(20, Torpedo.session.map.size*Field.buttonSize+205);

        panel.add(putShip1);
        panel.add(putShip2);
        panel.add(putShip3);
        panel.add(putShip4);
        panel.add(startButton);

        startButton.addActionListener(startButton());
        startButton.addActionListener(putShip1());
        startButton.addActionListener(putShip2());
        startButton.addActionListener(putShip3());
        startButton.addActionListener(putShip4());

        panel.setLayout(null);
        panel.setVisible(true);
    }

    private ActionListener startButton(){
        return new ActionListener(){
             public void actionPerformed(ActionEvent e) {
                 putPanel.panel.setVisible(false);
                 actionPanel.panel.setVisible(true);
             }
        };

    }

    private ActionListener putShip1(){
        return new ActionListener(){
             public void actionPerformed(ActionEvent e) {
                 selectedShip = 1;
                 putShip1.setBackground(Color.red);
                 putShip2.setBackground(null);
                 putShip3.setBackground(null);
                 putShip4.setBackground(null);
                 switch(ship1Direction){
                    case NORTH: ship1Direction = ShipDirection.EAST;
                                putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_east.png", null));
                                break;
                    case EAST:  ship1Direction = ShipDirection.SOUTH;
                                putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_south.png", null));
                                break;
                    case SOUTH: ship1Direction = ShipDirection.WEST;
                                putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_west.png", null));
                                break;
                    case WEST:  ship1Direction = ShipDirection.NORTH;
                                putShip1.setIcon(createImageIcon(Torpedo.session.map.shipPath+"/ship1/full_north.png", null));
                                break;
                 }
                 putShip1.repaint();
                 putShip2.repaint();
                 putShip3.repaint();
                 putShip4.repaint();
                 panel.repaint();
                 JOptionPane.showMessageDialog(new JFrame(), "Repaint finished", "Repaint status info", JOptionPane.INFORMATION_MESSAGE); //this here hangs the program when the method is finally called
             }
        };

When one of the putShip* buttons is clicked, it should rotate its own icon right 90° (means changing it to the next image), but it does nothing until the startButton is clicked, which changes the panel to an other one. (Only the first button's actionListener is here, the others' are practically the same). The panel is in a JFrame with two other panels that yet do nothing.

How could I make it work properly?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2024-09-05 06:15:36

正如用户 Chuk Lee 所说,也将 ActionListener 添加到 putShip* 按钮。这里有一些代码供您添加:

    startButton.addActionListener(startButton());
    startButton.addActionListener(putShip1());
    startButton.addActionListener(putShip2());
    startButton.addActionListener(putShip3());
    startButton.addActionListener(putShip4());

    putShip1.addActionListener(putShip1());
    putShip2.addActionListener(putShip2());
    putShip3.addActionListener(putShip3());
    putShip3.addActionListener(putShip3());

    panel.setLayout(null);
    panel.setVisible(true);

As told by user Chuk Lee add the ActionListeners to the putShip* buttons too. Heres a little bit of code for you to add:

    startButton.addActionListener(startButton());
    startButton.addActionListener(putShip1());
    startButton.addActionListener(putShip2());
    startButton.addActionListener(putShip3());
    startButton.addActionListener(putShip4());

    putShip1.addActionListener(putShip1());
    putShip2.addActionListener(putShip2());
    putShip3.addActionListener(putShip3());
    putShip3.addActionListener(putShip3());

    panel.setLayout(null);
    panel.setVisible(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文