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* ボタンの 1 つがクリックされると、それ自体のアイコンが右に 90 度回転する (次の画像に変更されることを意味します) 必要がありますが、パネルが別のパネルに変更される startButton がクリックされるまで何もしません。(最初のボタンの actionListener のみがここにあり、他のボタンは実質的に同じです)。パネルは JFrame 内にあり、他の 2 つのパネルはまだ何もしていません。

どうすれば適切に機能させることができますか?

ありがとうございました。

4

1 に答える 1

1

ユーザーChuk Leeが言ったように、ボタンにもActionListenersを追加します. 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);
于 2010-05-12T14:27:34.147 に答える