2014-10-10 44 views
1

我正在編寫一個非常基本的代碼,使點在屏幕上移動。我的目標是最終有一個冒險遊戲。它主要是GUI,但我的按鈕有一些問題。代碼非常相似的按鈕不能同時工作。爲什麼?

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

class gui { 
    int x = 240; 
    int y = 240; 
    JPanel panel1 = new JPanel(); 
    MyDrawPanel drawpanel = new MyDrawPanel(); 
    public void go() { 
     JFrame frame = new JFrame("DotMover"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.getContentPane().add(BorderLayout.CENTER, drawpanel); 
     frame.getContentPane().add(BorderLayout.SOUTH, panel1); 
     frame.setSize(500, 500); 
     drawpanel.repaint(); 

     JButton leftbutton = new JButton("<----"); 
     leftbutton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       x = x - 1; 
       drawpanel.repaint(); 
      } 
     }); 
     panel1.add(leftbutton); 

     JButton rightbutton = new JButton("---->"); 
     rightbutton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       x++; 
       drawpanel.repaint(); 
      } 
     }); 
     panel1.add(rightbutton); 

     JButton upbutton = new JButton("Up"); 
     upbutton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       y++; 
       drawpanel.repaint(); 
      } 
     }); 
     panel1.add(upbutton); 

     JButton downbutton = new JButton("Down"); 
     upbutton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       y = y - 1; 
       drawpanel.repaint(); 
      } 
     }); 
     panel1.add(downbutton); 
     frame.setVisible(true); 
    } 

    class MyDrawPanel extends JPanel { 
     public void paintComponent(Graphics g) { 
      g.setColor(Color.WHITE); 
      g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
      g.setColor(Color.GREEN); 
      g.fillOval(x, y, 20, 20); 
     } 
    } 
} 

public class dotmoveriii { 
    public static void main(String[] args) { 
     gui a = new gui(); 
     a.go(); 
    } 
} 

當我在電腦上運行它時,左右按鈕可以工作,但上下按鈕不起作用。這是怎麼回事?

+0

請縮進代碼,以便它是可讀 – njzk2 2014-10-10 17:48:09

+2

動作監聽器沒有設置在downlistener – njzk2 2014-10-10 17:50:37

+1

如果你使用'X ++',爲什麼你不使用'X - '(而不是'x = x - 1')? – Tom 2014-10-10 17:50:51

回答

4

您添加了兩個動作偵聽器,它們可以將每個其他效果都撤消到向上按鈕,並且在向下按鈕上沒有動作偵聽器。這就是爲什麼這兩個按鈕都不會產生任何可見效果

一個按鈕可以有多個動作監聽器。實際上,您的向上按鈕向上移動橢圓形,然後立即向下移動。整體效果是橢圓形保持原位。

顯然,所有你需要做的是修理你複製粘貼錯誤:

downbutton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     y++; // The "up" button should be y--, because the y axis points down 
     drawpanel.repaint(); 
    } 
}); 

(注意使用y--代替y = y - 1既然你已經有了一個++,沒有理由不有一個--)。

0

的問題是在這裏:

JButton upbutton = new JButton("Up"); 
upbutton.addActionListener(new ActionListener() 
{ 
    public void actionPerformed(ActionEvent e) 
    {     
     y++; 
     drawpanel.repaint(); 
    } 
}); 
panel1.add(upbutton); 

JButton downbutton = new JButton("Down"); 
upbutton.addActionListener(new ActionListener() 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     y = y - 1; 
     drawpanel.repaint(); 
    } 
}); 
panel1.add(downbutton); 

創建downbutton後,再次添加監聽器upbutton而不是downbutton

因此,每次單擊upbutton時,都會添加1,然後在第二個偵聽器之後移除。當按下downbutton時,什麼都不會做,這就是爲什麼這個圈子永遠不會移動。您的邏輯不正確。如果你想下去,你需要添加+1而不是刪除一個,否則這個行爲將被顛倒過來。

另外,我注意到你使用了y = -1y++。需要注意的是,你還可以做y--

相關問題